10、过滤查询
POST /student/_search { "query": { "bool": { "filter": [ { "range": { "age": { "gte": 18, "lte": 20 } } },{ "match": { "name": "诗圣" } } ] } } }
- 模糊匹配用match
- 精确查询用term
- 多条件组合是bool
- must,filter相配合
- should非强制要求
11、判断文档是否存在
#判断文档是否存在 HEAD /student/_doc/1001
12、exists查询
POST /student/_search { "query": { "exists": { "field": "hello" } } } # 找到有hello属性的文档 { "took" : 739, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "student", "_type" : "_doc", "_id" : "1006", "_score" : 1.0, "_source" : { "name" : "白居易", "hello" : "word" } } ] } }
13、批量
#批量查询ID POST student/_doc/_mget { "ids":["1001","1002"] }
四、其他功能
1、排序
POST student/_search { "query": { "match_all": { } }, "sort": [ { "age": { "order": "asc" } } ] }
2、高亮
#高亮 GET student/_search { "query": { "match": { "age": "21" } }, "highlight": { "fields": { "age": { "pre_tags": "<span style='color:res'>", "post_tags": "</span>" } } } }
3、_source-部分字段
POST /student/_search { "query": { "match": { "name": "诗圣" } }, "_source": ["name"] }
4、分页
#分页 POST student/_search { "query": { "match_all": { } }, "from": 0, "size": 2 }
聚合
五、映射与分词
1、Mapping
- ES里面保存是JSON
- 第一次保存了以后,每个索引的数据模型就确定好了,es在第一次保存一个json文档的时候就自动确定了。
- 在自动确定阶段,所有文本数据都会被确定为text,默认就可以全文检索,这个文本值在存储的时候分词,利用倒排索引存储
- Mappings第一次已经确定好的属性就不能修改了,但是未知的属性随便添加
自定义的数据类型(text,keyword),一定告诉es,这个属性的精确类型
2、分词
- 分词发生在text字段
- 保存text类型的数据
- 把这个值先分词(英文:空格为分割 中文:词库 比较麻烦)
- 存储利用倒排索引,会记录这些单词都在哪些文档出现
- 检索
- 搜索“我是中国人”
- 把要搜索的”我是中国人“分词(利用词库对比)
- 中国人: 看那些文档里有
- 中国 看那些文档里有
- 我 看那些文档里有
- 是 看那些文档里有
GET /_analyze { "analyzer": "standard", "text":"Offer收割机" }
必须使用中文分词器
ik分词器
2.1安装ik分词器
下载当前对应版本;
没下载到对应版本的话,可以在ik分词器解压后,在plugin-descriptor.properties
文件中,修改成对应的ES版本
GET /_analyze { "analyzer": "ik_smart", "text":"Offer收割机" }
PUT world { "mappings": { "properties": { "name":{ "type": "keyword" }, "song":{ "type": "text", "analyzer": "ik_smart" } } } }
3 Nested嵌入式
PUT /test_nested { "mappings": { "properties": { "user":{ "type": "nested" } } } }
六、Springboot整合ES
6.1 引依赖
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.14.0</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.14.0</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.14.0</version> <exclusions> <exclusion> <artifactId>commons-logging</artifactId> <groupId>commons-logging</groupId> </exclusion> </exclusions> </dependency>
6.2 写配置文件
spring.elasticsearch.rest.uris[0]=http://localhost:9200
6.3 写测试代码
6.3.1 索引操作
@Resource RestHighLevelClient restHighLevelClient; @Test public void contextLoads() { System.out.println("restHighLevelClient = " + restHighLevelClient); } @Test public void createIndex() throws IOException { CreateIndexRequest request = new CreateIndexRequest("google"); request.settings(Settings.builder() .put("index.number_of_shards","1") .put("index.number_of_replicas","1") .build() ); CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); System.out.println("response = " + response.isAcknowledged() ); } @Test public void deleteIndex() throws IOException { DeleteIndexRequest request = new DeleteIndexRequest("hello"); AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT); boolean b = delete.isAcknowledged(); System.out.println(b?"删除成功":"删除失败"); }
6.3.2 待更新 ,嘻嘻