PUT/test_data/_doc/1{"name":"xiao sa","desc":"First place in the singing contest, The Voice first, love diving","age":25,"tage":["Singer","diver"],"owner":{"job":"Middle school teacher","city":"shenzhen"},"create_date":"2024-03-03","count":30000}PUT/test_data/_doc/2{"name":"zhang feng","desc":"Third place in the Singing contest, second place in the Voice, love mountaineering","age":22,"tage":["Singer","Companion (s) travelling with you"],"owner":{"job":"Freelance work","city":"shenzhen"},"create_date":"2024-03-03","count":22000}PUT/test_data/_doc/3{"name":"li sa","desc":"The Voice of China number one, love diving","age":29,"tage":["Singer","diver"],"owner":{"job":"University teacher","city":"chongqin"},"create_date":"2024-03-02","count":28000}PUT/test_data/_doc/4{"name":"yang chao","desc":"International F1 driver, extreme sports enthusiast","age":25,"tage":["Racing driver","Extreme sports"],"owner":{"job":"Scientific research worker","city":"chongqin"},"create_date":"2024-03-01","count":20000}
基本数据操作
查询所有数据
GET test_data/_search
删除指定数据
DELETE/test_data/_doc/U_2xAo4BpIDK9BOwZXqI
查询结果字段指定字段返回
GET test_data/_search
{"_source":["name","owner.job"],// 指定返回的字段"query":{"match_all":{}}}
多字段查询
GET test_data/_search
{"query":{"multi_match":{"query":"Singer","fields":["desc","tage"]// 同时在desc和tage查询有 Singer 的数据}}}
短语查询 查询词语会被分词,被检索的数据包含查询词语分词的所有并且顺序相同才会被检索到
GET test_data/_search
{"query":{"match_phrase":{"desc":"China number one"}}}
term 精准查询
GET test_data/_search
{"query":{"term":{"name":"li sha"}}}
范围查询
{"query":{"range":{"age":{"gte":25,"lte":29}}}}
组合查询
must 所有条件都满足
{"query":{"bool":{"must":[{"match":{"name":"sa"}},{"match_phrase":{"desc":"China number one"}}]}}}
filter 注意 查询结果不会有_score 得分
{"query":{"bool":{"filter":[{"match":{"name":"sa"}},{"match_phrase":{"desc":"China number one"}}]}}}
must_not
{"query":{"bool":{"must_not":[{"match":{"name":"sa"}},{"match_phrase":{"desc":"China number one"}}]}}}
should 相当于MYSQL 的 or 满足其中一个就行
{"query":{"bool":{"should":[{"match_phrase":{"desc":"China number one"}},{"match":{"name":"zhang"}}]}}}
must 和filter的组合使用,filter不计算相关性评分,先筛选数据后再做数据must匹配可以提升查询性能,减少查询的资源消耗,filter的相同条件查询会有缓存,短时间内再次查询会更快
{"query":{"bool":{"filter":[{"range":{"age":{"gte":20,"lte":29}}}],"must":[{"match":{"name":"zhang"}}]}}}
filter should组合查询,minimum_should_match若不填则should里面的查询条件将失效,minimum_should_match的值是should里面满足条件的最低个数
{"query":{"bool":{"filter":[{"range":{"age":{"gte":20,"lte":29}}}],"should":[{"match":{"name":"zhang"}}],"minimum_should_match":1}}}