I'm new to elasticsearch and struggling this situation (for example):
I want to index offices and clerks of each office. So, I've created this mapping:
PUT office
{
"mappings": {
"properties": {
"office_name": {
"type": "text"
},
"clerks": {
"type": "nested"
}
}
}
}
and indexing some data:
PUT /office/_doc/1
{
"name": "office a",
"clerks": [
{
"name": "a a a a"
},
{
"name": "a a"
},
{
"name": "b"
}
]
}
PUT /office/_doc/2
{
"name": "office b",
"clerks": [
{
"name": "a a a"
}
]
}
Now, I want to search for 'a':
GET /office/_search
{
"query": {
"nested": {
"path": "clerks",
"query": {
"match": {
"clerks.name": "a"
}
},
"inner_hits": {}
}
}
}
the result is:
{
"took" : 624,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.1272885,
"hits" : [
{
"_index" : "office",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.1272885,
"_source" : {
"name" : "office b",
"clerks" : [
{
"name" : "a a a"
}
]
},
"inner_hits" : {
"clerks" : {
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.1272885,
"hits" : [
{
"_index" : "office",
"_type" : "_doc",
"_id" : "2",
"_nested" : {
"field" : "clerks",
"offset" : 0
},
"_score" : 1.1272885,
"_source" : {
"name" : "a a a"
}
}
]
}
}
}
},
{
"_index" : "office",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.1184678,
"_source" : {
"name" : "office a",
"clerks" : [
{
"name" : "a a a a"
},
{
"name" : "a a"
},
{
"name" : "b"
}
]
},
"inner_hits" : {
"clerks" : {
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.1468691,
"hits" : [
{
"_index" : "office",
"_type" : "_doc",
"_id" : "1",
"_nested" : {
"field" : "clerks",
"offset" : 0
},
"_score" : 1.1468691,
"_source" : {
"name" : "a a a a"
}
},
{
"_index" : "office",
"_type" : "_doc",
"_id" : "1",
"_nested" : {
"field" : "clerks",
"offset" : 1
},
"_score" : 1.0900666,
"_source" : {
"name" : "a a"
}
}
]
}
}
}
}
]
}
}
I expect 'a a a' appears before 'a a' and the result to be like this:
[
{
"name": "a a a a"
},
{
"name": "a a a"
},
{
"name": "a a"
}
]
How can I achieve this result?