简体   繁体   中英

Filtering Elasticsearch Results - Nest

I currently want to write a query that matches 'name', from a class called entity, then filters out the results where a guid field (stored as a string in ES) is compared to a list of string guids. If the guid stored in the Elasticsearch record matches any in the list of guids then return those results.

The code I have written to do this is:

        ISearchResponse<Entity> oResponse = null;

        oResponse = _client.Search<Entity>(s => s
       .Size(oReq.returnValue)
       .Query(qr => qr
            .Bool(b => b
                 .Filter(flt => flt.Terms(tms => tms.Field(fd => fd.extentUid).Terms<string>(oReq.extentUids.ToList())))
                 .Should(sh => sh
                      .Match(mt => mt.Field(fl => fl.name.ToLower()).Query(oReq.name.ToLower())))))
            .Sort(srt => srt.Descending(SortSpecialField.Score)));

The following code returns 0 results when I know for certain that a lot of the Elasticsearch records have a guid that matches one in the list. Can someone help?

Note: In the code I filter the records first but I have tried putting that after the .Should() and I've tried using the .PostFilter().

Thanks in advance,

Gary

I would check your mapping and make sure your guid field is not analyzed. As shown in elastic search's Finding Exact Values , if the field is analyzed, each group separated by a - will be considered a different token. A terms query says that it

Filters documents that have fields that match any of the provided terms (not analyzed).

This means that it will not analyze the strings you provide it and query those non-analyzed strings on the specified field. If your field is analyzed, you will not get any matches.

As a side note, unless it is specified in your mapping, your name field is probably analyzed by default. You shouldn't have to use .ToLower() in your match query, since match queries will use the default search analyzer on your queried string.

I have found that when retrieving the extentUid field from elasticsearch it presents the string as uppercase. Even when using the code - tms.Field(fd => fd.extentUid.ToLower()) the string still remains uppercase.

SOLUTION = If the elasticsearch string is not analyzed, ensure the string(s) being passed to be compared against the Elasticsearch string field are uppercase.

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