gpt4 book ai didi

search - ElasticSearch Has_Child不返回结果

转载 作者:行者123 更新时间:2023-12-03 01:03:38 25 4
gpt4 key购买 nike

我是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创建了 parent
{"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"}

我创造了这些 child
{"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更改为不是数组,而只是其中包含值“animals”的纯字符串,则查询将返回正确的结果。
任何想法将不胜感激。
{
"query": {
"has_child": {
"type": "child",
"query": {
"bool": {
"must": [
{
"term": {
"cons": "animals"
}
}
]
}
}
}
}
}

最佳答案

由于您是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 查询进行多个匹配。该查询将为您提供在数组中包含“动物”或“治疗”的 parent 。
GET parent_index/parent/_search
{
"query": {
"has_child": {
"type": "child",
"query": {
"bool": {
"must": [
{
"terms": {
"cons": [
"animals",
"treat"
]
}
}
]
}
}
}
}
}

ES提供了多种方法,可根据您的需要将数据 query

关于search - ElasticSearch Has_Child不返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33725028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com