I have a set of documents that contain nested paragraphs and sentences, I need the order of the inner hits to be in the order they occur in the document, eg. paragraph 0 sentence 0, paragraph 0 sentence 5, paragraph 2 sentence 0. Currently they return in the reverse order, but I have no sort order applied.
I understand the offset
property in the response is not part of my data, but is it possible to sort the inner_hits results in the manner I would like using the offset
property? Or would it require adding numeric fields for the paragraph and sentence position values and sorting off of those new fields?
Still using ES 5.3, so sorry about older syntax.
Mapping
PUT /myindex
{
"mappings": {
"doc": {
"paragraphs": {
"type": "nested",
"properties": {
"prop1": {
"type": "float"
},
"sentences": {
"type": "nested",
"properties": {
"value": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
}
Document
PUT /myindex/doc/1
{
"title": {
"value": "I am a test document"
},
"paragraphs": [
{
"sentences": [
{
"value": "I am the first sentence."
},
{
"value": "I am the second sentence."
},
{
"value": "This is some more test data"
}
]
}
]
}
Query
POST /myindex/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"query": {
"bool": {
"must": {
"prefix": {
"paragraphs.sentences.value": "I am"
}
}
}
},
"path": "paragraphs.sentences",
"inner_hits": {
"from": 0,
"size": 10,
"sort": {
}
}
}
}
]
}
}
}