返回"搜索字词"以及结果 - Elasticsearch

时间:2015-11-19 07:16:25

标签: search elasticsearch

在我建立的elasticsearch模块中,是否可以返回"输入搜索词"在搜索结果中?

例如:

GET /signals/_search
{
"query": {
"match": {
   "focused_content": "stock"
}
}
}

返回

{
"took": 2,
"timed_out": false,
"_shards": {
  "total": 5,
  "successful": 5,
  "failed": 0
},
"hits": {
  "total": 1,
  "max_score": 0.057534903,
  "hits": [
     {
        "_index": "signals",
        "_type": "signal",
        "_id": "13",
        "_score": 0.057534903,
        "_source": {
           "username": "abc@abc.com",
           "tags": [
              "News"
           ],
           "content_url": "http://www.wallstreetscope.com/morning-stock-highlights-western-digital-corporation-wdc-fibria-celulose-sa-fbr-ametek-inc-ame-cott-corporation-cot-graftech-international-ltd-gti/25375462/",
           "source": null,
           "focused_content": "Morning Stock Highlights: Western Digital Corporation (WDC), Fibria Celulose SA (FBR), Ametek Inc. (AME), Cott Corporation (COT), GrafTech International Ltd. (GTI) - WallStreet Scope",
           "time_stamp": "2015-08-12"
        }
     }
  ]
}

是否可以输入搜索词" stock"以及每个结果(例如额外的JSON密钥以及" content_url"," source"," focused_content"," time_stamp")确定哪个搜索词带来了结果?

提前致谢!

2 个答案:

答案 0 :(得分:0)

我能想到的就是使用highlighting功能。因此它会带回额外的键_highlight,它会突出显示匹配的内容。

它不会带来完全匹配的术语。你必须在你的申请中处理它们。您可以使用前/后标签功能以特殊方式将它们包装起来,这样您的应用就可以识别它是匹配的。

答案 1 :(得分:0)

您可以在所有字段中使用突出显示,例如@Evaldas建议。这将返回结果以及匹配的字段中的值,由可自定义标记(默认为<em>)包围。

GET /signals/_search
{
  "highlight": {
    "fields": {
    "username": {},
    "tags": {},
    "source": {},
    "focused_content": {},
    "time_stamp": {}
  }
},
  "query": {
    "match": {
      "focused_content": "stock"
    }
  }
}