Elastic Search 5.x嵌套多个查询C# [英] Elastic Search 5.x Nest Multiple Queries C#

查看:303
本文介绍了Elastic Search 5.x嵌套多个查询C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using C# with those nuget packeges;

  <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的简单请求,对吗?

所以我为此编写了一个代码.这里是;

So I wrote a code for this. Here it is;

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插件的文档.

But it doesn't work because request has been successfull but there is no document(s) in the result, but I have. I can see the docs with Sense plugin.

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

If I combine two queries, nest will throw exception in runtime ( Says: "QueryContainer can only hold a single query already contains a TermQuery" ). Here it is;

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

Also, I can't use fluent api, because I pass the parameters to my repository-like function;

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

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

How can I combine two simple queries ( search in description field & price range between 2000-3000 ) in SearchRequest of Nest dll. And what am I doing wrong?

推荐答案

您要尝试的是从两个查询组成一个复合查询,其中两个查询都必须由文档满足才能被认为是比赛. bool查询用于合并,使用must子句来指定两个查询都必须满足.这是一个带有对象初始值设定项语法的示例

What you're trying to do is form a compound query from two queries, where both queries must be satisfied by a document in order for it to be considered a match. A bool query is used to combine queries in this manner, using the must clause to specify both queries must be satisfied. Here's an example, with the object initializer syntax

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子句

With the range query, a document is either a match for the query clause or not, so we can forgo a score being calculated for the query by adding it as a bool query filter clause

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);

这篇关于Elastic Search 5.x嵌套多个查询C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆