我无法将弹性搜索结果突出显示起作用。我找到了很多例子并尝试了不同的版本,但我没有将它应用到我自己的索引中。我做错了什么?
这是我的测试脚本:
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"
}
} ]
}
}%
正如您所见,高亮显示属性完全缺失。
答案 0 :(得分:3)
查询部分中有太多的结束花括号:
"query":{
"query_string":{
"query":"foo"
}
} <---- This one is not needed.
},
因此解析器会忽略突出显示部分。
顺便说一下,pre_tags
和post_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}
}
}
}'