我对一些我无法理解的嵌套对象产生了奇怪的结果。
我的意思是,在映射嵌套对象时,它们都是相同的,它们也有一个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/