Elastic Search: Combining multiple search results

时间:2015-06-25 10:02:34

标签: java search elasticsearch

i have a tricky case which I dont know how to solve.

I have 2 queries: q1 and q2 The 2 queries result in r1 and r2

each of the results is sorted in a special order.

As an overall search result i want to return a concatenation of r1+r2 before returning it to the application.

The reason is that i would break the pagination if I do it later.

Any idea? Its probalby an easy question but I am stuck on this...

PS:

we also experimented with sorting first by a priary sort criteria and then by the score.. but the score always breaks the first sort criteria somehow we cant get the score working only within its bucket.

2 个答案:

答案 0 :(得分:1)

你可以用一个因子提高你的成果,这样他们的得分总是高于蔬菜得分,但保留了顺序。您可以使用简单的weight function score query

来实现这一目标
PUT /test

POST /test/fruits/1
{
    "field": "hello world"
}

POST /test/fruits/2
{
    "field":"hello"
}

POST /test/veges/1
{
    "field": "hello world"
}

POST /test/veges/2
{
    "field":"hello"
}

POST /test/_search
{

    "query": {

        "function_score": {
            "query": {
                "match": {
                   "field": "hello"
                }   
            },
            "functions": [
                {
                    "filter": {
                        "type": {
                           "value": "fruits"
                        }
                    },
                    "weight": 2
                }
            ]
        }
    }
}

但请注意,如果您在大型数据库上执行此操作,那么向下浏览第一个蔬菜将非常耗费资源。我会考虑重建你的用户体验,以便水果和蔬菜实际上是单独的请求。您可以使用_bulk端点执行此操作,并在一个服务器API调用中为每个文档类型发送两个单独的查询,并返回两个结果。

答案 1 :(得分:0)

建议使用Combining query feature弹性搜索;