简体   繁体   中英

Highlight nested object in Elasticsearch

Here is my sample dataset,

{
   "parent":[
      {
         "name":"John Doe 1",
         "age":"100 year",
         "sex":"male",
         "child":[
            {
               "name":"Jane Doe 1",
               "height":100.00,
               "width":100.00
            },
            {
               "name":"Jane Doe 2",
               "height":100.00,
               "width":100.00
            }
         ]
      },
      {
         "name":"John Doe 2",
         "age":"100 year",
         "sex":"male",
         "child":[
            {
               "name":"Jane Doe 3",
               "height":100.00,
               "width":100.00
            },
            {
               "name":"Jane Doe 4",
               "height":100.00,
               "width":100.00
            }
         ]
      }
   ]
}

And my definition:

{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "default": {
            "type": "simple"
          }
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "parent": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "keyword"
            },
            "age": {
              "type": "text"
            },
            "sex": {
              "type": "text"
            },
            "child": {
              "type": "nested",
              "properties": {
                "name": {
                  "type": "text"
                },
                "height": {
                  "type": "float"
                },
                "width": {
                  "type": "float"
                }
              }
            }
          }
        }
      }
    }
  }
}

I'm using the following query to look for matches in the parent.name property and can get highlights.

{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "inner_hits": {
              "highlight": {
                "fields": {
                  "parent.name": {}
                },
                "number_of_fragments": 0,
                "pre_tags": [
                  "<span>"
                ],
                "post_tags": [
                  "</span>"
                ]
              }
            },
            "path": "parent",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "parent.name": {
                        "query": "John",
                        "fuzziness": "AUTO:3,6",
                        "prefix_length": "0"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ],
    }
  },
  "_source": ["parent"],
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "createdOn": {
        "order": "desc"
      }
    }
  ]
}

Is there a way to get inline highlights for the matches in the child.name properties also so that it would be easy to find exactly which element of that corresponding array got matched?

For example, for the given sample data, if I search by "Doe", I'm expecting to get 6 hits, whereas if I search by "Jane", I would get only 4.

You can simply add another nested query clause inside you top level should .

Here's how your query should look:

{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "inner_hits": {
              "highlight": {
                "fields": {
                  "parent.name": {}
                },
                "number_of_fragments": 0,
                "pre_tags": [
                  "<span>"
                ],
                "post_tags": [
                  "</span>"
                ]
              }
            },
            "path": "parent",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "parent.name": {
                        "query": "John Doe 1"
                      }
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "inner_hits": {
              "highlight": {
                "fields": {
                  "parent.child.name": {}
                },
                "number_of_fragments": 0,
                "pre_tags": [
                  "<span>"
                ],
                "post_tags": [
                  "</span>"
                ]
              }
            },
            "path": "parent.child",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "parent.child.name": {
                        "query": "Jane Doe 1"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "_source": ["parent"],
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "createdOn": {
        "order": "desc"
      }
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM