跨多个索引搜索时,如何获取搜索结果中的索引名称?

When searching across multiple indices, how to get the index name in search results?

来自 elasticsearch 的 'multi-match' 查询 returns 跨多个索引搜索时搜索结果中的索引名称。

GET localhost:9200/users,locations/_search

{
    "query":{
        "multi_match":{
            "query": "pus",
            "type": "bool_prefix",
            "fields":[
                "name",
                "name._2gram",
                "name._3gram"
            ]
        }
    }
}

响应包含 _index 字段,该字段说明产生结果的索引

"hits": [{
    "_index": "locations",
    "_type": "_doc",
    "_id": "418",
    "_score": 1.0,
    "_source": {
      "name": "pusan-kwangyokshi | korea republic of | asia",
      "@timestamp": "2020-09-21T07:12:34.432Z"
    }
  },
  {
    "_index": "users",
    "_type": "_doc",
    "_id": "4",
    "_score": 1.0,
    "_source": {
      "name": "Puspjer taojomi",
      "uniqueId": "skjhs-sklhjs125",
      "accountStatus": 1,
      "email": "xyz@gmail.com",
      "@timestamp": "2020-09-22T09:57:22.977Z"
    }
  },
  {
    "_index": "users",
    "_type": "_doc",
    "_id": "52",
    "_score": 1.0,
    "_source": {
      "name": "Puspa yabna",
      "uniqueId": "sjhslkj-ta18ynsarda5",
      "accountStatus": 1,
      "email": "abc@gmail.com",
      "@timestamp": "2020-09-22T09:57:23.121Z"
    }
  }
]

spring-data-elasticsearch 中用于 hits 的 class 是 SearchHits 并且 SearchHit 有字段 id, content, score 用于获取与 elasticsearch 查询相似的数据。但它不包含用于存储_index字段信息的相关字段。

还支持吗?我需要发送搜索命中类型(_index 名称),客户端应用程序将根据该类型生成一些 URL。

这是我使用 spring

的搜索查询
final NativeSearchQuery query = new NativeSearchQuery(QueryBuilders.multiMatchQuery(q, "name", "name._2gram", "name._3gram").type(MultiMatchQueryBuilder.Type.BOOL_PREFIX));
IndexCoordinates indexCoordinates = IndexCoordinates.of("users","locations");

return operations.search(query, OutDto.class, indexCoordinates)
        .getSearchHits()
        .stream()
        .map(SearchHit::getContent)
        .collect(Collectors.toList());


//The DTO class
public class OutDto{
  private Integer id;
  private String uniqueId;
  private String name;
  private String index;
}

这是通过 https://jira.spring.io/browse/DATAES-848 实现的,自里程碑版本 4.1.M1 以来在 4.1 版本中。

有关如何使用 RC 或里程碑版本的信息,请访问 https://github.com/spring-projects/spring-data-elasticsearch#maven-configuration