This is the mapping:
{
myindex: {
mappings: {
properties: {
identifier: {
type: "keyword"
},
parts: {
type: "nested",
properties: {
_id: {
type: "text",
fields: {
keyword: {
type: "keyword",
ignore_above: 256
}
}
},
user_id: {
type: "text",
fields: {
keyword: {
type: "keyword",
ignore_above: 256
}
}
}
}
}
}
}
}
}
The index contains this entry:
_source: {
identifier: "6002af6aa8672d4f8d06be3b6002c4db0fa23460882e82ab",
parts: [
{
_id: "6002c4dc0fa23460882e82af",
user_id: "6002c4db0fa23460882e82ab",
},
{
_id: "6002c4dc0fa23460882e82b0",
user_id: "6002af6aa8672d4f8d06be3b",
}
]
}
When searching for a top level field, then the entry above is returned:
curl -X GET "localhost:9200/myindex/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{
"term": {
"identifier": "6002af6aa8672d4f8d06be3b6002c4db0fa23460882e82ab"
}
}
]
}
}
}'
However, when I run this query (for searching inside the "parts" array), I don't get any result (i.e. an empty hits):
curl -X GET "localhost:9200/myindex/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{
"term": {
"parts.user_id": "6002c4db0fa23460882e82ab"
}
}
]
}
}
}'
Why? I read about nested elements. What I understood, mapping the field named "parts" as nested is the right way to search inside arrays.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…