ElasticSearch Has_Child 没有 return 结果
ElasticSearch Has_Child doesn't return the results
我对 ElasticSearch 还很陌生,正在尝试 parent-child 关系。
我的映射是这样的
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"}
我创造了这些 children
{"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"}
现在当我在 parent 中搜索 has_child
时;我什么也得不到。
但是,如果我将 cons 更改为不是数组,而只是其中包含值 "animals" 的纯字符串,则查询 returns 会得到正确的结果。
任何想法将不胜感激。
{
"query": {
"has_child": {
"type": "child",
"query": {
"bool": {
"must": [
{
"term": {
"cons": "animals"
}
}
]
}
}
}
}
}
由于您是 elasticsearch 的新手,因此您需要了解如何 ES analyzes 您提供的文本。
由于您使用的是 term
查询,ES 不会分析文本,而是会寻找 精确 匹配。
如果您想使用 term
查询,您需要更改 child
mapping
像这样
"properties": {
"cons": {
"type": "string",
"index" : "not_analyzed"
},
"ccnid": {
"type": "string"
},
"nid": {
"type": "string"
}
}
如果我没记错的话,您使用的是默认值 standard analyzer
,因为您没有指定任何分析器。 index : not_analyzed
将按原样为术语编制索引。
之后,下面的查询将为您提供预期的结果。您需要使用 terms
查询进行多重匹配。该查询将为您提供 parents,其中数组内有 "animals" 或 "treat"。
GET parent_index/parent/_search
{
"query": {
"has_child": {
"type": "child",
"query": {
"bool": {
"must": [
{
"terms": {
"cons": [
"animals",
"treat"
]
}
}
]
}
}
}
}
}
ES 根据您的需要
提供了很多 query 您的数据的方法
我对 ElasticSearch 还很陌生,正在尝试 parent-child 关系。
我的映射是这样的
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"}
我创造了这些 children
{"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"}
现在当我在 parent 中搜索 has_child
时;我什么也得不到。
但是,如果我将 cons 更改为不是数组,而只是其中包含值 "animals" 的纯字符串,则查询 returns 会得到正确的结果。
任何想法将不胜感激。
{
"query": {
"has_child": {
"type": "child",
"query": {
"bool": {
"must": [
{
"term": {
"cons": "animals"
}
}
]
}
}
}
}
}
由于您是 elasticsearch 的新手,因此您需要了解如何 ES analyzes 您提供的文本。
由于您使用的是 term
查询,ES 不会分析文本,而是会寻找 精确 匹配。
如果您想使用 term
查询,您需要更改 child
mapping
像这样
"properties": {
"cons": {
"type": "string",
"index" : "not_analyzed"
},
"ccnid": {
"type": "string"
},
"nid": {
"type": "string"
}
}
如果我没记错的话,您使用的是默认值 standard analyzer
,因为您没有指定任何分析器。 index : not_analyzed
将按原样为术语编制索引。
之后,下面的查询将为您提供预期的结果。您需要使用 terms
查询进行多重匹配。该查询将为您提供 parents,其中数组内有 "animals" 或 "treat"。
GET parent_index/parent/_search
{
"query": {
"has_child": {
"type": "child",
"query": {
"bool": {
"must": [
{
"terms": {
"cons": [
"animals",
"treat"
]
}
}
]
}
}
}
}
}
ES 根据您的需要
提供了很多 query 您的数据的方法