欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

ElasticSearch学习:文档的基本操作

时间:2023-05-05

上一个我们基本是围绕索引操作,里面的指定类型,或者文档里面的一些属性。

这里是主要关注文档操作,毕竟主要常见的操作就是围绕文档内容来进行的,毕竟主要是做搜索。

首先先导入一些数据进去。
一 简单的查询操作

put /testdoc/user/1{ "name": "张三", "age": 28, "desc": "法外狂徒", "tags": ["旅游", "渣男", "交友"]} put /testdoc/user/2{ "name": "李四", "age": 30, "desc": "佳木斯大拐", "tags": ["旅游", "靓女", "唱歌"]} #以下是查询#id查询GET testdoc/user/1 #分词查询GET testdoc/user/_search?q=name:张

以下是GET testdoc/user/_search?q=name:张 的返回结果:

{ "took" : 9, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.6931471, "hits" : [ { "_index" : "testdoc", "_type" : "user", "_id" : "1", "_score" : 0.6931471, "_source" : { "name" : "张三", "age" : 28, "desc" : "法外狂徒", "tags" : [ "旅游", "渣男", "交友" ] } } ] }}

二 复杂的查询
排序、分页、高亮、模糊查询、标准查询
首先再添加俩用户

put /testdoc/user/3{ "name": "王二麻子", "age": 28, "desc": "买豆腐", "tags": ["游戏", "猛男", "宅"]} put /testdoc/user/4{ "name": "王二麻", "age": 30, "desc": "主播", "tags": ["直播", "购物", "吹牛"]}

模糊查询

GET testdoc/user/_search{ "query": { "match": { "name": "王二麻" } }}

返回结果

{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 2.5518394, "hits" : [ { "_index" : "testdoc", "_type" : "user", "_id" : "4", "_score" : 2.5518394, "_source" : { "name" : "王二麻", "age" : 30, "desc" : "主播", "tags" : [ "直播", "购物", "吹牛" ] } }, { "_index" : "testdoc", "_type" : "user", "_id" : "3", "_score" : 2.234622, "_source" : { "name" : "王二麻子", "age" : 28, "desc" : "买豆腐", "tags" : [ "游戏", "猛男", "宅" ] } } ] }}

套用一下别人的截图

针对模糊搜索的添加过滤

#真对_source的过滤#_source先看一下都有些什么{ "_index" : "testdoc", "_type" : "user", "_id" : "4", "_version" : 2, "_seq_no" : 4, "_primary_term" : 1, "found" : true, "_source" : { "name" : "王二麻", "age" : 30, "desc" : "主播", "tags" : [ "直播", "购物", "吹牛" ] }}#根据_source里面的name和desc来过滤GET testdoc/user/_search{ "query": { "match": { "name": "王二麻" } }, "_source": ["name", "desc"]}

排序过滤

# ASC 升序 DESC降序GET testdoc/user/_search{ "query": { "match": { "name": "王二麻" } }, "sort":[{ "age": "asc" }]}

分页查询

#这里的form和size类似我们的分页参数的pageNum和pageSizeGET testdoc/user/_search{ "query": { "match": { "name": "王二麻" } }, "sort":[{ "age": "asc" }], "from":0, "size":1}

布尔值查询
这里要注意bool的几个设置,must shuould must_not gt大于 lt小于 (当然还有gte 大于等于 lte 小于等于)

# 多条件查询 must 相当于andGET testdoc/user/_search{ "query": { "bool": { "must": [ {"match": { "name": "王二麻" }}, {"match": { "age": 30 }} ] } }}# 多条件查询 should 相当于orGET testdoc/user/_search{ "query": { "bool": { "should": [ {"match": { "name": "王二麻" }}, {"match": { "age": 30 }} ] } }}# 多条件查询 must_not 相当于 notGET testdoc/user/_search{ "query": { "bool": { "must_not": [ {"match": { "name": "王二麻" }}, {"match": { "age": 30 }} ] } }}# 过滤查询1 age > 24GET testdoc/user/_search{ "query": { "bool": { "must_not": [ {"match": { "name": "王二麻" }} ], "filter": [ {"range": { "age": { "gt": 24 } }} ] } }}# 过滤器2 22

多条件查询
实际还是match匹配的引用,只不过不是完全用上面那些关键字来

GET testdoc/user/_search{ "query": { "match": { "tags": "主播 男" } }}

精确插叙
term关键字根据底层的倒排索引
于match对比:

term直接精确查找match根据分词器来查找

keyword类型和text类型
keyword类型不会被分词器解析,term: 精确匹配

PUT test4{ "mappings": { "properties": { "name": { "type": "text" }, "desc": { "type": "keyword" } } }}PUT /test4/_doc/1{ "name": "Java Name", "desc": "Java Desc"}PUT /test4/_doc/2{ "name": "Java Name", "desc": "Java Desc 2"}GET test4/_search{ "query": { "term": { "desc": "ZP Java Desc" } }}# 按照text类型匹配(查不到可能是自己的分词器不行我这个用的是ik,没加别的分词器,单词的匹配就不行)GET test4/_search{ "query": { "term": { "desc": "Java" } }}

_analyze, _explain是Elasticsearch提供的辅助API,经常不为人所知和所用。_explain 用来帮助分析文档的relevance score是如何计算出来的,而_analyze是Elasticsearch一个非常有用的API,它可以帮助你分析每一个field或者某个analyzer/tokenizer是如何分析和索引一段文字。

GET test4/_search{ "query": { "term": { "desc": "ZP Java Desc" } }}GET _analyze{ "analyzer": "standard", "text":"ZP JAVA"}

多个值精确匹配

PUT /test_db/_doc/3{ "t1": "22", "t2": "2020-09-10"} PUT /test_db/_doc/4{ "t1": "33", "t2": "2020-09-11"} GET test_db/_search{ "query": { "bool": { "should": [ { "term": { "t1": "22" } }, { "term": { "t1": "33" } } ] } }}

高亮查询

GET test5/user/_search{ "query": { "match": { "name": "张三" } }, "highlight": { "pre_tags": "

", "post_tags": "

", "fields": { "name": {} } }}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。