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

最新文章

  1. MVC, MVP, MVVM比较以及区别(上)
  2. strncpy和memcpy的区别
  3. MySql 中 case when then else end 的用法
  4. 精妙SQL语句
  5. Application.StartupPath同System.Environment.CurrentDirectory区别
  6. SQL Server -SET ANSI_NULLS
  7. 属性&quot;XmlFileName&quot;的代码生成失败
  8. precision、recall、accuracy的概念
  9. [HAOI 2010]软件安装
  10. 【代码笔记】Web-JavaScript-JavaScript 运算符
  11. kibana升级之后原本保存的数据dashboards, visualizations, index patterns丢失
  12. (转)C#串口SerialPort常用属性方法
  13. groovy编程注意事点
  14. 可以触发点击事件并变色的UILabel
  15. POJ 1243 One Person
  16. Rest架构下的增删改查
  17. C++作用域 (二)
  18. slqite3练习
  19. Oracle11gr2_ADG管理之switchover补充
  20. 常用模块(hashlib,configparser,logging)

热门文章

  1. 图片上传 纯js编码
  2. sqoop安装配置
  3. 什么是2MSL
  4. 106. Construct Binary Tree from Inorder and Postorder Traversal (Tree; DFS)
  5. [udemy]WebDevelopment_Bootstrap,Templates
  6. 使用Log4J监控系统日志邮件警报
  7. python全栈考试
  8. Android-Java读写文件到自身APP目录
  9. 结对项目— 词频统计2(语言C++)
  10. Selenium WebDriver之JavaScript