2015-11-19 116 views
1

在我创建的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": "[email protected]", 
      "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" 
     } 
    } 
    ] 
} 

是否有输入搜索词“股”可能与每一结果(如连同额外的JSON重点沿“ content_url“,”source“,”focused_content“,”time_stamp“)来标识哪个搜索词带来了该结果?

在此先感谢!

回答

0

我能想到的,会使用highlighting功能。所以它会带来额外的密钥_highlight,它会突出显示匹配的东西。

它不会带来完全匹配的术语,寿。你必须在你的应用程序中处理它们。您可以使用前/后标签功能以某种方式将它们包装起来,以便您的应用程序可以识别它是否匹配。

0

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

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