【问题标题】:Highlighting a numeric field from results fetched from custom “_all” fields突出显示从自定义“_all”字段获取的结果中的数字字段
【发布时间】:2019-07-25 17:59:05
【问题描述】:

我是 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】:

    我已经测试了您的查询,在我看来,它突出显示了原始字段,在本例中为 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}
        }
      }
    }
    

    【讨论】:

    • 嗨@whatapalaver,我可以知道您使用的是哪个版本的elasticsearch。我得到以下结果 '{ "took": 14, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, " hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "myindex", "_type": "mytype", "_id": "1", "_score": 1,“_source”:{“first_name”:“John”,“mobile_number”:9988776655}}]}}'
    • 我使用的是 elasticsearch 6.6,所以这可能是造成差异的原因。版本 5 文档针对每个突出显示字段显示了 require_field_match: false 。我将编辑我的答案并提出尝试的建议。 elastic.co/guide/en/elasticsearch/reference/5.0/…
    猜你喜欢
    • 2017-12-22
    • 1970-01-01
    • 2012-12-01
    • 2017-05-22
    • 1970-01-01
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    相关资源
    最近更新 更多