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. Jmeter Http接口性能测试

    Jmeter Http接口性能测试 1.      启动Jmeter Jmeter下载解压即可使用,Jmeter启动,点击D:\ProgramFiles\jmeter\apache-jmeter-2. ...

  2. Davinci-DM6467板子-外围器件的I2C地址的疑惑解答

    这篇文即调试标清视频或者说调试TVP5147和ADV7343所使用的程序名称为video_sd_playback_480i_composite 我们用的是合众达公司(SEED)的SEED-DVSD64 ...

  3. 【Java】JavaIO(一)、基础知识

    一.常用概念介绍 Java的IO,实现输入输出的基础,可以方便的实现数据的输入输出 二.流的分类 1. 按照流向来分: a). 输入流:向应用程序输 b). 输出流:从应用程序中输出 输入输出流是站在 ...

  4. ORM是什么?如何理解ORM?

    一.ORM简介         对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术.简单的说,ORM是通过使 ...

  5. R_CNN

    https://blog.csdn.net/briblue/article/details/82012575 背景本篇论文的题目是 <Rich feature hierarchies for a ...

  6. react-navigation 3.x版本的安装以及react-native-gesture-handler配置

    一.安装依赖,使用npm或yarn命令,3.x版本必须安装react-native-gesture-handler react-navigation react-native-gesture-hand ...

  7. 命名空间p方式的属性注入

    ---------------------siwuxie095 命名空间 p 方式的属性注入 命名空间 p 方式的属性注入是 Spring 2.x 版本后提供的方式 1.编写一个普通类 Book.ja ...

  8. 未能映射路径"/"

    1.检查Server.MapPath 这里面需要像这样:  ~/uploads/   有~符号. 2.应用程序池出现问题,换一个应用程序池,或者重启程序池.

  9. AES加解密

    AES加密类 <?php //php aes加密类 class AESMcrypt { public $iv = null; public $key = null; ; private $cip ...

  10. C++ 类 & 对象-C++ 内联函数-C++ this 指针-C++ 类的静态成员

    C++ 内联函数 C++ 内联函数是通常与类一起使用.如果一个函数是内联的,那么在编译时,编译器会把该函数的代码副本放置在每个调用该函数的地方. 对内联函数进行任何修改,都需要重新编译函数的所有客户端 ...