一尘不染

Elastic Search 5.x嵌套多个查询C#

elasticsearch

我正在将C#与这些nuget包一起使用;

  <package id="Elasticsearch.Net" version="5.2.0" targetFramework="net462" />
  <package id="NEST" version="5.2.0" targetFramework="net462" />
  <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net462" />

我想在这里做些什么,我想获得价格在
2000-3000之间的“白色”物品。这是对搜索API的简单要求,对吗?

所以我为此编写了代码。这里是;

private static void Search(IElasticContext elasticContext, string indexName)
    {
        IQueryContainer termQueryContainer = new QueryContainer();
        termQueryContainer.Term = new TermQuery
                                  {
                                      Field = new Field("description"),
                                      Value = "white"
                                  };

        IQueryContainer rangeQueryContainer = new QueryContainer();
        rangeQueryContainer.Range = new NumericRangeQuery
                                    {
                                        Field = new Field("price"),
                                        LessThanOrEqualTo = 3000,
                                        GreaterThanOrEqualTo = 2000
                                    };

        //Should get 2 items.

        SearchRequest<Product> searchRequest = new SearchRequest<Product>(indexName, typeof(Product))
                                               {
                                                   Size = 10,
                                                   From = 0,
                                                   Query = (QueryContainer) rangeQueryContainer,
                                                   PostFilter = (QueryContainer) termQueryContainer
                                               };

        EsSearchResponse<Product> response = elasticContext.Search<Product>(searchRequest);

        Console.WriteLine(response.StatusMessage);

        if (response.IsValid)
        {
            foreach (Product product in response.Documents)
            {
                Console.WriteLine("Id: {0} | Name: {1}", product.Id, product.Name);
            }
        }
    }

但这不起作用,因为请求已成功完成,但是结果中没有文档,但是我有。我可以看到带有Sense插件的文档。

如果我合并两个查询,则nest将在运行时引发异常(说:“ QueryContainer只能容纳一个已经包含TermQuery的查询”)。
这里是;

Nest引发异常

另外,我不能使用流利的api,因为我将参数传递给了类似存储库的函数。

    EsSearchResponse<Product> response = elasticContext.Search<Product>(searchRequest);

如何在Nest dll的SearchRequest中组合两个简单查询(在描述字段中搜索以及价格范围在2000-3000之间)。我在做什么
错呢?


阅读 562

收藏
2020-06-22

共1个答案

一尘不染

您想要做的是从两个查询组成一个复合查询,其中两个查询都必须由文档满足才能将其视为
匹配。一个bool查询用于查询在此结合的方式,使用must条款同时指定
查询,必须得到满足。这是一个带有对象初始化器语法的示例

var client = new ElasticClient();
var indexName = "index-name";
var mustClauses = new List<QueryContainer>();

mustClauses.Add(new TermQuery
{
    Field = new Field("description"),
    Value = "white"
});

mustClauses.Add(new NumericRangeQuery
{
    Field = new Field("price"),
    LessThanOrEqualTo = 3000,
    GreaterThanOrEqualTo = 2000
});

var searchRequest = new SearchRequest<Product>(indexName)
{
    Size = 10,
    From = 0,
    Query = new BoolQuery { Must = mustClauses }
};

var searchResponse = client.Search<Product>(searchRequest);

对于range查询,文档是否是查询子句的匹配项,所以我们可以通过将其添加为bool查询filter子句来放弃为查询计算的分数

var indexName = "index-name";
var mustClauses = new List<QueryContainer>();
var filterClauses = new List<QueryContainer>();

mustClauses.Add(new TermQuery
{
    Field = new Field("description"),
    Value = "white"
});

filterClauses.Add(new NumericRangeQuery
{
    Field = new Field("price"),
    LessThanOrEqualTo = 3000,
    GreaterThanOrEqualTo = 2000
});

var searchRequest = new SearchRequest<Product>(indexName)
{
    Size = 10,
    From = 0,
    Query = new BoolQuery 
    { 
        Must = mustClauses,
        Filter = filterClauses
    }
};

var searchResponse = client.Search<Product>(searchRequest);
2020-06-22