栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Elasticsearch数组must和must_not

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Elasticsearch数组must和must_not

这是一种似乎可以完成您想要的方法:http
:
//sense.qbox.io/gist/4dd806936f12a9668d61ce63f39cb2c284512443

首先,我创建了一个带有显式映射的索引。我这样做是

"tags"
为了将属性设置为
"index":"not_analyzed"
。这意味着将不会以任何方式修改文本,这将简化此示例的查询过程。

curl -XPUT "http://localhost:9200/test_index" -d'{    "mappings": {        "docs" : { "properties": {     "tags" : {         "type": "string",         "index": "not_analyzed"     },     "label" : {         "type": "string"     } }        }    }}'

然后添加一些文档:

curl -XPUT "http://localhost:9200/test_index/docs/1" -d'{    "tags" : [        "tag-1",        "tag-2",        "tag-3",        "tag-A"    ],    "label" : "item 1"}'curl -XPUT "http://localhost:9200/test_index/docs/2" -d'{    "tags" : [        "tag-1",        "tag-2",        "tag-3"    ],    "label" : "item 2"}'curl -XPUT "http://localhost:9200/test_index/docs/3" -d'{    "tags" : [        "tag-1",        "tag-2"    ],    "label" : "item 3"}'

然后,我们可以在过滤器中使用

must
must_not
子句进行查询
bool
,如下所示:

curl -XPOST "http://localhost:9200/test_index/_search" -d'{   "query": {      "filtered": {         "query": { "match_all": {}         },         "filter": { "bool": {    "must": [       {          "terms": {  "tags": [     "tag-1",     "tag-2",     "tag-3"  ],  "execution" : "and"          }       }    ],    "must_not": [       {"term": {   "tags": "tag-A"}       }    ] }         }      }   }}'

产生正确的结果:

{   "took": 3,   "timed_out": false,   "_shards": {      "total": 2,      "successful": 2,      "failed": 0   },   "hits": {      "total": 1,      "max_score": 1,      "hits": [         { "_index": "test_index", "_type": "docs", "_id": "2", "_score": 1, "_source": {    "tags": [       "tag-1",       "tag-2",       "tag-3"    ],    "label": "item 2" }         }      ]   }}

注意子句中过滤器中的

"execution" :"and"
参数。这意味着将仅返回具有所有指定内容的文档(而不是与一个或多个匹配的文档)。那可能就是你所缺少的。您可以在ES文档中阅读有关选项的更多信息。
terms``must``"tags"

如果您在上安装并运行了ES,我在这里做了一个可运行的示例,您可以使用它,

localhost:9200
也可以提供自己的端点。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/399502.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号