elasticsearch - 通过嵌套对象查询/筛选在某些嵌套对象中获得意外结果

标签 elasticsearch nested filtering

我对一些我无法理解的嵌套对象产生了奇怪的结果。
我的意思是,在映射嵌套对象时,它们都是相同的,它们也有一个id和一个树id,它们都是long,就是这样,但是professional和category不会返回期望值,而其他的则返回(professionType,professionalSubtype和attributeType)。

所以我将索引映射为:

对应:

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

按行业类型.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

为了实现适当的字段解析并消除id类型的category字段与category.id类型的attribute字段之间的歧义,您需要指定字段的完整路径,包括<type>.<fieldname>类型。

例:

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

对此issue thread进行了更多讨论。

关于elasticsearch - 通过嵌套对象查询/筛选在某些嵌套对象中获得意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32588187/

相关文章:

php - FILTER_VALIDATE_EMAIL 的一致性如何?

elasticsearch - 如何区分几个应该从句的命中

elasticsearch - Elasticsearch-每次点击过滤并获取匹配结果

elasticsearch - 发布 https://apm.<acme>.com/intake/v2/rum/events net::ERR_BLOCKED_BY_CLIENT

c# - 选择具有特定数量的嵌套文档项目的文档

javascript - 使用 lodash (或 vanilla javascript)对对象数组进行排序和过滤

python - ElasticSearch 在 python 中请求范围时间戳

html - 嵌套列表中的反重置问题

python - 使用嵌套列表计算足球队获胜的次数

python - 如何对数据帧的行进行求和,以便它仅根据月、日或年对值进行求和。然后将所有结果形成报告