更新时间:2023-12-04 11:04:58
这是一个似乎完成你想要的方法: http ://sense.qbox.io/gist/4dd806936f12a9668d61ce63f39cb2c284512443
Here is a method that seems to accomplish you want: http://sense.qbox.io/gist/4dd806936f12a9668d61ce63f39cb2c284512443
首先我创建了一个具有显式映射的索引。我这样做,所以我可以将标签
属性设置为index:not_analyzed
。这意味着文本不会以任何方式进行修改,这将简化此示例的查询过程。
First I created an index with an explicit mapping. I did this so I could set the "tags"
property to "index": "not_analyzed"
. This means that the text will not be modified in any way, which will simplify the querying process for this example.
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"
}'
然后,我们可以使用中
中$ c>和
过滤器如下: must_not
> bool
Then we can query using must
and must_not
clauses in a bool
filter as follows:
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"
}
}
]
}
}
注意执行:和
子句中的
参数。这意味着只有具有指定的所有术语
过滤器中的标签/ code的文档将被返回(而不是匹配一个或多个)。这可能是你失踪了您可以在 ES文档中阅读有关这些选项的更多信息>。
我创建了一个可运行的示例这里,你可以玩,如果您有ES安装并运行在 localhost:9200
,或者您可以提供您自己的端点。
I made a runnable example here that you can play with, if you have ES installed and running at localhost:9200
, or you can provide your own endpoint.