嵌套对象的查询/筛选在一些嵌套对象

时间:2015-09-15 14:04:56

标签: elasticsearch nested filtering

我在某些嵌套对象上有奇怪的结果,我无法理解。 我的意思是,在映射中嵌套对象都是一样的,它们还有一个 id 和一个 tree id,两者都是long就是这样,但是< em>专业和类别执行返回预期值,其他人( professionType professionSubtype 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
               }
            }
         }
      ]
   }
}
  

请注意,在此示例中,category.id没有预期值(这在 0 而不是 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

1 个答案:

答案 0 :(得分:1)

从映射看,索引的类型为category,而attribute类型的字段名为category

要在id类型的字段category与类型category.id中的字段attribute之间进行正确的字段解析和消除歧义,您需要指定字段的完整路径包括类型ie <type>.<fieldname>

示例:

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

issue thread对此有更多的讨论。