1

I have trouble getting elasticsearch results highlighting to work. I have found many examples and tried different versions but I fail applying it to my own index. What am I doing wrong?

Here is my test script:

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}
        }
    }
}'

and here the output:

{
  "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"
    }
    } ]
  }
}%  

As you can see the highlight property is missing completely.

4

1 回答 1

3

您的查询部分中有太多右大括号:

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

所以高亮部分被解析器简单地忽略了。

顺便说一句,pre_tagsandpost_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}
        }
    }    
}' 
于 2013-01-17T12:54:42.923 回答