Query/Filter 嵌套对象在某些嵌套对象中出现意外结果

Query/Filter by nested objects getting unexpected results in some nested objects

我在 一些 我无法理解的嵌套对象上有 奇怪的 结果。 我的意思是,在映射中,嵌套对象都是相同的,它们有一个 id 和一个 tree id,都是 long就是这样,但是专业类别 return期望值,其他人做(professionTypeprofessionSubtypeattributeType) .

所以我将索引映射为:

映射:

{
  "attribute-tree": {
    "mappings": {
      "attribute": {
        "dynamic": "strict",
        "properties": {
          "attributeType": {
            "properties": {
              "id": {"type": "long"},
              "tree": {"type": "long"}
            }
          },
          "category": {
            "properties": {
              "id": {"type": "long"},
              "tree": {"type": "long"}
            }
          },
          "family": {"type": "long"},
          "id": {"type": "long"},
          "name": {
            "type": "string",
            "index": "not_analyzed"
          },
          "parentTree": {"type": "long"},
          "profession": {
            "properties": {
              "id": {"type": "long"},
              "tree": {"type": "long"}
            }
          },
          "professionSubtype": {
            "properties": {
              "id": {"type": "long"},
              "tree": {"type": "long"}
            }
          },
          "professionType": {
            "properties": {
              "id": {"type": "long"},
              "tree": {"type": "long"}
            }
          },
          "sorter": {
            "properties": {
              "id": {"type": "long"},
              "name": {
                "type": "string",
                "index": "not_analyzed"
              },
              "tree": {"type": "long"}
            }
          },
          "suggester": {
            "type": "completion",
            "index_analyzer": "edgeNGram_analyzer",
            "search_analyzer": "whitespace_analyzer",
            "payloads": true,
            "preserve_separators": true,
            "preserve_position_increments": true,
            "max_input_length": 50
          },
          "tree": {"type": "long"},
          "type": {"type": "string"}
        }
      },
      "division": {
        // same as "attribute"
      },
      "profession-subtype": {
        // same as "attribute"
      },
      "profession-type": {
        // same as "attribute"
      },
      "profession": {
        // same as "attribute"
      },
      "category": {
        // same as "attribute"
      }
    }
  }
}

如果缺少任何类型的信息,请说

提前致谢

例子

过滤 category.id:

POST /attribute-tree/_search
{
  "size": 2,
  "query": {
    "filtered": {
      "filter": {
       "term": {"category.id": 1}
      }
    }
  }
}

我得到:

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 31,
      "max_score": 1,
      "hits": [
         {
            "_index": "attribute-tree",
            "_type": "category",
            "_id": "4064",
            "_score": 1,
            "_source": {
               "id": 1,
               "tree": 4064,
               "name": "Profession",
               "type": "C",
               "parentTree": 4063,
               "profession": {
                  "id": null,
                  "tree": null
               },
               "professionType": {
                  "id": 1,
                  "tree": 1
               },
               "professionSubtype": {
                  "id": 6,
                  "tree": 4063
               },
               "category": {
                  "id": null,
                  "tree": null
               },
               "attributeType": {
                  "id": null,
                  "tree": null
               },
               "family": [
                  4063,
                  1
               ],
               "suggester": {
                  "input": [
                     "Profession"
                  ],
                  "output": "Profession"
               },
               "sorter": {
                  "name": "Profession",
                  "id": 1,
                  "tree": 4064
               }
            }
         },
         {
            "_index": "attribute-tree",
            "_type": "category",
            "_id": "4083",
            "_score": 1,
            "_source": {
               "id": 1,
               "tree": 4083,
               "name": "Profession",
               "type": "C",
               "parentTree": 4082,
               "profession": {
                  "id": null,
                  "tree": null
               },
               "professionType": {
                  "id": 2,
                  "tree": 4072
               },
               "professionSubtype": {
                  "id": 8,
                  "tree": 4082
               },
               "category": {
                  "id": null,
                  "tree": null
               },
               "attributeType": {
                  "id": null,
                  "tree": null
               },
               "family": [
                  4082,
                  4072
               ],
               "suggester": {
                  "input": [
                     "Profession"
                  ],
                  "output": "Profession"
               },
               "sorter": {
                  "name": "Profession",
                  "id": 1,
                  "tree": 4083
               }
            }
         }
      ]
   }
}

Notice that on this example the category.id doesn't have the expected value (this is also valid for when it is 0 instead of null)

但是我的节点 category.id = 1:

POST /attribute-tree/_search
{
  "size": 2,
  "query": {
    "filtered": {
      "filter": {
       "term": {"tree": 4}
      }
    }
  }
}

项目:

{
   "took": 5,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "attribute-tree",
            "_type": "profession",
            "_id": "4",
            "_score": 1,
            "_source": {
               "id": 1,
               "tree": 4,
               "name": "A&R Administrator",
               "type": "P",
               "parentTree": 3,
               "profession": {
                  "id": null,
                  "tree": null
               },
               "professionType": {
                  "id": 1,
                  "tree": 1
               },
               "professionSubtype": {
                  "id": 1,
                  "tree": 2
               },
               "category": {
                  "id": 1,
                  "tree": 3
               },
               "attributeType": {
                  "id": null,
                  "tree": null
               },
               "family": [
                  3,
                  2,
                  1
               ],
               "suggester": {
                  "input": [
                     "A&R",
                     "Administrator"
                  ],
                  "output": "A&R Administrator"
               },
               "sorter": {
                  "name": "A&R Administrator",
                  "id": 1,
                  "tree": 4
               }
            }
         }
      ]
   }
}

过滤 professionType.id:

POST /attribute-tree/_search
{
  "size": 2,
  "query": {
    "filtered": {
      "filter": {
       "term": {"professionSubtype.id": 1}
      }
    }
  }
}

我得到:

{
   "took": 7,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3890,
      "max_score": 1,
      "hits": [
         {
            "_index": "attribute-tree",
            "_type": "category",
            "_id": "251",
            "_score": 1,
            "_source": {
               "id": 4,
               "tree": 251,
               "name": "Medium",
               "type": "C",
               "parentTree": 2,
               "profession": {
                  "id": null,
                  "tree": null
               },
               "professionType": {
                  "id": 1,
                  "tree": 1
               },
               "professionSubtype": {
                  "id": 1,
                  "tree": 2
               },
               "category": {
                  "id": null,
                  "tree": null
               },
               "attributeType": {
                  "id": null,
                  "tree": null
               },
               "family": [
                  2,
                  1
               ],
               "suggester": {
                  "input": [
                     "Medium"
                  ],
                  "output": "Medium"
               },
               "sorter": {
                  "name": "Medium",
                  "id": 4,
                  "tree": 251
               }
            }
         },
         {
            "_index": "attribute-tree",
            "_type": "profession",
            "_id": "4",
            "_score": 1,
            "_source": {
               "id": 1,
               "tree": 4,
               "name": "A&R Administrator",
               "type": "P",
               "parentTree": 3,
               "profession": {
                  "id": null,
                  "tree": null
               },
               "professionType": {
                  "id": 1,
                  "tree": 1
               },
               "professionSubtype": {
                  "id": 1,
                  "tree": 2
               },
               "category": {
                  "id": 1,
                  "tree": 3
               },
               "attributeType": {
                  "id": null,
                  "tree": null
               },
               "family": [
                  3,
                  2,
                  1
               ],
               "suggester": {
                  "input": [
                     "A&R",
                     "Administrator"
                  ],
                  "output": "A&R Administrator"
               },
               "sorter": {
                  "name": "A&R Administrator",
                  "id": 1,
                  "tree": 4
               }
            }
         }
      ]
   }
}

也测试为:

POST /attribute-tree/_search
{
  "query":{
    "filtered": {
      "query": {"match_all":{}},
      "filter": {
        "nested": {
          "path": "category",
          "filter": {
            "bool": {
              "must": [
                {"term": {"category.id": 1}}
              ]
            }
          }
        }
      }
    }
  }
}

但这给出了一个错误

org.elasticsearch.index.query.QueryParsingException: [attribute-tree] [nested] nested object under path [category] is not of nested type

从映射来看,索引似乎具有 category 类型以及 attribute 类型的字段,名为 category.

要允许正确的字段解析并消除类型 category 中的字段 id 与类型 attribute 中的字段 category.id 之间的歧义,您需要指定整个路径该字段包括类型,即 <type>.<fieldname>.

示例:

POST /attribute-tree/_search
{
  "size": 2,
  "query": {
    "filtered": {
      "filter": {
       "term": {"attribute.category.id": 1}
      }
    }
  }
}

issue thread 对此有更多讨论。