Search code examples
elasticsearchsearch-engine

How to make flattened sub-field in the nested field in elastic search?


Here, I have a indexed document like:

doc = {  
  "id": 1,  
  "content": [  
    {  
      "txt": I,  
      "time": 0,  
    },  
    {  
      "txt": have,  
      "time": 1,  
    },  
    {  
      "txt": a book,  
      "time": 2,  
    },  
    {  
      "txt": do not match this block,  
      "time": 3,  
    },  
  ]  
}  

And I want to match "I have a book", and return the matched time: 0,1,2. Is there anyone who knows how to build the index and the query for this situation? I think the "content.txt" should be flattened but "content.time" should be nested?


Solution

  • want to match "I have a book", and return the matched time: 0,1,2.

    Adding a working example with index mapping,search query, and search result

    Index Mapping:

    {
      "mappings": {
        "properties": {
          "content": {
            "type": "nested"
          }
        }
      }
    }
    

    Search Query:

    {
      "query": {
        "nested": {
          "path": "content",
          "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "content.txt": "I have a book"
                  }
                }
              ]
            }
          },
          "inner_hits": {}
        }
      }
    }
    

    Search Result:

    "inner_hits": {
              "content": {
                "hits": {
                  "total": {
                    "value": 3,
                    "relation": "eq"
                  },
                  "max_score": 2.5226097,
                  "hits": [
                    {
                      "_index": "64752029",
                      "_type": "_doc",
                      "_id": "1",
                      "_nested": {
                        "field": "content",
                        "offset": 2
                      },
                      "_score": 2.5226097,
                      "_source": {
                        "txt": "a book",
                        "time": 2
                      }
                    },
                    {
                      "_index": "64752029",
                      "_type": "_doc",
                      "_id": "1",
                      "_nested": {
                        "field": "content",
                        "offset": 0
                      },
                      "_score": 1.5580825,
                      "_source": {
                        "txt": "I",
                        "time": 0
                      }
                    },
                    {
                      "_index": "64752029",
                      "_type": "_doc",
                      "_id": "1",
                      "_nested": {
                        "field": "content",
                        "offset": 1
                      },
                      "_score": 1.5580825,
                      "_source": {
                        "txt": "have",
                        "time": 1
                      }
                    }
                  ]
                }
              }
            }
          }