elasticsearch-笔记

elasticsearch-笔记

起男 1,285 2021-07-22

elasticsearch-笔记

视频: 【尚硅谷】ElasticSearch入门到精通2021最新教程(基于ELK技术栈elasticsearch 7.8.x版本)_哔哩哔哩_bilibili

elasticsearch,简称es,es是一个开源的高扩展的分布式全文搜索引擎,是整个elasticstack技术栈的核心。它可用近乎于实时的存储、检索数据;本身扩展性很好,可用扩展到上百台服务器,处理pb级别的数据

数据格式

es是面向文档型数据库,一条数据在这里就是一个文档

和MySQL进行类比

esmysql
index(索引)database(数据库)
type(类型)table(表)
documenta(文档)row(行)
fields(字段)column(列)

es里的index可用看做一个库,而type相当于表,document则相当于表的行

这里type的概念已经被注解弱化,es6中,一个index下已经只能包含一个type,es7中type概念已经删除

基础操作

索引操作

操作请求方式请求
创建索引puthttp://localhost:9200/{索引名}
查询索引gethttp://localhost:9200/{索引名}
查询所有索引gethttp://localhost:9200/_cat/indices?v
删除索引deletehttp://localhost:9200/{索引名}

文档操作

操作请求方式请求请求体
创建文档posthttp://localhost:9200/{索引}/_docjson
创建文档(自定义id)post、puthttp://localhost:9200/{索引}/_doc/{自定义id}
http://localhost:9200/{索引}/_create/{自定义id}
json
主键查询gethttp://localhost:9200/{索引}/\_doc/{文档id}
查询索引下所有文档gethttp://localhost:9200/{索引}/_search可选:{“query”:{"match_all":{}}
全量更新puthttp://localhost:9200/{索引}/_doc/{文档id}json
局部更新posthttp://localhost:9200/{索引}/_update/{文档id}{"doc":}
删除deletehttp://localhost:9200/{索引}/\_doc/{文档id}
条件查询gethttp://localhost:9200/shoping/_search?q=:
http://localhost:9200/{索引}/_search
可选:{“query”:{"match":}}
分页查询gethttp://localhost:9200/{索引}/_search{"from":启示位置,“size”:每页条数}
过滤字段gethttp://localhost:9200/{索引}/_search{"_source":["展示字段"]}
排序gethttp://localhost:9200/{索引}/_search{"sort":{"字段":{"order":"desc或asc"}}}
完全匹配gethttp://localhost:9200/{索引}/_search{“query”:{"match_phrase":}}
高亮显示gethttp://localhost:9200/{索引}/_search{"highlight":{fields:{key:{}}}}

多条件查询

get  http://localhost:9200/{索引}/_search
{
    "query":{ //查询
        "bool":{ //条件
            "must":[  //must:必须 、should:应该、must_not:必须不
                {//条件1
                    "match":{  //匹配
                        "category":"小米"
                    }
                },{//条件2
                    "match":{  
                        "price":10
                    }
                }
            ],
            "filter":{ //过滤
                "range":{ //范围
                    "price":{
                        "gt":5000 //大于5000
                    }
                }
            }
        }
    }
}

聚合函数

get  http://localhost:9200/{索引}/_search
{
    "aggs":{ //聚合操作
        "price_group":{ //自定义名称
            "terms":{ //分组 avg平均值
                "field":"price" //分组字段
            }
        }
    },
    "size":0 //查询数量,用来去除原始数据
}

映射关心

put http://localhost:9200/{索引名}/_mapping
{
    "properties":{
        "name":{ //字段
            "type":"text", //类型是文本,可用分词
            "index":true //这个字段可用索引查询
        },
        "sex":{
            "type":"keyword", //不能分词,需要完整匹配
            "index":true
        },
        "tel":{
            "type":"keyword", //不能分词,需要完整匹配
            "index":false //不能被索引
        }
    }
}

javaAPI

依赖

    <dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.8.0</version>
        </dependency>
        <!-- elasticsearch 的客户端 -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.8.0</version>
        </dependency>
    </dependencies>

创建客户端

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200))
        );

索引操作

创建索引

		CreateIndexResponse response = client
                .indices()
                //设置索引名称
                .create(new CreateIndexRequest("user"), RequestOptions.DEFAULT);
        //响应状态
        System.out.println(response.isAcknowledged());

查询索引

        GetIndexResponse response = client
                .indices()
                .get(new GetIndexRequest("user"), RequestOptions.DEFAULT);
        //结果
        System.out.println(response.getAliases());
        System.out.println(response.getMappings());
        System.out.println(response.getSettings());

删除索引

        AcknowledgedResponse response = client
                .indices()
                .delete(new DeleteIndexRequest("user"), RequestOptions.DEFAULT);
        //结果
        System.out.println(response.isAcknowledged());

文档操作

插入数据

		IndexResponse response = client
                .index(new IndexRequest()
                        .index("user")
                        .id("1")
                        .source(userJson, XContentType.JSON), RequestOptions.DEFAULT);
        //结果
        System.out.println(response.getResult());

修改数据(局部)

		UpdateResponse response = client
                .update(new UpdateRequest()
                        .index("user")
                        .id("1")
                        .doc(XContentType.JSON,"sex","女"), RequestOptions.DEFAULT);
        System.out.println(response.getResult());

查询数据

		GetResponse response = client.get(new GetRequest()
                .index("user")
                .id("1"), RequestOptions.DEFAULT);

        System.out.println(response.getSourceAsString());

删除数据

		DeleteResponse response = client
                .delete(new DeleteRequest()
                        .index("user")
                        .id("1"), RequestOptions.DEFAULT);
        System.out.println(response.getResult());

批量插入

		BulkResponse responses = client
                .bulk(new BulkRequest()
                        .add(new IndexRequest().index("user").id("2").source(XContentType.JSON,"name","张三"))
                        .add(new IndexRequest().index("user").id("3").source(XContentType.JSON,"name","李四")), RequestOptions.DEFAULT);

        System.out.println(responses.getTook());
        System.out.println(responses.getItems());

批量删除

		BulkResponse responses = client
                .bulk(new BulkRequest()
                        .add(new DeleteRequest().index("user").id("2"))
                        .add(new DeleteRequest().index("user").id("3")), RequestOptions.DEFAULT);

        System.out.println(responses.getTook());
        System.out.println(responses.getItems());

高级查询

全量查询

        SearchResponse response = client.search(new SearchRequest()
                .indices("user")
                .source(new SearchSourceBuilder()
                        .query(QueryBuilders.matchAllQuery())), RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        //条数
        System.out.println(hits.getTotalHits());
        //时间
        System.out.println(response.getTook());
        //数据
        for(SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }

条件查询

        SearchResponse response = client.search(new SearchRequest()
                .indices("user")
                .source(new SearchSourceBuilder()
                        .query(QueryBuilders.termQuery("name","zhangsan"))), RequestOptions.DEFAULT);

分页查询

	SearchResponse response = client.search(new SearchRequest()
                .indices("user")
                .source(new SearchSourceBuilder()
                        .query(QueryBuilders.matchAllQuery())
                        .from(0)
                        .size(5)), RequestOptions.DEFAULT);

排序

        SearchResponse response = client.search(new SearchRequest()
                .indices("user")
                .source(new SearchSourceBuilder()
                        .sort("age", SortOrder.ASC)), RequestOptions.DEFAULT);

过滤字段

        SearchResponse response = client.search(new SearchRequest()
                .indices("user")
                .source(new SearchSourceBuilder()
                        //参数1 包含字段,参数2 排除字段
                        .fetchSource(new String[]{"name"},new String[]{})), RequestOptions.DEFAULT);

组合条件

        SearchResponse response = client.search(new SearchRequest()
                .indices("user")
                .source(new SearchSourceBuilder()
                        .query(QueryBuilders
                                .boolQuery()
                                .must(QueryBuilders.matchQuery("name", "zhangsan"))
                                .must(QueryBuilders.matchQuery("age", 10)))), RequestOptions.DEFAULT);

范围查询

        SearchResponse response = client.search(new SearchRequest()
                .indices("user")
                .source(new SearchSourceBuilder()
                        .query(QueryBuilders
                                .rangeQuery("age")
                                .gt(10)
                                .lt(100))), RequestOptions.DEFAULT);

模糊查询

        SearchResponse response = client.search(new SearchRequest()
                .indices("user")
                .source(new SearchSourceBuilder()
                        .query(QueryBuilders
                                .fuzzyQuery("name","zhangsa")
                                //差一个
                                .fuzziness(Fuzziness.ONE))), RequestOptions.DEFAULT);

高亮查询

        SearchResponse response = client.search(new SearchRequest()
                .indices("user")
                .source(new SearchSourceBuilder()
                        .query(QueryBuilders.termQuery("name","zhangsan"))
                        .highlighter(new HighlightBuilder()
                                .preTags("<a>")
                                .postTags("</a>")
                                .field("name"))), RequestOptions.DEFAULT);

        for (SearchHit hit : response.getHits()) {
            System.out.println(hit.getHighlightFields().get("name").getFragments()[0]);
        }

聚合查询-最大值

        SearchResponse response = client.search(new SearchRequest()
                .indices("user")
                .source(new SearchSourceBuilder()
                        .aggregation(AggregationBuilders
                                .max("maxAge")
                                .field("age"))), RequestOptions.DEFAULT);
        Max max = response.getAggregations().get("maxAge");
        System.out.println(max.getValue());

聚合查询-分组

		SearchResponse response = client.search(new SearchRequest()
                .indices("user")
                .source(new SearchSourceBuilder()
                        .aggregation(AggregationBuilders
                                .terms("termsAge")
                                .field("age"))), RequestOptions.DEFAULT);
        ParsedLongTerms termsAge = response.getAggregations().get("termsAge");
        for (Terms.Bucket bucket : termsAge.getBuckets()) {
            System.out.println(bucket.getKeyAsString()+":"+bucket.getDocCount());
        }