简体   繁体   中英

Limit the size of documents returned by a query container NEST elasticsearch

I have a big query in NEST that is constructed from 2 query containers. Now i would like to limit the size of the returned documents of one query container to 10 and no limit to the other query container. For my result i need to have .Take(take) and .Skip(skip) for pagination. This is an example:

            var result = EsClient.Search<Business>(q => q
                           .Query(qq =>
                           {

                               QueryContainer nearByQuery= null;
                               QueryContainer locationQuery = null;

                               locationQuery = qq.Term("postCode", toLowSearchLocation);
                               nearByQuery = qq
                                                    .Filtered(ft => ft
                                                       .Filter(fl => fl
                                                           .Bool(n => n
                                                               .MustNot(x => x.Term(na => na.PostCode, searchLocation.ToLower()))
                                                           )
                                                        );
                           return locationQuery || nearByQuery;

Now what i really want is to put a .Size on the second query so i get only 10 from that one. Did anybody know how ?

Thanks!

Not possible with the Search endpoint as it only takes a single query. Even though you are building two separate queries in code, once you OR them together, they become one.

The best way to achieve this would probably be to use multi search , which will allow you to execute multiple queries in a single request, in which you could specify the desired size for each.

NEST example:

var result = EsClient.MultiSearch<Business>(ms => ms
    .Search<Business>(s => s.Size(50).Query(locationQuery))
    .Search<Business>(s => s.Size(10).Query(nearByQuery))
);

See these tests for more examples.

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