突出显示弹性搜索中的嵌套对象

时间:2018-12-18 06:24:57

标签: elasticsearch

这是我的样本数据集,

{
   "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次点击。

1 个答案:

答案 0 :(得分:0)

您可以简单地在顶层var select = document.getElementById("selectMessageView"), requestHistoryForm = document.RequestHistoryForm; fnOnChangeType = ()=> select.disabled = requestHistoryForm.strMessageType.value != "All"; fnOnChangeView = ()=> select.disabled = requestHistoryForm.strMessageView.value != ""; 内添加另一个nested query子句。

查询内容如下:

should