简体   繁体   English

在 Elasticsearch 中突出显示嵌套对象

[英]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.我正在使用以下查询在parent.name属性中查找匹配项,并且可以获得高亮显示。

{
  "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?有没有办法获得child.name属性中匹配项的内联突出显示,以便很容易找到匹配的相应数组的哪个元素?

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.例如,对于给定的样本数据,如果我按“Doe”搜索,我预计会得到 6 次点击,而如果我按“Jane”搜索,我只会得到 4 次。

You can simply add another nested query clause inside you top level should .您可以简单地在顶级should添加另一个nested query子句。

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"
      }
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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