amazon-web-services - ElasticSearch NEST QueryParsingException bool查询不支持过​​滤器

标签 amazon-web-services elasticsearch nest

我正在使用NEST 2.3.2查询ElasticSearch,并且需要基于可选的 bool(boolean) 值为FollowUpDateTime字段创建条件过滤器。如果那个 bool(boolean) 值

  • 是真实的-想要查看所有带有非空白日期的记录
  • 为假-不含日期的记录
  • 为空-显示所有记录。

  • 这是我在Nest中所拥有的:
                        var containers = new QueryContainer[7];
                        //Size is based on hte number of search parameters.. 
                        var descriptor = new QueryContainerDescriptor<EsNoteModel>();
    
                        var filters = new QueryContainer[1];
                        var filterdescriptor = new QueryContainerDescriptor<EsNoteModel>();  
    
           /*...... some code for other parameters here .....*/
    
            if (hasFollowUpDate.HasValue)
                        {
                            if ((bool) hasFollowUpDate)
                            {
                                //If true shows only with a non-blank follow-up date
                                filters[0] = filterdescriptor.Exists(ff => ff.Field("FollowUpDateTime"));
                            }
                            else
                            {
                                //If false shows only notes with a blank follow-up date
                                filters[0] = filterdescriptor.Missing(ff => ff.Field("FollowUpDateTime"));
                            }
                        }
    
    /*...... CALL to ES below .....*/    
    
                        var result = ElasticSearchClient.Search<EsNoteModel>(body => body
                            .From(offset - 1)
                            .Size(rows)
                            .Query(q => q
                                .Bool(b => b
                                      .Must(containers)
                                      .Filter(f => f.Bool(t => t.Must(filters)))
                                      )
                                    )
    
                            .Sort(s => s
                                .Field(f => f
                                    .Field(p => p.NoteDate)
                                    .Order(SortOrder.Descending)))
    
                            );
    

    问题是上面的查询生成了错误。请看下面:

    Invalid NEST response built from a unsuccessful low level call on POST: /_search?pretty=true

    Audit trail of this API call:

    • BadResponse: Node: http://**********.es.amazonaws.com/ Took: 00:00:00.1381301

      OriginalException: System.Net.WebException: The remote server returned an error: (400) Bad Request. at

    System.Net.HttpWebRequest.GetResponse() at Elasticsearch.Net.HttpConnection.Request[TReturn](RequestData requestData)


    # Request:
    {
      "from": 0,
      "size": 25,
      "sort": [
        {
          "NoteDate": {
            "order": "desc"
          }
        }
      ],
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "CustomerId": {
                  "query": "2810855"
                }
              }
            }
          ],
          "filter": [
            {
              "bool": {
                "must": [
                  {
                    "exists": {
                      "field": "FollowUpDateTime"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
    

    回应如下:
    # Response:
    {
      "error" : "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[-t7SwBo_TjeH_98h_56ANw][customernote][0]: RemoteTransportException[[Neophyte][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[customernote][0]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@2d55231>!]: Parse Failure [Failed to parse source [{\r\n  \"from\": 0,\r\n  \"size\": 25,\r\n  \"sort\": [\r\n    {\r\n      \"NoteDate\": {\r\n        \"order\": \"desc\"\r\n      }\r\n    }\r\n  ],\r\n  \"query\": {\r\n    \"bool\": {\r\n      \"must\": [\r\n        {\r\n          \"match\": {\r\n            \"CustomerId\": {\r\n              \"query\": \"28_10855\"\r\n            }\r\n          }\r\n        }\r\n      ],\r\n      \"filter\": [\r\n        {\r\n          \"bool\": {\r\n            \"must\": [\r\n              {\r\n                \"exists\": {\r\n                  \"field\": \"FollowUpDateTime\"\r\n                }\r\n              }\r\n            ]\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[TOOugCq3ROqF9CGnHIay2g][customernote][1]: SearchParseException[[customernote][1]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@562f97b5>!]: Parse Failure [Failed to parse source [{\r\n  \"from\": 0,\r\n  \"size\": 25,\r\n  \"sort\": [\r\n    {\r\n      \"NoteDate\": {\r\n        \"order\": \"desc\"\r\n      }\r\n    }\r\n  ],\r\n  \"query\": {\r\n    \"bool\": {\r\n      \"must\": [\r\n        {\r\n          \"match\": {\r\n            \"CustomerId\": {\r\n              \"query\": \"28_10855\"\r\n            }\r\n          }\r\n        }\r\n      ],\r\n      \"filter\": [\r\n        {\r\n          \"bool\": {\r\n            \"must\": [\r\n              {\r\n                \"exists\": {\r\n                  \"field\": \"FollowUpDateTime\"\r\n                }\r\n              }\r\n            ]\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[TOOugCq3ROqF9CGnHIay2g][customernote][2]: SearchParseException[[customernote][2]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@394cf46e>!]: Parse Failure [Failed to parse source [{\r\n  \"from\": 0,\r\n  \"size\": 25,\r\n  \"sort\": [\r\n    {\r\n      \"NoteDate\": {\r\n        \"order\": \"desc\"\r\n      }\r\n    }\r\n  ],\r\n  \"query\": {\r\n    \"bool\": {\r\n      \"must\": [\r\n        {\r\n          \"match\": {\r\n            \"CustomerId\": {\r\n              \"query\": \"28_10855\"\r\n            }\r\n          }\r\n        }\r\n      ],\r\n      \"filter\": [\r\n        {\r\n          \"bool\": {\r\n            \"must\": [\r\n              {\r\n                \"exists\": {\r\n                  \"field\": \"FollowUpDateTime\"\r\n                }\r\n              }\r\n            ]\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[-t7SwBo_TjeH_98h_56ANw][customernote][3]: RemoteTransportException[[Neophyte][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[customernote][3]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@46d07ba8>!]: Parse Failure [Failed to parse source [{\r\n  \"from\": 0,\r\n  \"size\": 25,\r\n  \"sort\": [\r\n    {\r\n      \"NoteDate\": {\r\n        \"order\": \"desc\"\r\n      }\r\n    }\r\n  ],\r\n  \"query\": {\r\n    \"bool\": {\r\n      \"must\": [\r\n        {\r\n          \"match\": {\r\n            \"CustomerId\": {\r\n              \"query\": \"28_10855\"\r\n            }\r\n          }\r\n        }\r\n      ],\r\n      \"filter\": [\r\n        {\r\n          \"bool\": {\r\n            \"must\": [\r\n              {\r\n                \"exists\": {\r\n                  \"field\": \"FollowUpDateTime\"\r\n                }\r\n              }\r\n            ]\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[TOOugCq3ROqF9CGnHIay2g][customernote][4]: SearchParseException[[customernote][4]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@74fb3710>!]: Parse Failure [Failed to parse source [{\r\n  \"from\": 0,\r\n  \"size\": 25,\r\n  \"sort\": [\r\n    {\r\n      \"NoteDate\": {\r\n        \"order\": \"desc\"\r\n      }\r\n    }\r\n  ],\r\n  \"query\": {\r\n    \"bool\": {\r\n      \"must\": [\r\n        {\r\n          \"match\": {\r\n            \"CustomerId\": {\r\n              \"query\": \"28_10855\"\r\n            }\r\n          }\r\n        }\r\n      ],\r\n      \"filter\": [\r\n        {\r\n          \"bool\": {\r\n            \"must\": [\r\n              {\r\n                \"exists\": {\r\n                  \"field\": \"FollowUpDateTime\"\r\n                }\r\n              }\r\n            ]\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }]",
      "status" : 400
    }
    

    如您所见,错误报告“嵌套:QueryParsingException [[customernote] bool查询不支持[filter]];”有什么想法我在这个例子中做错了吗?这是filter子句的正确使用吗?

    ****更新

    变更查询
     var result = ElasticSearchClient.Search<EsNoteModel>(body => body
                    .From(offset - 1)
                    .Size(rows)
                    .Query(q => q
                        .Bool(b => b
                              .Must(containers)
                              .Filter(filters)
                              )
                            )
    
                    .Sort(s => s
                        .Field(f => f
                            .Field(p => p.NoteDate)
                            .Order(SortOrder.Descending)))
    
                    );
    

    请求
    # Request:
    {
      "from": 0,
      "size": 25,
      "sort": [
        {
          "NoteDate": {
            "order": "desc"
          }
        }
      ],
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "CustomerId": {
                  "query": "2810855"
                }
              }
            }
          ],
          "filter": [
            {
              "exists": {
                "field": "FollowUpDateTime"
              }
            }
          ]
        }
      }
    }
    

    得到响应错误:
    # Response:
    {
      "error" : "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[TOOugCq3ROqF9CGnHIay2g][customernote][0]: SearchParseException[[customernote][0]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@4839a60c>!]: Parse Failure [Failed to parse source [{\r\n  \"from\": 0,\r\n  \"size\": 25,\r\n  \"sort\": [\r\n    {\r\n      \"NoteDate\": {\r\n        \"order\": \"desc\"\r\n      }\r\n    }\r\n  ],\r\n  \"query\": {\r\n    \"bool\": {\r\n      \"must\": [\r\n        {\r\n          \"match\": {\r\n            \"CustomerId\": {\r\n              \"query\": \"2810855\"\r\n            }\r\n          }\r\n        }\r\n      ],\r\n      \"filter\": [\r\n        {\r\n          \"exists\": {\r\n            \"field\": \"FollowUpDateTime\"\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[-t7SwBo_TjeH_98h_56ANw][customernote][1]: RemoteTransportException[[Neophyte][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[customernote][1]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@21d845ae>!]: Parse Failure [Failed to parse source [{\r\n  \"from\": 0,\r\n  \"size\": 25,\r\n  \"sort\": [\r\n    {\r\n      \"NoteDate\": {\r\n        \"order\": \"desc\"\r\n      }\r\n    }\r\n  ],\r\n  \"query\": {\r\n    \"bool\": {\r\n      \"must\": [\r\n        {\r\n          \"match\": {\r\n            \"CustomerId\": {\r\n              \"query\": \"2810855\"\r\n            }\r\n          }\r\n        }\r\n      ],\r\n      \"filter\": [\r\n        {\r\n          \"exists\": {\r\n            \"field\": \"FollowUpDateTime\"\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[-t7SwBo_TjeH_98h_56ANw][customernote][2]: RemoteTransportException[[Neophyte][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[customernote][2]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@6124d214>!]: Parse Failure [Failed to parse source [{\r\n  \"from\": 0,\r\n  \"size\": 25,\r\n  \"sort\": [\r\n    {\r\n      \"NoteDate\": {\r\n        \"order\": \"desc\"\r\n      }\r\n    }\r\n  ],\r\n  \"query\": {\r\n    \"bool\": {\r\n      \"must\": [\r\n        {\r\n          \"match\": {\r\n            \"CustomerId\": {\r\n              \"query\": \"2810855\"\r\n            }\r\n          }\r\n        }\r\n      ],\r\n      \"filter\": [\r\n        {\r\n          \"exists\": {\r\n            \"field\": \"FollowUpDateTime\"\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[TOOugCq3ROqF9CGnHIay2g][customernote][3]: SearchParseException[[customernote][3]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@f884425>!]: Parse Failure [Failed to parse source [{\r\n  \"from\": 0,\r\n  \"size\": 25,\r\n  \"sort\": [\r\n    {\r\n      \"NoteDate\": {\r\n        \"order\": \"desc\"\r\n      }\r\n    }\r\n  ],\r\n  \"query\": {\r\n    \"bool\": {\r\n      \"must\": [\r\n        {\r\n          \"match\": {\r\n            \"CustomerId\": {\r\n              \"query\": \"2810855\"\r\n            }\r\n          }\r\n        }\r\n      ],\r\n      \"filter\": [\r\n        {\r\n          \"exists\": {\r\n            \"field\": \"FollowUpDateTime\"\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }{[-t7SwBo_TjeH_98h_56ANw][customernote][4]: RemoteTransportException[[Neophyte][inet[/x.x.x.x:y]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[customernote][4]: from[0],size[25],sort[<custom:\"NoteDate\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@77228f64>!]: Parse Failure [Failed to parse source [{\r\n  \"from\": 0,\r\n  \"size\": 25,\r\n  \"sort\": [\r\n    {\r\n      \"NoteDate\": {\r\n        \"order\": \"desc\"\r\n      }\r\n    }\r\n  ],\r\n  \"query\": {\r\n    \"bool\": {\r\n      \"must\": [\r\n        {\r\n          \"match\": {\r\n            \"CustomerId\": {\r\n              \"query\": \"2810855\"\r\n            }\r\n          }\r\n        }\r\n      ],\r\n      \"filter\": [\r\n        {\r\n          \"exists\": {\r\n            \"field\": \"FollowUpDateTime\"\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}]]]; nested: QueryParsingException[[customernote] bool query does not support [filter]]; }]",
      "status" : 400
    }
    

    基本相同的错误

    嵌套:QueryParsingException [[customernote] bool查询不支持[filter]];

    最佳答案

    显然,我们在.NET 4.0项目中有以下引用

  • Elasticsearch.Net-官方低级Elasticsearch客户
    包\ Elasticsearch.Net.2.3.2 \ lib \ net45 \ Elasticsearch.Net.dll
  • NEST-官方高级Elasticsearch客户
    包\ NEST.2.3.2 \ lib \ net45 \ Nest.dll

  • 有趣的是,他们仍然使用不推荐使用的查询。正如弗雷德里克在这里提到的

    the client you're using but the server is 1.5.2 (since that is the only version provided by the aws elasticsearch service, which you appear to be using)



    见下文-它的工作原理:
                var query = new QueryContainer[8];//Size is based on hte number of search parameters
                var descriptor = new QueryContainerDescriptor<EsNoteModel>();
    
    /*........... skipping here some code for other parameters ..........*/
    
     if (hasFollowUpDate.HasValue)
                {
                    if ((bool) hasFollowUpDate)
                    {
                        //If true shows only with a non-blank follow-up date
                        query[7] = descriptor.Filtered(p => p.Filter(f => f.Exists(r => r.Field(u => u.FollowUpDateTime))));
                    }
                    else
                    {
                        ////If false shows only notes with a blank follow-up date
                        query[7] = descriptor.Filtered(p => p.Filter(f => f.Missing(r => r.Field(u => u.FollowUpDateTime))));
                    }
                }
    
                var result = ElasticSearchClient.Search<EsNoteModel>(body => body
                    .From(offset - 1)
                    .Size(rows)
                    .Query(q => q
                        .Bool(b => b
                              .Must(query)
                              )
                            )
    
                    .Sort(s => s
                        .Field(f => f
                            .Field(p => p.NoteDate)
                            .Order(SortOrder.Descending)))
    
                    );
    

    因此,只需将单词BOOL替换为Filtered-就可以了

    关于amazon-web-services - ElasticSearch NEST QueryParsingException bool查询不支持过​​滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37753691/

    相关文章:

    amazon-web-services - 如何在 Cloudformation 的 TemplateBody 部分传递变量

    c# - 索引= FieldIndexOption.No与OptOut = true?

    elasticsearch - 用于自定义日志的Logstash筛选器Grok

    elasticsearch - 为什么此Elasticsearch查询不返回任何内容

    elasticsearch - default_search_timeout 在 elasticsearch 上的行为

    c# - Elasticsearch 错误 "failed to find nested object under path"

    elasticsearch - 如何使用通配符从 Elasticsearch 查询字符串中找到真​​实分数?

    amazon-web-services - 如何从 CloudFormation 创建返回固定响应的 AWS LB 监听器?

    amazon-web-services - 如何在 Elastic Beanstalk Multicontainer docker 上使用 fluentd 日志驱动程序

    python - cron 事件的 chalice @app.schedule 语法是什么?