我对ElasticSearch很新,并尝试了亲子关系。
我的映射看起来像这样
mappings: {
parent: {
properties: {
sts: {
type: long
}
md: {
type: string
}
dty: {
type: string
}
cid: {
type: string
}
}
}
child: {
_routing: {
required: true
}
_parent: {
type: parent
}
properties: {
cons: {
type: string
}
ccnid: {
type: string
}
nid: {
type: string
}
}
}
}
我使用批量API
创建了父级{"index":{"_id":"cusId:NodeId1"," _routing":"cusId"}}
{"cid":"cusId","sts":0,"nm":"NodeId1_t"}
{"index":{"_id":"cusId:NodeId2"," _routing":"cusId"}}
{"cid":"cusId","sts":0,"nm":"NodeId2_t"}
我创造了这些孩子
{"index":{"_id":"c4","_routing":"cusId","parent":"cusId:NodeId1"}}
{"cons":["animals","treat","cat","feet"],"ccnid":"cusId","nid":"NodeId1"}
{"index":{"_id":"c5","_routing":"cusId","parent":"cusId:NodeId2"}}
{"cons":["cats","animals","something"],"ccnid":"cusId","nid":"NodeId2"}
现在我在父母中搜索has_child
;我一无所获。
但是,如果我将cons更改为不是数组而只是一个值为&#34的纯字符串;动物"在其中,查询返回正确的结果。
任何想法将不胜感激。
{
"query": {
"has_child": {
"type": "child",
"query": {
"bool": {
"must": [
{
"term": {
"cons": "animals"
}
}
]
}
}
}
}
}
答案 0 :(得分:2)
由于您不熟悉elasticsearch,因此您需要了解ES analyzes您提供的文字的方式。
由于您使用的是term
查询,因此ES不会分析文本,而是会查找完全匹配。
如果您想使用child
查询,则需要更改mapping
term
像这样
"properties": {
"cons": {
"type": "string",
"index" : "not_analyzed"
},
"ccnid": {
"type": "string"
},
"nid": {
"type": "string"
}
}
如果我没有错,您使用的是默认standard analyzer
,因为您没有指定任何分析器。 index : not_analyzed
将按原样对该术语编制索引。
之后,下面的查询将为您提供预期的结果。您需要使用terms
查询进行多次匹配。该查询将为您提供在数组中具有“animals”或“treat”的父母。
GET parent_index/parent/_search
{
"query": {
"has_child": {
"type": "child",
"query": {
"bool": {
"must": [
{
"terms": {
"cons": [
"animals",
"treat"
]
}
}
]
}
}
}
}
}
ES根据您的需要为query数据提供了很多方法