首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有排除ORs的嵌套筛选查询

带有排除ORs的嵌套筛选查询
EN

Stack Overflow用户
提问于 2014-02-11 02:30:09
回答 1查看 377关注 0票数 1

我无法将结果集限制为与下面两个kol_tags.scored.name选项和kol_tags.scored.score选项都匹配的文档。

我想匹配的文件,有kol_tags.scored.name的“核心种植者”和kol_tags.scored.score在1到100之间,除非他们也有kol_tags.scored.name的“连接”,其中kol_tags.scored.score,而不是在35至65。

考虑到以下映射(为简洁而省略的非嵌套字段):

代码语言:javascript
复制
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"
              }
            }
          }
        }
      }
    }
  }
}

我正在执行以下查询:

代码语言:javascript
复制
{
  "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
                  }
                }
              }
            ]
          }
        ]
      }
    }
  }
}

通过上面的查询,我得到了与“核心种植者”的kol_tags.scored.name和1到100之间的kol_tags.scored.score匹配的文档,以及在任何范围内都具有"Connectivity“和kol_tags.scored.scorekol_tags.scored.name的。

我需要的是匹配的文件:

  • “核心种植者”的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的文档
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-11 18:01:22

您的描述有些含糊不清,但我尝试在这里创建一个可以运行的示例:https://www.found.no/play/gist/8940202 (也嵌入在下面)

我做了几件事:

  • 将过滤器放入filtered-query中。顶级filter (在ElasticSearch1.0中重命名为post_filter )只应用于筛选命中,而不是方面。
  • 使用bool而不是andor,因为过滤器是可选的。这里有更多信息:http://www.elasticsearch.org/blog/all-about-elasticsearch-filter-bitsets/
  • 最重要的是,将nested放在bool中,这样逻辑就可以得到正确的wrt。嵌套文档与父文档之间应该匹配的内容。
  • 添加了一个must_not来解释您的最后一点。不确定是否可以有两个名为"Connectivity"的子文档,但如果可以的话,这就说明了这一点。如果您只拥有一个,则可以删除must_not

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

代码语言:javascript
复制
#!/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
                                    }
                                }
                            }
                        ]
                    }
                ]
            }
        }
    }
}
'
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21691882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档