elasticsearch在结果中缺少高亮属性

时间:2013-01-17 12:29:11

标签: search-engine elasticsearch highlighting

我无法将弹性搜索结果突出显示起作用。我找到了很多例子并尝试了不同的版本,但我没有将它应用到我自己的索引中。我做错了什么?

这是我的测试脚本:

init() {
    curl -XDELETE http://localhost:9200/twitter
    echo
    curl -XPUT http://localhost:9200/twitter
    echo

    curl -XPUT http://localhost:9200/twitter/tweet/_mapping -d '{
        "tweet" : {
            "properties" : {
                "user" : { "type" : "string" },
                "message" : {
                    "type" : "string",
                    "index": "analyzed",
                    "store": "yes",
                    "term_vector" : "with_positions_offsets"
                 }
            }
        }
    }'
    echo
    curl -XPOST http://localhost:9200/twitter/tweet -d '{
        "user": "kimchy",
        "message": "You know, for Search"
    }'
    echo
    curl -XPOST http://localhost:9200/twitter/tweet -d '{
        "user": "bar",
        "message": "You know, foo for Search"
    }'
    echo

    sleep 2
    echo '-------------------'
}

[ "$1" = "init" ] && init

curl -X GET 'http://localhost:9200/twitter/_search/?pretty=true' -d '{
    "query":{
            "query_string":{
                "query":"foo"
            }
        }
    },
    "highlight":{
        "pre_tags": "<b>",
        "post_tags": "</b>",
        "fields" : {
            "message" : {"number_of_fragments": 20}
        }
    }
}'

在这里输出:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.09492774,
    "hits" : [ {
      "_index" : "twitter",
      "_type" : "tweet",
      "_id" : "1tgGWGhnRLy-nJIAunFeeQ",
      "_score" : 0.09492774, "_source" : {
        "user": "bar",
        "message": "You know, foo for Search"
    }
    } ]
  }
}%  

正如您所见,高亮显示属性完全缺失。

1 个答案:

答案 0 :(得分:3)

查询部分中有太多的结束花括号:

"query":{
        "query_string":{
            "query":"foo"
        }
    } <---- This one is not needed.
},

因此解析器会忽略突出显示部分。

顺便说一下,pre_tagspost_tags应该是数组:

curl "localhost:9200/twitter/tweet/_search?pretty=true" -d '{
    "query": {
        "query_string": {
            "query": "foo"
        }
    },
    "highlight": {
        "pre_tags": ["<b>"],
        "post_tags": ["</b>"],
        "fields": {
            "message": {"number_of_fragments": 20}
        }
    }    
}'