在弹性搜索中构建多个过滤器 [英] Build multiple filter in Elasticsearch

查看:149
本文介绍了在弹性搜索中构建多个过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以建议如何根据多个过滤器构建查询。
目前,我想使用以下过滤器实现搜索功能:




  • ProductName

  • 国家(数组)

  • 城市(数组)



县或市没有选定的值,查询假设您正在搜索所有国家和城市。
如果有选定的县和市,那么结果应该是基于所选的县市。



我以下查询开始。 / p>

  static void Main(string [] args)
{
var uri = new Uri(http: //本地主机:9200\" );
var connectionPool = new SingleNodeConnectionPool(uri);
var settings = new ConnectionSettings(connectionPool);

var client = new ElasticClient(settings);

if(counties.Count> 0)
{
foreach(县中的字符串国家/地区)
{
//添加要添加的查询在client.Search
}
}

if(cities.Count> 0)
{
foreach(城市中的城市)
{
//添加要添加到customer.Search中的查询
}
}

client.Search< Product>(s => s
.Query(q => q
.Bool(b => b
.Must(mu =μ b
.Match(m => m
Field(f => f.ProductName)
.Query(some text)
),




);
}


解决方案

我只是回答自己的问题。目前,我采用了这种方法。

  var sd = new SearchDescriptor< object>(); 
var qc = new QueryContainer();
var qd = new QueryContainerDescriptor< object>();

sd.From(0);
sd.Size(100);
sd.Index(产品);
sd.Type(Product);

if(!string.IsNullOrEmpty(title))
{
qc = qd.Match(m1 => m1
.Field(title)
.Query(title)
);
}

if(countries.Count> 0)
{
qc = qd.Terms(t => t
.Field国家)
.Terms(countries.ToArray())
);
}

if(cities.Count> 0)
{
qc = qd.Terms(t => t
.Field城市)
.Terms(cities.ToArray())
);
}

sd.Query(q => q
.Bool(b => b
.Must(qc)

);

var result = client.Search< object>(s => s = sd);


Can you suggest how do I build a query based on multiple filter. Currently, I want to implement a search functionality using the following filters:

  • ProductName
  • Countries (array)
  • Cities (array)

The requirements is that, when counties or cities has no selected value, the query assumes that you are searching all countries and cities. If there are selected counties and cities, then the result should be base on the selected counties and cities.

I have the query below to start with.

static void Main(string[] args)
{   
    var uri = new Uri("http://localhost:9200");
    var connectionPool = new SingleNodeConnectionPool(uri);
    var settings = new ConnectionSettings(connectionPool);

    var client = new ElasticClient(settings);

    if (counties.Count > 0) 
    {
        foreach(string country in counties)
        {
            // Add a query to be added in client.Search     
        }
    }

    if (cities.Count > 0)
    {
        foreach(string city in cities)
        {
            // Add a query to be added in client.Search
        }
    }

    client.Search<Product>(s => s
                            .Query(q => q
                                .Bool(b => b
                                    .Must(mu => mu
                                        .Match(m => m
                                            .Field(f => f.ProductName)
                                            .Query("some text")
                                        ),
                                        .
                                    )
                                )
                            )
                        );
}

解决方案

I just answer my own question. Currently, I came with this approach.

var sd = new SearchDescriptor<object>();
var qc = new QueryContainer();
var qd = new QueryContainerDescriptor<object>();

sd.From(0);
sd.Size(100);
sd.Index("Products");
sd.Type("Product");

if (!string.IsNullOrEmpty(title))
{
    qc = qd.Match(m1 => m1
        .Field("title")
        .Query(title)
    );
}

if (countries.Count > 0)
{
    qc = qd.Terms(t => t
            .Field("country")
            .Terms(countries.ToArray())
        );
}

if (cities.Count > 0)
{
    qc = qd.Terms(t => t
            .Field("city")
            .Terms(cities.ToArray())
        );
}

sd.Query(q => q
        .Bool(b => b
            .Must(qc)
        )
    );

var result = client.Search<object>(s => s = sd);

这篇关于在弹性搜索中构建多个过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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