如何在 NEST(Elasticsearch)中使用表达语法进行查询?

How to query using expressive syntax in NEST (Elasticsearch)?

我正在寻找一个小工具来使用 NEST APIs 查询 elasticsearch,并希望支持 status:[400 TO 499] AND (extension:php OR extension:html)

等查询

我认为 SearchQueryStringSimpleQueryString API 会有所帮助,但无法获得正确的结果。

var r = await this.client.SearchAsync<LogEvent>(s => s
    .Query(q =>
        q.SimpleQueryString(c => c.Query(searchQuery))) // searchQuery -> query string
    .From(from)
    .Size(size)
    .Index("oxyb*")
    .Sort(ss =>
        ss.Descending(f => f.TimeStamp)));

另请注意,elasticsearch 支持类似的语法 API Query String Query

您可以在嵌套中使用 QueryString 查询:

var r = client.Search<LogEvent>(s => s
    .Query(q => q.QueryString(qs => qs
       .Query("Some String")
       .AnalyzeWildcard(true)
       .DefaultOperator(Operator.And)));

您可以使用 query_string 查询来支持这些类型的查询。这些映射到 NEST 的 QueryString query

var from = 0;
var size = 20;
var searchQuery = "status:[400 TO 499] AND (extension:php OR extension:html)";
var response = await this.client.SearchAsync<LogEvent>(s => s
    .Query(q => q
        .QueryString(c => c
            .Query(searchQuery) 
        )
     )
    .From(from)
    .Size(size)
    .Index("oxyb*")
    .Sort(ss => ss
        .Descending(f => f.TimeStamp)
    )
);

产生

POST http://localhost:9200/oxyb*/logevent/_search?pretty=true 
{
  "from": 0,
  "size": 20,
  "sort": [
    {
      "timeStamp": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "query_string": {
      "query": "status:[400 TO 499] AND (extension:php OR extension:html)"
    }
  }
}