elasticsearch 如何在多个索引中排序msearch结果

gpfsuwkq  于 7个月前  发布在  ElasticSearch
关注(0)|答案(2)|浏览(86)

我需要对多个索引进行查询以匹配一个条件。我使用“multisearch”在多个索引中搜索。下面的Java代码:
List<SearchHits<?>> searchHits = searchOperations.multiSearch(queryList,classList,indexCoordinatesList);
结果是按照每个索引的“score”分别给出的,也就是说,首先在一个索引中按“score”排序,然后在另一个索引中排序,依此类推。但很明显,有必要将整个结果作为一个整体进行排序,而不是针对每个索引单独进行排序。我在Spring Data Elasticsearch文档或Elasticsearch指南中都没有找到答案。也就是说,事实上,我有两个问题:

  1. Query或BaseQuery类中是否有方法将一个查询应用于多个索引,或将这些类的每个查询仅应用于一个索引?
    1.我是否正确地理解了“searchOperations.multiSearch(...)”的应用程序只允许在java库的排序器级别进行排序,而不是在elasticsearch级别进行排序?通常,我尝试在elasticsearch级别进行排序。
    如果有人能告诉我应该使用Query类或BaseQuery中的哪种方法,我将特别感激。
velaa5lx

velaa5lx1#

您需要在合并查询时执行普通搜索,而不是执行mSearch。这可能会产生一些不兼容的分数,因此您可能需要在合并它们时应用boost。这是一个简单的示例,使用不同的查询搜索两个索引,同时从第一个索引提升分数。它在特殊的'_index'字段上使用术语查询来过滤特定的索引。

DELETE test_foo
DELETE test_bar

POST _bulk?refresh
{"index": {"_index": "test_foo"}}
{"foo": "This is a test"}
{"index": {"_index": "test_bar"}}
{"bar": "How does this work?"}

GET test_foo,test_bar/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "match": {
                  "foo": "test"
                }
              }
            ],
            "filter": [
              {
                "term": {
                  "_index": "test_foo"
                }
              }
            ],
            "boost": 2.0
          }
        },
        {
          "bool": {
            "must": [
              {
                "match": {
                  "bar": "work"
                }
              }
            ],
            "filter": [
              {
                "term": {
                  "_index": "test_bar"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

字符串

v1uwarro

v1uwarro2#

谢谢@imotov,为了解决我的问题,我用了一个“别名”:

POST:localhost:9200/_aliases
{
    "actions" : [
        { "add" : { "index" : "native_film_index", "alias" : "alias_cinema"}},
        { "add" : { "index" : "serial_index", "alias" : "alias_cinema"}},
        { "add" : { "index" : "directors_index", "alias" : "alias_cinema"}},
        { "add" : { "index" : "actors_index", "alias" : "alias_cinema"}}
    ]
}

字符串
我向名称“alias_cinema”发出请求,并在请求中指定相应索引的文档的字段名称。它在elasticsearch中工作,但我不知道如何在Spring Data Elasticsearch中发出这样的请求。
“Spring Data Elasticsearch”是否允许通过“alias”访问索引?如果是,如何访问?

相关问题