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

本文关键字:创建 elasticsearch 过滤 查询 Elasticsearch NET 新创建 使用 | 更新日期: 2023-09-27 18:12:08

我有麻烦获得正确的elasticsearch查询字符串从elasticsearch输出。净api。这是我想要生成的查询:

{
   "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#语法吗??

谢谢!

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

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

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

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

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

希望能有所帮助。