Elastic Search 5.x Nest Multiple Queries C#
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" />
What I want to do here, I want to get "white" items in price range 2000 - 3000. It's a simple request for a search api, am I right ?
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);
}
}
}
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.
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;

Also, I can't use fluent api, because I pass the parameters to my repository-like function;
EsSearchResponse<Product> response = elasticContext.Search<Product>(searchRequest);
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?
Answer
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);
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 Nest Multiple Queries C#的更多相关文章
- Getting Started with Elastic Search in .NET
I have been working on many application during my career. Many if not all had some searching capabi ...
- Elastic Search操作入门
前言 Elastic Search是基于Lucene这个非常成熟的索引方案,另加上一些分布式的实现:集群,sharding,replication等.具体可以参考我同事写的文章. 本文主要介绍ES入门 ...
- elastic search查询命令集合
Technorati 标签: elastic search,query,commands 基本查询:最简单的查询方式 query:{"term":{"title" ...
- elastic search 学习笔记
Elastic search在数据分析的应用中相当于一个数据库的搜索引擎. 跟MySQL类似,它有自己的查询语言,只不过不是关系型数据库,属于NoSQL. 可以根据索引从分布式服务器文件系统中快速存取 ...
- elastic search 学习 一
初步阅读了elastic search 的文档,并使用command实践操作. 大概明白其概念模型.
- 分库分表后跨分片查询与Elastic Search
携程酒店订单Elastic Search实战:http://www.lvesu.com/blog/main/cms-610.html 为什么分库分表后不建议跨分片查询:https://www.jian ...
- 自学elastic search
工作也有一段时间了,虽然来这个公司之后学会了几门不同的语言,但想拨尖还是任重道远. 想往高级程序员甚至是架构师方向发展.他仍然是我的学习对象.我现在做着的,无非是他玩剩下的罢了. luncene之前有 ...
- Elastic Search 上市了,市值翻倍,这群人财务自由了!
国庆长假,大部分人还深浸在风花雪月之中,而就在昨天(美国时间10月5号),我们 Java 程序员所熟知的大名鼎鼎的 Elastic Search 居然在美国纽约证券交易所上市了! 当说到搜索时,大部分 ...
- Elastic Search 安装和配置
目标 部署一个单节点的ElasticSearch集群 依赖 java环境 $java -version java version "1.8.0_161" Java(TM) SE R ...
随机推荐
- 使用.sig签名验证文件
Linux下载文件的时候,由于网络等原因,下载的文件可能不完整,对于别有心机的人可以更改文件,这就需要我们对文件的完整性进行验证.这里以securityonion-14.04.5.2.iso为例进行验 ...
- Python3 min() 函数
Python3 min() 函数 Python3 数字 描述 min() 方法返回给定参数的最小值,参数可以为序列. 语法 以下是 min() 方法的语法: min( x, y, z, .... ) ...
- 73. Set Matrix Zeroes (Array)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow ...
- Spring分配置文件开发
---------------------siwuxie095 Spring 分配置文件开发 Spring 分配置文件开 ...
- for 续2
--------siwuxie095 (二)skip=n 忽略(屏蔽.隐藏)文本前 N 行的内容. (N 必须大于 0,不能等于 0) 格式: FOR /F " ...
- 微信小程序-滑动视图注意事项
真的得吐槽下微信的开发文档,一点点都不详细的好吗. <!--垂直滚动,这里必须设置高度--> <scroll-view scroll-y="true" style ...
- IPMI设置与使用(远程控制服务器)
如果服务器crash了或者就hang住了,我们不必要跑到机房去按电源键的,因为我们也想“运筹帷幄之中,决胜千里之外”嘛.我们可以用IPMI,它可以让我们远程用一条命令开启(关闭.重启)一台服务器,也可 ...
- WPF设置Window的数据上下文(DataContext)为自身
WPF设置Window的数据上下文(DataContext)为自身的XAML: DataContext="{Binding RelativeSource={RelativeSource Se ...
- Python MySQLdb连接报2003错误原因
经测试,本地连接使用:localhost会报2003错误. 解决办法: 使用:127.0.0.1代替:localhost.
- Golang之strings包
只列举了部分函数方法的使用: 太多了....... package main import ( "fmt" "strings" ) func main() { ...