使用Elasticsearch.NET重新创建elasticsearch筛选查询

时间:2015-09-10 20:53:17

标签: c# .net nest elasticsearch

我无法从Elasticsearch.NET api输出正确的elasticsearch查询字符串。这是我想要生成的查询:

{
   "size": 5000,
   "sort": [
      {
         "id": {
            "order": "desc"
         }
      }
   ],
   "query": {
      "filtered": {
         "query": {
            "bool": {
               "must": [
                  {
                     "match_all": {}
                  },
                  {
                     "term": {
                        "stateType": {
                           "value": 2
                        }
                     }
                  }
               ]
            }
         },
         "filter": {
            "or": [
               {
                  "term": {
                     "concealed": true
                  }
               },
               {
                  "term": {
                     "govt": true
                  }
               }
            ]
         }
      }
   }
}

我以前得到的C#代码如下:

FilterContainer filterContainer = new FilterContainer();

filterContainer &= Filter<ElasticsearchLoad>.Or(f => f.Term("concealed", true));
filterContainer &= Filter<ElasticsearchLoad>.Or(f => f.Term("govt", true));

QueryContainer container = new MatchAllQuery();
container &= Query<ElasticsearchLoad>.Filtered(fq => Query<ElasticsearchLoad>.Term(l => l.StateType, 2));

SearchDescriptor<ElasticsearchLoad> searchDescriptor = new SearchDescriptor<ElasticsearchLoad>();
searchDescriptor.Query(container).Filter(filterContainer);
searchDescriptor.Size(5000);

var searchResponse = Client.Search<ElasticsearchMyDoc>(s => searchDescriptor);

正如您所看到的,我没有得到“查询”:{“过滤”:{...}}语法要生成,而且,对于我的生活,我无法弄清楚如何。有人可以帮我确定使其工作所需的C#语法吗?

谢谢!

2 个答案:

答案 0 :(得分:3)

希望你不要介意我使用流利的语法。

var searchResponse = client.Search<Person>(s => s
    .Size(5000)
    .Sort(x => x.OnField(f => f.Id).Descending())
    .Query(query => query
        .Filtered(filtered => filtered
            .Query(q => q.Bool(b => b.Must(
                m => m.MatchAll(),
                m => m.Term(t => t.OnField(f => f.StateType).Value("2")))))
            .Filter(filter => filter.Or(
                f => f.Term(t => t.Concealed, true),
                f => f.Term(t => t.Govt, true))))));

希望它有所帮助。

答案 1 :(得分:3)

您缺少的是如何使用Nest构建过滤后的查询。请参阅下面的解决方案除此之外,我建议你使用boolean过滤器(就像我在下面做的那样)而不是and / or过滤器,因为前者通常被缓存,因此执行得非常快。阅读this

FilterContainer filterContainer = null;

filterContainer |= Filter<ElasticsearchMyDoc>.Term("concealed", true);
filterContainer |= Filter<ElasticsearchMyDoc>.Term("govt", true);

QueryContainer queryContainer = new MatchAllQuery();
queryContainer &= Query<ElasticsearchMyDoc>.Term(l => l.StateType, 2);

SearchDescriptor<ElasticsearchMyDoc> searchDescriptor = new SearchDescriptor<ElasticsearchMyDoc>();
searchDescriptor.Query(qd => qd.Filtered(fq =>
    {
        fq.Filter(f => filterContainer);
        fq.Query(q => queryContainer);
    }));

searchDescriptor.Size(5000);
var searchResponse = Client.Search<ElasticsearchMyDoc>(s => searchDescriptor);