【问题标题】:ElasticSearch NEST QueryParsingException bool query does not support filterElasticSearch NEST QueryParsingException bool 查询不支持过​​滤器
【发布时间】:2016-10-11 17:50:32
【问题描述】:

我正在使用 NEST 2.3.2 来查询 ElasticSearch,并且需要根据可选的布尔值为 FollowUpDateTime 字段创建条件过滤器。如果那个布尔值

  • 为真 - 希望查看所有具有非空白日期的记录,
  • 是假的 - 没有日期的记录,
  • 为空 - 显示所有记录。

这是我在 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)))

                        );

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

由不成功的低级别调用构建的无效 NEST 响应 POST:/_search?pretty=true

此 API 调用的审计跟踪:

  • BadResponse:节点:http://**********.es.amazonaws.com/ 使用:00:00:00.1381301

    OriginalException:System.Net.WebException:远程服务器返回错误:(400)错误请求。在

System.Net.HttpWebRequest.GetResponse() 在 Elasticsearch.Net.HttpConnection.Request[TReturn](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]];”任何想法我在这个例子中做错了什么?这是对过滤子句的正确使用吗?

**** 更新

更改查询

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

【问题讨论】:

  • 布尔查询只能有一个从 elasticsearch 2 开始的过滤器。
  • 使用 Elasticsearch.Net.2.3.2
  • 这是您正在使用的客户端,但服务器是 1.5.2(因为这是您似乎正在使用的 aws elasticsearch 服务提供的唯一版本)
  • 弗雷德里克 - 你值得一颗星!谢谢
  • NEST 的主要版本与 Elasticsearch 的主要版本兼容,因此对于 Elasticsearch 1.5.2,您应该使用最新的 NEST 1.x,目前为 1.8.2。查看 github 上的兼容性矩阵 - github.com/elastic/elasticsearch-net

标签: amazon-web-services elasticsearch nest


【解决方案1】:

显然我们在 .NET 4.0 项目中有以下对

的引用
  1. Elasticsearch.Net - 官方低级 elasticsearch 客户端 包\Elasticsearch.Net.2.3.2\lib\net45\Elasticsearch.Net.dll
  2. NEST - 官方高级弹性搜索客户端 包\NEST.2.3.2\lib\net45\Nest.dll

有趣的事实是,他们仍然使用已弃用的查询。正如弗雷德里克在这里提到的那样

您正在使用的客户端,但服务器是 1.5.2(因为那是 由 aws elasticsearch 服务提供的唯一版本,您 似乎正在使用)

见下文 - 它有效:

            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 - 成功了

【讨论】:

  • 你是如何使用nest连接aws ES的?我认为简单的帖子不起作用。它需要添加一些安全性
【解决方案2】:

您可以使用QueryContainerFilterContainer 动态执行此操作。这是我们解决方案中非常相似的一个:

var query = new List<QueryContainer>
{
    Query<AlertForReporting>.Range(r => r.OnField(f => f.AlertDate)
                                          .Greater(start.ToEPCISFormat())
                                          .Lower(end.ToEPCISFormat())
        )
};


var result = ElasticClient.Search<AlertForReporting>(s =>
                        s.Size(0)
                            .Query(q =>
                                q.Bool(b =>
                                        b.Must(query.ToArray())
                                    )
                            ));

【讨论】:

  • 我们已经将 QueryContainer 用于 DateRange、Match 和 QueryStrings 搜索的其他一些参数。 (上面更新的代码)。我们是否需要同时使用:FilterContainer 和 QueryContainer?如果是这样 - 如何?我正在寻找答案..阅读elastic.co/guide/en/elasticsearch/reference/current/…
  • @NataliaZolotareva 在 NEST 1.x 中,有 FilterContainerQueryContainer,前者用于需要过滤器的地方,后者用于需要查询的地方。在与 Elasticsearch 2.x 一起使用的 NEST 2.x 中,只有 QueryContainer 来反映 Elasticsearch 中过滤器和查询合并的变化。在 NEST 2.x 中,您使用 QueryContainer,而在 NEST 1.x 中您将使用 QueryContainerFilterContainer
猜你喜欢
  • 2016-09-11
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
  • 2016-06-06
相关资源
最近更新 更多