elasticsearch,Search,Lucene,elasticsearch" /> elasticsearch,Search,Lucene,elasticsearch" />

Search 带排除OR的嵌套筛选查询

Search 带排除OR的嵌套筛选查询,search,lucene,elasticsearch,Search,Lucene,elasticsearch,我无法将结果集限制为同时匹配以下或选项的kol_标记.scored.name和kol_标记.scored.scored.score范围的文档 我希望匹配具有“核心种植者”的kol_标记.scored.name和kol_标记.scored.scored.scored介于1和100之间的文档,除非它们也具有kol_标记.scored.name“连通性”,其中kol_标记.scored.scored.scored.scored在35到65之间 给定以下映射(为简洁起见,省略非嵌套字段): 我正在执行以

我无法将结果集限制为同时匹配以下
选项的
kol_标记.scored.name
kol_标记.scored.scored.score
范围的文档

我希望匹配具有“核心种植者”的
kol_标记.scored.name
kol_标记.scored.scored.scored
介于1和100之间的文档,除非它们也具有
kol_标记.scored.name
“连通性”,其中
kol_标记.scored.scored.scored.scored
在35到65之间

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

我正在执行以下查询:

{
  "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_标签.scored.name
kol_标签.scored.scored.scored.scored
在1到100之间,以及
kol_标签.scored.name
在任何范围内的“连通性”和
kol_标签.scored.scored
相匹配

我需要的是符合以下条件的文档:

  • kol_标签。得分。核心种植者的姓名和
    kol_标签。得分。得分介于1和100之间
  • kol_标签。得分。名称
    “连通性”和
    kol_标签。得分。得分
    介于35和65之间
  • 排除所有带有
    kol_标记的文档。评分。名称
    “连通性”和
    kol_标记。评分
    小于34且大于66

    • 您的描述有些含糊不清,但我已经尝试制作一个可运行的示例,在这里应该可以使用:(也嵌入在下面)

      以下是我做的几件事:

      • 将过滤器放入一个
        过滤的
        -查询中。仅当您要过滤点击,而不是面时,才应使用顶级
        过滤器(在Elasticsearch 1.0中重命名为
        post_filter

      • 使用
        bool
        而不是
        ,因为过滤器是可缓存的。详情如下:

      • 最重要的是,将嵌套的
        放在
        bool
        中,这样逻辑就正确了。嵌套文档与父文档上应匹配的内容

      • 添加了一个
        不得
        来说明您的最后一点。不确定是否可以有两个名为“Connectivity”
        的子文档,但如果可以的话,应该考虑到这一点。如果您只有一个,您可以删除
        必须\u 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
                                          }
                                      }
                                  }
                              ]
                          }
                      ]
                  }
              }
          }
      }
      '
      
      #!/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
                                          }
                                      }
                                  }
                              ]
                          }
                      ]
                  }
              }
          }
      }
      '