使用 Elasticsearch.NET 重新创建 elasticsearch 过滤查询

Recreate elasticsearch filtered query using Elasticsearch.NET

我无法从 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);

如您所见,我没有生成 "query" : { "filtered" : { ...}} 语法,而且,对于我来说,我可以想办法。有人可以帮我确定让它工作所需的 C# 语法吗?

谢谢!

希望你不介意我使用了流畅的语法。

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

希望对您有所帮助。

您缺少的是如何使用 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);