从自定义“ _all”字段获取的结果中突出显示一个数字字段

时间:2019-03-04 12:09:55

标签: elasticsearch elasticsearch-5 elasticsearch-highlight

我是Elasticsearch的新手。我们需要索引和检索具有不同数据类型的某些数据。我们正在使用自定义“ _all”字段,如以下链接所述

Custom "_all" fields

以下是我们的代码

用于创建索引

PUT myindex
{
  "mappings": {
    "mytype": {
      "properties": {
        "first_name": {
          "type":    "text",
          "copy_to": "contact_details" 
        },
        "mobile_number": {
          "type":    "long",
          "copy_to": "contact_details" 
        },
        "contact_details": {
          "type":    "text"
        }
      }
    }
  }
}

用于添加索引

PUT myindex/mytype/1
{
  "first_name": "John",
  "mobile_number": 9988776655
}

搜索

GET myindex/_search
{
  "query": {
    "multi_match": {
      "query": "9988776655",
      "fields": [
        "contact_details"
      ],
      "fuzziness": "auto"
    }
  },
  "highlight": {
    "require_field_match": false,
    "pre_tags": [
      "<tag1>"
    ],
    "post_tags": [
      "</tag1>"
    ],
    "fields": {
      "first_name": {},
      "mobile_number": {}
    }
  }
}

使用以上查询,我们可以获取结果,但不能突出显示原始字段值,如以下链接中所述

Highlighting Original Fields

需要知道我们做错了什么还是有错误

请注意,我们必须使用自定义“ _all”字段,因为这对我们的要求很重要。另外,字段的数据类型不能更改。

非常感谢

1 个答案:

答案 0 :(得分:0)

我已经测试了您的查询,在我看来突出显示了原始字段,在这种情况下,mobile_number周围带有标签字段。

这是我得到的结果:

    {
  "took" : 93,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "my_so_index",
        "_type" : "mytype",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "first_name" : "John",
          "mobile_number" : 9988776655
        },
        "highlight" : {
          "mobile_number" : [
            "<tag1>9988776655</tag1>"
          ]
        }
      }
    ]
  }
}

我在ElasticSearch 6.6版本上运行上述查询。对于版本5,文档显示的结构略有不同,您可以尝试以下方法吗:

GET myindex/_search
{
  "query": {
    "multi_match": {
      "query": "9988776655",
      "fields": [
        "contact_details"
      ],
      "fuzziness": "auto"
    }
  },
  "highlight": {
    "pre_tags": [
      "<tag1>"
    ],
    "post_tags": [
      "</tag1>"
    ],
    "fields": {
      "first_name": {"require_field_match": false},
      "mobile_number": {"require_field_match": false}
    }
  }
}
相关问题