elasticsearch hightlight查询所有字段

时间:2017-10-05 09:57:04

标签: elasticsearch

我想突出显示所有可搜索字段的搜索文本。

我使用this doc求助。 突出显示不起作用,查询得到正常结果没有任何突出显示。 查询没有得到突出显示,我的查询有什么问题?

我的弹性版本“5.5.2”

_索引的映射: -

{
  "products": {
    "mappings": {
      "product": {
        "_all": {
          "enabled": true
        },
        "properties": {
          "description": {
            "type": "text"
          },
          "price": {
            "type": "long"
          },
          "title": {
            "type": "text",
            "boost": 2
          }
        }
      }
    }
  }
}

我的弹性搜索查询: -

curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match": { "_all": "rock clamb" }
    },
    "highlight" : {
        "pre_tags" : ["<tag1>"],
        "post_tags" : ["</tag1>"],
        "fields" : {
          "_all" : {}
        }
    }
}'

输出

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 6,
    "successful" : 6,
    "failed" : 0
  },
"hits" : {
    "total" : 2,
    "max_score" : 0.99835247,
    "hits" : [
      {
        "_index" : "products",
        "_type" : "product",
        "_id" : "2",
        "_score" : 0.99835247,
        "_source" : {
          "title" : "Push Clamb Car Holder for Samsung and iPhone",
          "price" : 3,
          "description" : "A high quality car holder for smartphones that features a push clamp function Features: Universal car holder Height adjustment pivot Ball shape 360"
        }
      },
      {
        "_index" : "products",
        "_type" : "product",
        "_id" : "5",
        "_score" : 0.7741236,
        "_source" : {
          "price" : 1,
          "description" : "This item fits directly to all 3 power boxes (7850 7880 7890)Together in use these clamps are bi-polarEach individual clamb is uni-polar cable include",
          "title" : "Rimba Luxurous Electrosex clamps Uni-polar (2 pcs) - Naughty"
        }
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:0)

为了执行突出显示,需要该字段的实际内容。如果存储了相关字段(在映射中将store设置为true),则将使用它,否则,将加载实际的_source并从中提取相关字段。

无法从_source中提取_all字段,因此只有在映射到store设置为true时才能用于突出显示。请参阅链接https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html#plain-highlighter