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. 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#的更多相关文章

  1. 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 ...

  2. Elastic Search操作入门

    前言 Elastic Search是基于Lucene这个非常成熟的索引方案,另加上一些分布式的实现:集群,sharding,replication等.具体可以参考我同事写的文章. 本文主要介绍ES入门 ...

  3. elastic search查询命令集合

    Technorati 标签: elastic search,query,commands 基本查询:最简单的查询方式 query:{"term":{"title" ...

  4. elastic search 学习笔记

    Elastic search在数据分析的应用中相当于一个数据库的搜索引擎. 跟MySQL类似,它有自己的查询语言,只不过不是关系型数据库,属于NoSQL. 可以根据索引从分布式服务器文件系统中快速存取 ...

  5. elastic search 学习 一

    初步阅读了elastic search 的文档,并使用command实践操作. 大概明白其概念模型.

  6. 分库分表后跨分片查询与Elastic Search

    携程酒店订单Elastic Search实战:http://www.lvesu.com/blog/main/cms-610.html 为什么分库分表后不建议跨分片查询:https://www.jian ...

  7. 自学elastic search

    工作也有一段时间了,虽然来这个公司之后学会了几门不同的语言,但想拨尖还是任重道远. 想往高级程序员甚至是架构师方向发展.他仍然是我的学习对象.我现在做着的,无非是他玩剩下的罢了. luncene之前有 ...

  8. Elastic Search 上市了,市值翻倍,这群人财务自由了!

    国庆长假,大部分人还深浸在风花雪月之中,而就在昨天(美国时间10月5号),我们 Java 程序员所熟知的大名鼎鼎的 Elastic Search 居然在美国纽约证券交易所上市了! 当说到搜索时,大部分 ...

  9. Elastic Search 安装和配置

    目标 部署一个单节点的ElasticSearch集群 依赖 java环境 $java -version java version "1.8.0_161" Java(TM) SE R ...

随机推荐

  1. canvas动画--demo

    canvas动画:bubble

  2. 【bzoj1911】[Apio2010]特别行动队

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4048  Solved: 1913[Submit][Statu ...

  3. Linux System V Semaphore semget多进程同时创建缺陷解决方法

    System V Semaphore的创建过程缺陷是创建与赋初值由两个函数完成,这会导致两个进程同时创建的话会出现竞争和不一致状态,即使是使用了IPC-EXCL标记. 示例: oflag = IPC- ...

  4. setitemdata 32位 or 64位

    我用SetItemData 和GetItemData把数据库一条数据导入到CListctrl 32位下 程序都可以跑 GetItemData能得到自定义类数据 可以64位得不到?

  5. Android Service 通知Activity更新界面的方法研究

    Android Service 通知Activity更新界面的方法研究   Android的最重要的组件式service和activity,那么在使用的过程中,我们最常遇到的问题是他们之间的通信问题. ...

  6. 20155317 2016-2017-2 《Java程序设计》第7周学习总结

    20155317 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 1.在只有Lambda表达式的情况下,参数的类型必须写出来. 2.Lambda表达式本身是中 ...

  7. iOS7修改UISearchBar的Cancel按钮的颜色和文字

    两行代码搞定: [[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor ...

  8. CodeForces - 669D——(思维题)

    Little Artem is fond of dancing. Most of all dances Artem likes rueda — Cuban dance that is danced b ...

  9. Geronimo 叛逆者: 使用集成软件包:Codehaus 的 Woodstox(转载)

    XML 解析器通常是高性能.健壮应用程序的关键.传统的 XML 解析技术包括文档对象模型(Document Object Model,DOM)和 Simple API for XML (SAX).现在 ...

  10. Could not load file or assembly 'System.Data.SQLite ... 试图加载格式不正确的程序

    坑爹的System.Data.SQLite. 先给出下载地址:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki ...