您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

包含异或的嵌套过滤器查询

包含异或的嵌套过滤器查询

您的描述含糊不清,但我尝试制作一个可运行的示例,该示例应在此处运行:https ://www.found.no/play/gist/8940202(也嵌入在下面)

这是我做的几件事:

将过滤器放入filtered-query中。仅当您要过滤匹配时才使用顶层filterpost_filter在Elasticsearch 1.0中重命名),而不要过滤方面。

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

而最重要的,把nested 里面bool,所以逻辑得到正确的WRT。嵌套文档与父文档应该匹配什么。

must_not为您的最后一点添加一个帐户。不知道您是否可以拥有两个名为name的子文档"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
                                    }
                                }
                            }
                        ]
                    }
                ]
            }
        }
    }
}
'
其他 2022/1/1 18:13:53 有586人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶