我有以下映射:
curl -XPUT 'http://localhost:9200/bookstore/user/_mapping' -d '
{
"user": {
"properties": {
"user_id": { "type": "integer" },
"gender": { "type": "string", "index" : "not_analyzed" },
"age": { "type": "integer" },
"age_bracket": { "type": "string", "index" : "not_analyzed" },
"current_city": { "type": "string", "index" : "not_analyzed" },
"relationship_status": { "type": "string", "index" : "not_analyzed" },
"books" : {
"type": "nested",
"properties" : {
"b_oid": { "type": "string", "index" : "not_analyzed" },
"b_name": { "type": "string", "index" : "not_analyzed" },
"bc_id": { "type": "integer" },
"bc_name": { "type": "string", "index" : "not_analyzed" },
"bcl_name": { "type": "string", "index" : "not_analyzed" },
"b_id": { "type": "integer" }
}
}
}
}
}'
现在,我尝试查询具有“性别”的用户的例子:“男性”,购买了某个类别的书“bcl_name”:“Trivia”并显示“b_name”书名。我莫名其妙地无法让它运行。
我有查询
curl -XGET 'http://localhost:9200/bookstore/user/_search?pretty=1' -d '{
"size": 0,
"from": 0,
"query": {
"filtered": {
"query": {
"terms": {
"gender": [
"Male"
]
}
}
}
},
"facets": {
"CategoryFacet": {
"terms": {
"field": "books.b_name",
"size": 5,
"shard_size": 1000,
"order": "count"
},
"nested": "books",
"facet_filter": {
"terms": {
"books.bcl_name": [
"Trivia"
]
}
}
}
}
}'
返回结果,但我不确定这是否正确。我找了一些例子,并找到了这个(http://www.spacevatican.org/2012/6/3/fun-with-elasticsearch-s-children-and-nested-documents/)。我能够像这样重写我的查询:
curl -XGET 'http://localhost:9200/bookstore/user/_search?pretty=1' -d '{
"size": 0,
"from": 0,
"query": {
"filtered": {
"query": {
"terms": {
"gender": [
"Male"
]
}
},
"filter": {
"nested": {
"path": "books",
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": [
{
"term": {
"books.bcl_name": "Trivia"
}
}
]
}
}
}
}
}
}
},
"facets": {
"CategoryFacet": {
"terms": {
"field": "books.b_name",
"size": 5,
"shard_size": 1000,
"order": "count"
},
"nested": "books"
}
}
}'
显示不同的结果。
作为初学者,我现在已经迷失了。有人可以给我提示如何解决这个问题吗?非常感谢提前!
答案 0 :(得分:1)
首先查询意味着:
gender : "Male"
gender : "Male" AND
books.bcl_name : "Trivia"
因此,在结果集中,您将获得所有“男性”用户,但您的CategoryFacet会为您提供“男性用户及其books.bcl_name为Trivia”的计数。
在第二个查询中,“CategoryFacet”不包含额外的过滤。它只返回确切结果集的构面。