简体   繁体   English

ElasticSearch (NEST) 查询排除特定词条结果

[英]ElasticSearch (NEST) Query to exclude specific term results

I'm trying to find a QueryContainer query that I can perform on an ElasticSearch result that will essentially filter out any "A" status Items in my resultset.我试图找到一个 QueryContainer 查询,我可以对 ElasticSearch 结果执行该查询,该结果基本上会过滤掉我的结果集中的任何“A”状态项。 My ProductIndex document contains a field named "StatusCode", and I don't want to return these "A" status' to my search resultsets... I'm having the hardest time finding a way to remove these items.我的 ProductIndex 文档包含一个名为“StatusCode”的字段,我不想将这些“A”状态返回到我的搜索结果集中......我很难找到删除这些项目的方法。

This query properly finds these "A" status':此查询正确地找到这些“A”状态':

.Match(qm => qm
             .Field(f => f.StatusCode)
            .Query("A"));

But I want to do the opposite (not get all the "A" status items, but exclude them)但我想做相反的事情(不是得到所有的“A”状态项目,而是排除它们)

Based upon other threads I read on here, I came up with the following query, but it's not filtering out these results:根据我在这里阅读的其他线程,我提出了以下查询,但它没有过滤掉这些结果:

.Bool(b => b
        .MustNot(mn => mn
            .Terms(t => t
                .Field(f => f.StatusCode)
                .Terms("A")
            )
        ));

and

.Bool(b => b
        .MustNot(mn => mn
            .Term(t => t
                .Field(f => f.StatusCode).Value("A")
            )
        ));

But neither removes results that have an "A" statuscode但两者都不会删除具有“A”状态码的结果

Kibana value of a result that is still being returned, but has the status "A" code:仍在返回但具有状态“A”代码的结果的 Kibana 值:

在此处输入图像描述

The MustNot filter continues to not work for me - I ended up resolving this by performing two search queryies - one based upon the result set obtained from from the first query MustNot 过滤器对我仍然不起作用 - 我最终通过执行两个搜索查询来解决这个问题 - 一个基于从第一个查询获得的结果集

.Match(qm => qm
             .Field(f => f.StatusCode)
            .Query("A"));

This properly filtered the items to only return the ones I was trying to remove from my primary query.这正确过滤了项目,只返回我试图从主查询中删除的项目。 I obtained this document list, then applied this list as an exclusion filter to my underlying request, thus filtering out the items I no longer wanted to return.我获得了这个文档列表,然后将此列表作为排除过滤器应用于我的基础请求,从而过滤掉我不再想返回的项目。

Not as elegant as I would like, but got the job done.没有我想要的那么优雅,但完成了工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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