如何将两个弹性搜索结果组合成一个? [英] How can I combine two elasticsearch responses into one?

查看:116
本文介绍了如何将两个弹性搜索结果组合成一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前停留在多搜索结果响应中,为每个添加的查询返回一系列响应,但是我需要组合响应,以便相关性可以确定其顺序。获取两个独立的匹配列表对于创建统一的搜索结果页面并不好用。

I currently stuck with a result response from a multi-search that returns an array of responses for each query I add, but I need the responses to be combined so that the relevancy can determine their order. Getting back two separated lists of hits isn't great for creating a unified search results page.

我目前的搜索代码是:

SearchRequestBuilder srb1 = client.prepareSearch(index)                     
    .setSearchType(SearchType.QUERY_THEN_FETCH)
    .setQuery(QueryBuilders.queryString(query).field("body").field("title").field("author")
    .addHighlightedField("body").addHighlightedField("title").addHighlightedField("author")
    .setHighlighterPreTags("<div class='highlight'>").setHighlighterPostTags("</div>");

SearchRequestBuilder srb2 = client.prepareSearch(index2)                    
    .setSearchType(SearchType.QUERY_THEN_FETCH)
    .setQuery(
        QueryBuilders.queryString(query)
        .field("file")
        .field("title")
        .field("author")
    )
    .addHighlightedField("file").addHighlightedField("title").addHighlightedField("author")
    .setHighlighterPreTags("<div class='highlight'>").setHighlighterPostTags("</div>");

MultiSearchResponse sr = client.prepareMultiSearch()
    .add(srb1)
    .add(srb2)
    .execute().actionGet();

感谢您能给予任何帮助!



编辑:
这是我的最终代码

Thanks for any help you all can give!


This is my final code

SearchRequestBuilder srb = client
                            .prepareSearch(index,index2)    
                            .setSearchType(SearchType.QUERY_AND_FETCH)
                            .setQuery(
                                QueryBuilders.boolQuery()
                                .should(
                                    QueryBuilders.queryString(query)
                                    .field("body")
                                    .field("title")
                                    .field("author")
                                )
                                .should(
                                    QueryBuilders.queryString(query)
                                    .field("file")
                                    .field("title")
                                    .field("author")
                                )
                                .minimumNumberShouldMatch(1)
                            )
                            .addHighlightedField("title")
                            .addHighlightedField("author")
                            .addHighlightedField("body")    
                            .addHighlightedField("file")
                            .setHighlighterPreTags("<div class='highlight'>")
                            .setHighlighterPostTags("</div>");                  
SearchResponse sr = srb.execute().actionGet();


推荐答案

而不是使用MultiSearch,我会使用Bool查询:

Instead of using a MultiSearch I'd use a Bool Query:


Bool查询

查询匹配其他
查询的布尔组合的文档。 bool查询映射到Lucene BooleanQuery。它使用
构建一个或多个布尔子句,每个子句都带有类型的事件。
事件类型是:

A query that matches documents matching boolean combinations of other queries. The bool query maps to Lucene BooleanQuery. It is built using one or more boolean clauses, each clause with a typed occurrence. The occurrence types are:

必须

子句查询)必须出现在匹配的文档中。

The clause (query) must appear in matching documents.

应该

)应该出现在匹配文档中。在一个
布尔查询中,没有必需条款,一个或多个应该
条款必须匹配一个文档。可以使用minimum_should_match参数设置
匹配的最小数量的条款。

The clause (query) should appear in the matching document. In a boolean query with no must clauses, one or more should clauses must match a document. The minimum number of should clauses to match can be set using the minimum_should_match parameter.

must_not

子句(查询)不得出现在匹配的文档中。

The clause (query) must not appear in the matching documents.

http://www.elasticsearch.org/guide/en/elasticsearch /reference/current/query-dsl-bool-query.html

在你的情况下,我将使用一个should子句组合你的两个查询。这样的东西:

In your case I'd combine your two queries using a should clause. Something like this:

QueryBuilder qb = QueryBuilders
                    .boolQuery()
                    .should(termQuery("yourfield", "query1"))
                    .should(termQuery("yourfield", "query2"));

http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/query-dsl- queries.html

这篇关于如何将两个弹性搜索结果组合成一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆