【问题标题】:Highlight nested object in Elasticsearch在 Elasticsearch 中突出显示嵌套对象
【发布时间】:2019-05-18 13:01:30
【问题描述】:

这是我的示例数据集,

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

我的定义:

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

我正在使用以下查询来查找 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"
      }
    }
  ]
}

有没有办法在 child.name 属性中获得匹配的内联高亮显示,以便轻松找到对应数组的哪个元素被匹配?

例如,对于给定的样本数据,如果我按“Doe”搜索,我预计会得到 6 个命中,而如果按“Jane”搜索,我只会得到 4 个。

【问题讨论】:

  • 你想要匹配 parent.name AND child.name 或者你想要做 ORing。任何一个都应该匹配?
  • 是的,任何一个都应该匹配。例如,如果我按“Doe”搜索,我预计会得到 6 个命中,而如果我按“Jane”搜索,我只会得到 4 个。

标签: elasticsearch


【解决方案1】:

您可以在顶层should 中简单地添加另一个nested query 子句。

您的查询应如下所示:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-02
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 2015-01-13
    • 2017-11-03
    • 2014-06-12
    相关资源
    最近更新 更多