一尘不染

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

elasticsearch

我无法将结果集限制为同时满足以下两个选项的范围kol_tags.scored.namekol_tags.scored.score范围的文档or

我想匹配具有kol_tags.scored.name“ Core
Grower”且kol_tags.scored.score介于1到100之间的文档,除非它们也具有kol_tags.scored.name
Connectivity” kol_tags.scored.score不在 35到65之间)。

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

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
                  }
                }
              }
            ]
          }
        ]
      }
    }
  }
}

通过上面的查询,我得到了与kol_tags.scored.name“ Core Grower”
匹配且kol_tags.scored.score在1到100之间且还具有“ Connectivity”且在任意范围内的 ALSO的
文档。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之间
  • 排除任何具有kol_tags.scored.name“连通性”且kol_tags.scored.score小于34且大于66的文档

阅读 306

收藏
2020-06-22

共1个答案

一尘不染

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

这是我做的几件事:

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

  • 使用bool代替andor,因为过滤器是可缓存的。此处更多信息:http : //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
                                    }
                                }
                            }
                        ]
                    }
                ]
            }
        }
    }
}
'
2020-06-22