Filtering Elasticsearch Results - Nest

时间:2016-10-19 13:34:52

标签: c# .net elasticsearch 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

2 个答案:

答案 0 :(得分:0)

我会检查你的映射并确保你的guid字段没有被分析。如弹性搜索Finding Exact Values所示,如果分析字段,则由-分隔的每个组将被视为不同的标记。 terms query表示它

  

过滤包含与任何提供的字词匹配的字段(未分析)的文档。

这意味着它不会分析您提供的字符串并查询指定字段上的未分析字符串。如果您的领域得到分析,您将无法获得任何匹配。

作为旁注,除非在您的映射中指定,否则默认情况下可能会对您的name字段 进行分析。您不必在匹配查询中使用.ToLower(),因为match queries将在您查询的字符串上使用默认搜索分析器。

答案 1 :(得分:0)

我发现从elasticsearch检索extentUid字段时,它将字符串显示为大写。即使使用代码 - tms.Field(fd =&gt; fd.extentUid.ToLower()),字符串仍然保持大写。

SOLUTION =如果未分析elasticsearch字符串,请确保传递的字符串与Elasticsearch字符串字段进行比较是大写的。