简体   繁体   中英

Elastic Search: Combining multiple search results

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.

you can boost your fruits with a factor, so that their score will be always higher than the vegetable score, yet preserving the order. You can do that with a simple 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
                }
            ]
        }
    }
}

However please note that if you do that on a big database then it will be very resource consuming to page down to the first vegetable. I would consider rebuilding your UX so that fruits and vegetables are actually separate requests. You can do that with a _bulk endpoint and send two separate queries per each document type in one server API call, and return both results back.

建议使用弹性搜索的组合查询功能

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM