具有排除OR的嵌套过滤器查询

时间:2014-02-11 02:30:10

标签: search lucene elasticsearch

对于以下kol_tags.scored.name选项,我无法将结果集限制为与kol_tags.scored.scoreor范围匹配的文档。

我希望匹配“{1}”的“核心种植者”和kol_tags.scored.name介于1和100之间的文档,除非他们还有kol_tags.scored.score“连接”,其中{{1} } NOT 在35到65之间。

给定以下映射(为简洁省略了非嵌套字段):

kol_tags.scored.name

我正在执行以下查询:

kol_tags.scored.score

通过上面的查询,我得到的文档与“Core Grower”GET /production_users/user/_mapping { "user": { "_all": { "enabled": false }, "properties": { "kol_tags": { "type": "nested", "properties": { "scored": { "type": "nested", "properties": { "name": { "type": "string", "index": "not_analyzed", "omit_norms": true, "index_options": "docs" }, "score": { "type": "integer" } } } } } } } } { "filter": { "nested": { "path": "kol_tags.scored", "filter": { "or": [ { "and": [ { "terms": { "kol_tags.scored.name": [ "Core Grower" ] } }, { "range": { "kol_tags.scored.score": { "gte": 1, "lte": 100 } } } ] }, { "and": [ { "terms": { "kol_tags.scored.name": [ "Connectivity" ] } }, { "range": { "kol_tags.scored.score": { "gte": 35, "lte": 65 } } } ] } ] } } } } 的1到100之间以及 ALSO kol_tags.scored.name为“连接“和kol_tags.scored.score在任何范围内。

我需要的是匹配的文件:

    “核心种植者”的
  • kol_tags.scored.name和1到100之间的kol_tags.scored.score
  • “连接”的
  • kol_tags.scored.name和35到65之间的kol_tags.scored.score
  • 排除kol_tags.scored.name“连接”且kol_tags.scored.score小于34且大于66
  • 的所有文档

1 个答案:

答案 0 :(得分:2)

您的描述中存在一些歧义,但我尝试制作一个可在此处运行的可运行示例:https://www.found.no/play/gist/8940202(也在下面嵌入)

以下是我做过的一些事情:

  • 将过滤器放入filtered - 查询中。只有在您希望过滤匹配而非过滤分数时,才应使用顶级filter(在Elasticsearch 1.0中重命名为post_filter

  • 使用bool代替andor,因为过滤器是可缓存的。更多信息:http://www.elasticsearch.org/blog/all-about-elasticsearch-filter-bitsets/

  • 最重要的是,将nested 放在里面bool,这样逻辑就可以了。什么应该匹配嵌套与父文档。

  • 为您的上一个点添加了must_not帐户。不确定您是否可以拥有两个名为"Connectivity"的子文档,但如果可以的话,应该考虑到它。如果您只有一个,则可以删除must_not

你没有提供任何样本文件,所以我做了一些我觉得应该适合你的描述。我认为你不需要两层嵌套。

#!/bin/bash

export ELASTICSEARCH_ENDPOINT="http://localhost:9200"

# Create indexes

curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{
    "mappings": {
        "type": {
            "properties": {
                "kol_tags": {
                    "properties": {
                        "scored": {
                            "type": "nested",
                            "properties": {
                                "name": {
                                    "type": "string",
                                    "index": "not_analyzed"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}'

# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"type"}}
{"kol_tags":{"scored":[{"name":"Core Grower","score":36},{"name":"Connectivity","score":42}]}}
{"index":{"_index":"play","_type":"type"}}
{"kol_tags":{"scored":[{"name":"Connectivity","score":34},{"name":"Connectivity","score":42}]}}
{"index":{"_index":"play","_type":"type"}}
{"kol_tags":{"scored":[{"name":"Core Grower","score":36}]}}
{"index":{"_index":"play","_type":"type"}}
{"kol_tags":{"scored":[{"name":"Connectivity","score":36}]}}
'

# Do searches

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "should": [
                        {
                            "nested": {
                                "path": "kol_tags.scored",
                                "filter": {
                                    "bool": {
                                        "must": [
                                            {
                                                "term": {
                                                    "name": "Core Grower"
                                                }
                                            },
                                            {
                                                "range": {
                                                    "score": {
                                                        "gte": 1,
                                                        "lte": 100
                                                    }
                                                }
                                            }
                                        ]
                                    }
                                }
                            }
                        },
                        {
                            "nested": {
                                "path": "kol_tags.scored",
                                "filter": {
                                    "bool": {
                                        "must": [
                                            {
                                                "term": {
                                                    "name": "Connectivity"
                                                }
                                            },
                                            {
                                                "range": {
                                                    "score": {
                                                        "gte": 35,
                                                        "lte": 65
                                                    }
                                                }
                                            }
                                        ]
                                    }
                                }
                            }
                        }
                    ],
                    "must_not": [
                        {
                            "nested": {
                                "path": "kol_tags.scored",
                                "filter": {
                                    "bool": {
                                        "must": [
                                            {
                                                "term": {
                                                    "name": "Connectivity"
                                                }
                                            },
                                            {
                                                "not": {
                                                    "range": {
                                                        "score": {
                                                            "gte": 35,
                                                            "lte": 65
                                                        }
                                                    }
                                                }
                                            }
                                        ]
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}
'

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "filter": {
        "nested": {
            "path": "kol_tags.scored",
            "filter": {
                "or": [
                    {
                        "and": [
                            {
                                "terms": {
                                    "kol_tags.scored.name": [
                                        "Core Grower"
                                    ]
                                }
                            },
                            {
                                "range": {
                                    "kol_tags.scored.score": {
                                        "gte": 1,
                                        "lte": 100
                                    }
                                }
                            }
                        ]
                    },
                    {
                        "and": [
                            {
                                "terms": {
                                    "kol_tags.scored.name": [
                                        "Connectivity"
                                    ]
                                }
                            },
                            {
                                "range": {
                                    "kol_tags.scored.score": {
                                        "gte": 35,
                                        "lte": 65
                                    }
                                }
                            }
                        ]
                    }
                ]
            }
        }
    }
}
'
相关问题