【问题标题】:Return field where text was found in ElasticSearch返回在 ElasticSearch 中找到文本的字段
【发布时间】:2015-09-09 15:20:52
【问题描述】:

我需要帮助。我在 elasticsearch 1.6 上有这些文件

{
 "name":"Sam",
 "age":25,
 "description":"Something"
},
{
 "name":"Michael",
 "age":23,
 "description":"Something else"
}

用这个查询:

GET /MyIndex/MyType/_search?q=Michael

弹性返回这个对象:

{
 "name":"Michael",
 "age":23,
 "description":"Something else"
}

...没错,但我想获得找到文本“Michael”的确切密钥。那可能吗?非常感谢。

【问题讨论】:

  • 你找到答案了吗?

标签: search field elasticsearch


【解决方案1】:

我假设您所说的键是指文档 ID。
索引以下文档时:

PUT my_index/my_type/1
{
     "name":"Sam",
     "age":25,
     "description":"Something"
}

PUT my_index/my_type/2
{
     "name":"Michael",
     "age":23,
     "description":"Something else"
}

并搜索:

GET /my_index/my_type/_search?q=Michael

你会得到以下响应:

{
   "took": 8,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.15342641,
      "hits": [
         {
            "_index": "my_index",
            "_type": "my_type",
            "_id": "2",
            "_score": 0.15342641,
            "_source": {
               "name": "Michael",
               "age": 23,
               "description": "Something else"
            }
         }
     ]
   }
}

如您所见, hits 数组包含每个搜索命中的对象。
在这种情况下,Michael 的键是 "_id": "2",它是他的文档 ID。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2017-01-17
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 2015-06-11
    • 1970-01-01
    相关资源
    最近更新 更多