ElasticSearch高亮不高亮 [英] ElasticSearch Highlighting Not highlighting

查看:143
本文介绍了ElasticSearch高亮不高亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解如何突出显示工作.我的查询正在返回该项目,但我看不到会导致突出显示的标签.

I'm having difficulty understanding how to get highlighting to work. My queries are returning the item, but I do not see the tags that would cause the highlight.

这是测试索引的设置:

curl -XPUT 'http://localhost:9200/testfoo' -d '{
    "mappings": {
        "entry": {
            "properties": {
                "id": { "type": "integer" },
                "owner": { "type": "string" },
                "target": {
                    "properties": {
                        "id": { "type": "integer" },
                        "type": {
                            "type": "string",
                            "index": "not_analyzed"
                        }
                    }
                },
                "body": { "type": "string" },
                "body_plain": { "type": "string"}
            }
        }
    }
}'

这里有几个插入的文档:

Here's a couple of inserted documents:

curl -XPUT 'http://localhost:9200/testfoo/entry/1' -d'{
    "id": 1,
    "owner": "me",
    "target": {
        "type": "event",
        "id":   100
    },
    "body": "<div>Message One has foobar in it</div>",
    "body_plain": "Message One has foobar in it"
}'

curl -XPUT 'http://localhost:9200/testfoo/entry/2' -d'{
    "id": 2,
    "owner": "me",
    "target": {
        "type": "event",
        "id":   200
    },
    "body": "<div>Message One has no bar in it</div>",
    "body_plain": "Message One has no bar in it"
}'

简单搜索将返回预期的文档:

A Simple search returns the expected document:

curl -XPOST 'http://localhost:9200/testfoo/_search?pretty' -d '{
    "query": {
        "simple_query_string": {
            "query": "foobar"
        }
    }
}'
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.09492774,
    "hits" : [ {
      "_index" : "testfoo",
      "_type" : "entry",
      "_id" : "1",
      "_score" : 0.09492774,
      "_source" : {
        "id" : 1,
        "owner" : "me",
        "target" : {
          "type" : "event",
          "id" : 100
        },
        "body" : "<div>Message One has foobar in it</div>",
        "body_plain" : "Message One has foobar in it"
      }
    } ]
  }
}

但是,当我添加突出显示"时,我得到了相同的JSON,但是body_plain却没有与匹配项突出显示":

However, when I add "highlighting" I get the same JSON but body_plain is not "highlighted" with the matching term:

curl -XPOST 'http://localhost:9200/testfoo/_search?pretty' -d '{
    "query":{
            "query": {
                "simple_query_string":{
                    "query":"foobar"
                }
            }
    },
    "highlight": {
        "pre_tags": [ "<div class=\"highlight\">" ],
        "post_tags": [ "</div>" ],
        "fields": {
            "_all": {
                "fragment_size": 10,
                "number_of_fragments": 1
            }
        }
    },
    "sort": [
        "_score"
    ],
    "_source": [ "target", "id", "body_plain", "body" ],
    "min_score": 0.9,
    "size":10
}'
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "testfoo",
      "_type" : "entry",
      "_id" : "1",
      "_score" : 1.0,
      "_source" : {
        "id" : 1,
        "body" : "<div>Message One has foobar in it</div>",
        "target" : {
          "id" : 100,
          "type" : "event"
        },
        "body_plain" : "Message One has foobar in it"
      }
    } ]
  }
}

我期望body_plain看起来像

Where I was expecting body_plain to look like

  Message One has <div class="highlight">foobar</div> in it

想知道我在做什么错.谢谢.

Wondering what I'm doing wrong. Thanks.

推荐答案

来自

为了执行突出显示,该字段的实际内容为必需的.如果有问题的字段已存储(在以下位置将store设置为true:映射),否则将使用实际的_source加载,并将从中提取相关字段.

In order to perform highlighting, the actual content of the field is required. If the field in question is stored (has store set to true in the mapping) it will be used, otherwise, the actual _source will be loaded and the relevant field will be extracted from it.

无法从_source提取_all字段,因此只能用于突出显示是否已将其映射为将store设置为true.

The _all field cannot be extracted from _source, so it can only be used for highlighting if it mapped to have store set to true.

您有两种方法可以解决此问题.您可以更改映射以存储 _all 字段:

You have two ways to solve this. Either you change your mapping to store the _all field:

{
  "mappings": {
    "entry": {
      "_all": {              <-- add this
        "store": true
      },
      "properties": {
        ...

或者您将查询更改为此:

Or you change your query to this:

curl -XPOST 'http://localhost:9200/testfoo/_search?pretty' -d '{
    "query":{
            "query": {
                "simple_query_string":{
                    "query":"foobar"
                }
            }
    },
    "highlight": {
        "pre_tags": [ "<div class=\"highlight\">" ],
        "post_tags": [ "</div>" ],
        "require_field_match": false,             <-- add this
        "fields": {
            "*": {                                <-- use this                  
                "fragment_size": 10,
                "number_of_fragments": 1
            }
        }
    },
    "sort": [
        "_score"
    ],
    "_source": [ "target", "id", "body_plain", "body" ],
    "min_score": 0.9,
    "size":10
}'

这篇关于ElasticSearch高亮不高亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆