【问题标题】:Returning the "search term" along with result - Elasticsearch返回“搜索词”和结果 - Elasticsearch
【发布时间】:2015-11-19 07:16:25
【问题描述】:

在我自己搭建的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”)来识别哪个搜索词带来了这样的结果?

提前致谢!

【问题讨论】:

    标签: search elasticsearch


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-03
        • 1970-01-01
        • 2020-08-30
        • 1970-01-01
        • 2021-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多