赞
踩
版本:elasticSearch7.6.1
借鉴:https://blog.csdn.net/weixin_41910694/article/details/109407919
POST /forum/_doc/_bulk
{ "index": { "_id": 1 }}
{ "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" }
{ "index": { "_id": 2 }}
{ "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden": false, "postDate": "2017-01-02" }
{ "index": { "_id": 3 }}
{ "articleID" : "JODL-X-1937-#pV7", "userID" : 2, "hidden": false, "postDate": "2017-01-01" }
{ "index": { "_id": 4 }}
{ "articleID" : "QQPX-R-3956-#aD8", "userID" : 2, "hidden": true, "postDate": "2017-01-02" }
# 删除索引
DELETE forum
GET /forum/_search
{
"query": {
"term": {
"userID": "1"
}
}
}
GET /forum/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"userID": "1"
}
},
"boost": 1.3
}
}
}
这里有个小插曲,一开始我用【错误答案中的dsl】查出来的结果是错误的。结果是XHDK-A-1293-#fJ3、JODL-X-1937-#pV7,这两条都是2017-01-01的数据,看样子articleID的条件都没有生效。
这个错误产生的原因是,ElasticSearch5.0以后,字符串类型有重大变更,移除了string类型,string字段被拆分成两种新的数据类型: text和keyword。如果不指定类型,ElasticSearch字符串的字段(如新增字段articleID)将默认被同时映射成text和keyword类型,会自动创建下面的动态映射(dynamic mappings):
# articleID为text类型;articleID.keyword为keyword类型 { "articleID": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } # 可以使用以下dsl看分词后的结果 GET /_analyze { "text": ["KDKE-B-9947-#kL5"] } # 可以使用以下dsl看索引的结果 GET /forum/_mapping
所以错误答案中的dsl的term+articleID,虽然对输入是不分词的,但是由于articleID是text类型,搜索结果被分词了,所以也就找不到了。
正确答案1中使用match_phrase+articleID,是因为结果是被分词的,而且match_phrase是对输入也会分词的,并且结果要包含match_phrase分词后的所有短语+顺序一致,所以也能匹配上。
正确答案2中使用term+articleID.keyword,是因为结果是keyword不分词,而且term对输入也部分此,所以也能匹配上。
错误答案:
GET /forum/_search { "query": { "bool": { "must_not": [ { "term": { "articleID": { "value": "XHDK-A-1293-#fJ3" } } } ], "should": [ { "term": { "postDate": { "value": "2017-01-01" } } }, { "term": { "articleID":{ "value": "KDKE-B-9947-#kL5" } } } ] } } }
正确答案1:
GET /forum/_search { "query": { "bool": { "must_not": [ { "match_phrase": { "articleID": "XHDK-A-1293-#fJ3" } } ], "should": [ { "term": { "postDate": { "value": "2017-01-01" } } }, { "match_phrase": { "articleID": "KDKE-B-9947-#kL5" } } ] } } } # 结果是KDKE-B-9947-#kL5、JODL-X-1937-#pV7
正确答案2:
GET /forum/_search { "query": { "bool": { "must_not": [ { "term": { "articleID.keyword": { "value": "XHDK-A-1293-#fJ3" } } } ], "should": [ { "term": { "postDate": { "value": "2017-01-01" } } }, { "term": { "articleID.keyword":{ "value": "KDKE-B-9947-#kL5" } } } ] } } } # 结果是KDKE-B-9947-#kL5、JODL-X-1937-#pV7
比如有2条记录:[A,B,C]、[B、C]
我输入A、C,查出来[A,B,C]、[B、C],这叫随意包含。[A,B,C]和[B,C]都有包含A,C至少一个元素。
我输入A、B,查出来[A,B,C],这叫完全包含。[A,B,C]包含A,C全部元素。
我输入B,C,查出来[B,C],这叫仅包含。[B,C]包含B,C全部元素,并且[B, C]中只有B、C两个元素。
首先先更新下数据
POST /forum/_doc/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"tag" : ["java", "js", "hadoop"]} }
{ "update": { "_id": "2"} }
{ "doc" : {"tag" : ["java", "js"]} }
{ "update": { "_id": "3"} }
{ "doc" : {"tag" : ["js", "hadoop"]} }
{ "update": { "_id": "4"} }
{ "doc" : {"tag" : ["elasticsearch"]} }
# 方法一 GET /forum/_search { "query": { "bool": { "should": [ { "term": { "articleID.keyword": { "value": "KDKE-B-9947-#kL5" } } }, { "term": { "articleID.keyword": { "value": "QQPX-R-3956-#aD8" } } } ] } } }
# 方法二
GET /forum/_search
{
"query": {
"terms": {
"articleID.keyword": [
"KDKE-B-9947-#kL5",
"QQPX-R-3956-#aD8"
]
}
}
}
GET /forum/_search
{
"query": {
"term": {
"tag": {
"value": "java"
}
}
}
}
GET /forum/_search
{
"query": {
"terms": {
"tag": [
"java",
"js"
]
}
}
}
GET /forum/_search { "query": { "bool": { "must": [ { "term": { "tag": { "value": "java" } } }, { "term": { "tag": { "value": "js" } } } ] } } }
# 目前只知道一种解决方案,就是在添加tag数组数据的时候,同时维护一个tag_count。 POST /forum/_doc/_bulk { "update": { "_id": "1"} } { "doc" : {"tag" : ["java", "js", "hadoop"], "tag_count": 3} } { "update": { "_id": "2"} } { "doc" : {"tag" : ["java", "js"], "tag_count": 2} } { "update": { "_id": "3"} } { "doc" : {"tag" : ["js", "hadoop"], "tag_count": 2} } { "update": { "_id": "4"} } { "doc" : {"tag" : ["elasticsearch"], "tag_count": 1} } # 然后在查询 GET /forum/_search { "query": { "bool": { "must": [ { "term": { "tag": { "value": "java" } } }, { "term": { "tag": { "value": "js" } } }, { "term": { "tag_count": { "value": "2" } } } ] } } }
疑问:
"terms": {
"tag": [
"java",
"js"
]
}
为帖子数据增加浏览量的字段
POST /forum/_doc/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"view_count" : 30} }
{ "update": { "_id": "2"} }
{ "doc" : {"view_count" : 50} }
{ "update": { "_id": "3"} }
{ "doc" : {"view_count" : 100} }
{ "update": { "_id": "4"} }
{ "doc" : {"view_count" : 80} }
GET /forum/_search
{
"query": {
"range": {
"view_count": {
"gte": 30,
"lte": 60
}
}
}
}
GET /forum/_search
{
"query": {
"range": {
"postDate": {
"gte": "now-10y"
}
}
}
}
为帖子数据增加标题字段
POST /forum/_doc/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"title" : "this is java and elasticsearch blog"} }
{ "update": { "_id": "2"} }
{ "doc" : {"title" : "this is java blog"} }
{ "update": { "_id": "3"} }
{ "doc" : {"title" : "this is elasticsearch blog"} }
{ "update": { "_id": "4"} }
{ "doc" : {"title" : "this is elasticsearch, java, hadoop blog"} }
{ "update": { "_id": "5"} }
{ "doc" : {"title" : "this is spark blog"} }
GET /forum/_search
{
"query": {
"match": {
"title": "java elasticsearch"
}
}
}
解析:"title": "java elasticsearch"中会被分词为java和elasticsearch,所以才能命中倒排索引
# 方法一
GET /forum/_search
{
"query": {
"match": {
"title": {
"query": "java elasticsearch",
"operator": "and"
}
}
}
}
# 方法二
GET /forum/_search
{
"query": {
"match_phrase": {
"title": {
"query": "java elasticsearch",
"slop": 1000
}
}
}
}
GET /forum/_search
{
"query": {
"match": {
"title": {
"query": "java elasticsearch spark hadoop",
"minimum_should_match": "75%"
}
}
}
}
GET /forum/_search { "query": { "bool": { "must": { "match": { "title": "java" } }, "must_not": { "match": { "title": "spark" } }, "should": [ { "match": { "title": "hadoop" } }, { "match": { "title": "elasticsearch" } } ] } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。