一、认识ELK、ES
1.什么是ELK?
📚ELK是Elasticsearch、Logstash、Kibana三大开源框架首字母大写简称。 💧Elasticsearch是强大的数据搜索引擎,是分布式、通过restful方式进行交互的近实时搜索平台框架。 💧Logstash是免费且开放的服务器端数据处理通道,能够从多个来源收集数据,转换数据后将数据发送到数据存储库中。能够动态地采集、转换和传输数据,不受格式和复杂度的影响。 💧Kibana是针对Elasticsearch的开元分析及可视化平台,用于搜索、查看交互存储在Elasticsearch索引中的数据。
2.什么是ElasticSearch
elasticsearch:基于lucene的开源搜索引擎,是一个分布式的搜索分析系统,提供搜集、分析、存储数据三大功能。
3.ElasticSearch下载安装教程
ElasticSearch下载安装教程详解【7.10版本】:https://blog.csdn.net/Kevinnsm/article/details/120604537?spm=1001.2014.3001.5501
二、索引的CRUD
默认已经打开ES,使用postman进行测试
1.创建索引
如下图所示,创建索引obj,
(1)localhost:9200代表Es服务器地址 (2)obj代表索引 (3)put请求代表创建
2.查询某个索引信息
(1)localhost:9200代表Es服务器地址 (2)obj代表索引 (3)get代表查询
3.查询所有索引信息
localhost:9200/_cat/indices?v
4.删除索引
(1)localhost:9200代表Es地址 (2)obj代表要删除的索引 (3)delete代表删除
查询所有索引信息就会发现不存在了
三、文档的CRUD
1.创建文档
Ⅰ.随机id
首先创建一个obj索引 创建文档
obj代表索引 _doc代表在该索引下创建文档 post代表创建
Ⅱ.自定义文档id
localhost:9200/索引/_doc/文档id
2.查询文档
Ⅰ.查询文档信息
localhost:9200/索引/_doc/文档id 请求方式:get
Ⅱ.全查询
localhost:9200/obj/_search 请求方式 get
3.修改文档
Ⅰ.全量修改
修改之前的数据如下,对name进行修改
put请求代表全量修改
再次查询结果如下,修改成功!
Ⅱ.局部修改
_update代表修改 post请求局部修改 1111为文档id
修改之后内容如下
4.删除文档
delete请求 obj:索引 1111:文档id
四、条件查询
实现在obj索引下创建四个文档
1.请求路径传递参数
_search查询该索引下的所有文档 q=province:beijing代表查询条件
2.请求体传递参数
query代表查询 match代表匹配条件
五、分页查询
1.查询全部
{
"query":{ 查询
"match_all":{ 匹配所有
}
}
}
match_all代表查询全部
2.分页查询
from计算公式(页码- 1)* size
{
"query":{
"match_all":{
}
},
"from":0, 第几个数据
"size":1 每页数据大小
}
3.指定数据段
{
"query":{
"match_all":{
}
},
"from":0,
"size":2,
"_source":["name"] 指定name数据段
}
4.排序
{
"query":{
"match_all":{ //匹配所有
}
},
"from":0, //从第零条数据开始
"size":3, //每页大小
"_source":["age"], //执行age字段
"sort" : {
"age" : { //按照age字段排序
"order" : "asc" //升序排列
}
}
}
六、多条件查询
1.同时满足
{
"query" : {
"bool" : { //多条件
"must" : [ //必须同时满足
{
"match" : { //匹配条件
"province" : "beijing"
}
}
]
}
}
}
多个匹配条件
{
"query" : {
"bool" : {
"must" : [
{
"match" : {
"province" : "beijing"
}
},
{
"match" : {
"name" : "aaa"
}
}
]
}
}
}
2.满足某一个条件
{
"query" : {
"bool" : {
"should" : [ //表示满足一个match即可
{
"match" : {
"province" : "beijing"
}
},
{
"match" : {
"name" : "bbb"
}
}
]
}
}
}
七、范围查询
{
"query" : {
"bool" : {
"must" : [
{
"match" : {
"province" : "beijing"
}
}
],
"filter" : { //过滤
"range" : { //范围
"age" : {
"gt" : "18" //年龄大于18
}
}
}
}
}
}
八、高亮显示
{
"query" : {
"match" : {
"name" : "aaa"
}
},
"highlight" : { //高亮显示
"fields" : {
"name" : {}
}
}
}
九、聚合查询
{
"aggs" : { //聚合查询
"group_one" : { //自定义组名
"terms" : { //分组查询
"field" : "age"
}
}
},
"size" : 0 //去掉原始数据(因为默认也会查询出所有原始数据)
}
{
"aggs" : { //聚合查询
"age_avg" : { //自定义
"avg" : { //求平均值
"field" : "age"
}
}
},
"size" : 0
}
十、映射关系(分词)
首先创建user索引,然后执行_mapping开始映射
{
"properties" : {
"name" : {
"type" : "text", //表示name进行分词
"index" : true //可以索引查询
},
"age" : {
"type" : "keyword", //不能进行分词操作(也就是必须全匹配)
"index" : true
},
"province" : {
"type" : "keyword",
"index" : false //不能进行索引查询
}
}
}
向索引中加入文档内容 查询
十一、Java代码操作索引
在@Before和@After方法中已经对Client进行了初始化和关闭
1.创建索引
public class EsClient {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
CreateIndexRequest request = new CreateIndexRequest("api");
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
boolean flag = response.isAcknowledged();
System.out.println("索引:"+flag);
client.close();
}
}
使用postman查询全部索引
2.索引查询
private RestHighLevelClient client;
@Before
public void init() {
client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
}
@Test
public void get() throws IOException {
GetIndexRequest request = new GetIndexRequest("api");
GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
System.out.println(response.getAliases());
System.out.println(response.getMappings());
System.out.println(response.getDefaultSettings());
}
@After
public void close() throws IOException {
client.close();
}
3.删除索引
@Test
public void deleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("api");
AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
}
十二、API文档操作
1.创建文档
@Test
public void createDoc() throws IOException {
IndexRequest request = new IndexRequest();
User user = new User();
user.setName("张山").setAge(20).setProvince("henan");
ObjectMapper mapper = new ObjectMapper();
String valueJson = mapper.writeValueAsString(user);
request.source(valueJson, XContentType.JSON);
request.index("user").id("1001");
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
}
2.修改文档
public void updateDoc() throws IOException {
UpdateRequest request = new UpdateRequest();
request.index("user").id("1001");
request.doc(XContentType.JSON, "age", 51);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
}
3.查询文档
@Test
public void getDoc() throws IOException {
GetRequest request = new GetRequest();
request.index("user").id("1001");
GetResponse response = client.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
}
4.删除文档
@Test
public void deleteDoc() throws IOException {
DeleteRequest request = new DeleteRequest();
request.index("user").id("1001");
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
}
5.批量创建文档
@Test
public void bulkCreateDoc() throws IOException {
BulkRequest request = new BulkRequest();
request.add(new IndexRequest("user").id("1000").source(XContentType.JSON, "name", "李氏"));
request.add(new IndexRequest("user").id("1001").source(XContentType.JSON, "name", "王二"));
request.add(new IndexRequest("user").id("1002").source(XContentType.JSON, "name", "kO"));
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());
System.out.println(response.getItems());
}
6.批量删除文档
@Test
public void bulkDeleteDoc() throws IOException {
BulkRequest request = new BulkRequest();
request.add(new DeleteRequest().index("user").id("1000"));
request.add(new DeleteRequest().index("user").id("1001"));
request.add(new DeleteRequest().index("user").id("1002"));
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());
}
7.高级查询
Ⅰ.全量查询
@Test
public void searchAll() throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user");
request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
System.out.println(hit);
}
}
Ⅱ.条件查询
@Test
public void conditionSearch() throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user");
request.source(new SearchSourceBuilder().query(QueryBuilders.termsQuery("name", "ko1")));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
}
}
Ⅲ.分页查询
@Test
public void pageSearch() throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user");
SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
builder.from(0).size(2);
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
}
}
Ⅳ.排序查询
为每个数据加入age属性
@Test
public void sortSearch() throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user");
SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
builder.sort("age", SortOrder.ASC);
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
}
}
Ⅴ.字段查询
@Test
public void fieldSearch() throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user");
SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
String[] includes = {"name", "age"};
String[] excludes = {"age"};
builder.fetchSource(includes, excludes);
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
}
}
Ⅵ.组合查询
必须同时满足must中的条件
@Test
public void groupSearch() throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user");
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.matchQuery("name", "ko1"));
builder.query(boolQuery);
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
}
}
should意味着或的意思 与must相反
@Test
public void groupSearch() throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user");
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.should(QueryBuilders.matchQuery("name", "ko1"));
boolQuery.should(QueryBuilders.matchQuery("name", "ko2"));
builder.query(boolQuery);
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
}
}
Ⅶ.范围查询
@Test
public void rangeSearch() throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user");
SearchSourceBuilder builder = new SearchSourceBuilder();
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
rangeQuery.gte(20).lte("50");
builder.query(rangeQuery);
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
}
}
Ⅷ.模糊查询
Fuzziness.ONE代表相差一个字符 可以查询出来
@Test
public void partSearch() throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user");
SearchSourceBuilder builder = new SearchSourceBuilder();
FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name", "ko").fuzziness(Fuzziness.ONE);
builder.query(fuzzyQueryBuilder);
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
}
}
Ⅸ.高亮查询
@Test
public void highLightSearch() throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user");
SearchSourceBuilder builder = new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "ko1");
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("<font color='red'>");
highlightBuilder.postTags("</font>");
highlightBuilder.field("name");
builder.highlighter(highlightBuilder);
builder.query(termQueryBuilder);
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits()) {
System.out.println(hit);
}
}
|