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

问题描述 投票:1回答:1

我是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”字段,因为它对我们的要求很重要。此外,无法更改字段的数据类型。

非常感谢

elasticsearch elasticsearch-5 elasticsearch-highlight
1个回答
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}
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.