查询ElasticSearch中格式错误的空子句错误

时间:2018-05-03 14:40:02

标签: elasticsearch nest elasticsearch-plugin elasticsearch-6

我在.net代码中以及Kibana升级到6.x elasticsearch时出现以下错误。虽然之前渐变警告已被记录但是它根据查询返回结果。现在没有返回任何结果。

以下是我的弹性查询:

     {
        "from": 0,
    "size": 50,
      "query": {
     "bool": {
       "must": [
       {
          "bool": {
           "must": [
              {},
              {},
              {}, 
              {},
              {}
                  ]
                }
             }
        ],
     "filter": [
         {
          "bool": {
            "must": [
             {
            "range": {
              "image_id": {
                "gte": 0
              }
            }
          },
          {
            "term": {
              "collection.name.raw": {
                "value": "Hollis, ME: Tax List of Hollis, Maine, 1862"
              }
            }
          }
        ]
         }
         }
          ]
          }
       },
       "aggs": {
        "Category": {
            "terms": {
               "field": "collection.category.name.raw",
                 "size": 1000
                     }
                   }
                 }
                }

以下是错误:

 {
   "error": {
      "root_cause": [
     {
         "type": "illegal_argument_exception",
       "reason": "query malformed, empty clause found at [94:16]"
     }
                  ],
      "type": "illegal_argument_exception",
      "reason": "query malformed, empty clause found at [94:16]"
     },
    "status": 400
     }

当用户未提供任何输入时,会在场景中生成空子句。我检查了下面的链接,但没有找到任何解决我的问题。       Link1

Link2 Link3

         sr.Query = Query<Model.Name>
                .Bool(fd => fd
                    .Must(iq => iq
                    .Bool(b => b
                        .Must(must => must

                        #region Attributes
                            //TODO: Update for nested attribute object
                                .Raw(attributesQuery)
                        #endregion

                    #region ExactLocation
     , must => must.DisMax(d => d
                                .Queries(qs => qs
                                    .Bool(ib => ib
                                        .Should(should => should
                                                .Raw(locationrawExactMatch)

       ).MinimumShouldMatch(numRawLocations)
                                    )
                                        , qs => qs.Bool(ib => ib
                                        .Must(mu => mu
                                                .Raw(locationExactAllTerms)
                                                , mu => mu.Bool(bo => bo
                                                .Should(should => should

       .Raw(locationExactDuplicates)

           ).MinimumShouldMatch(numLocations)
                                            )
                                        )
                                    )
                                )
                            )
                    #endregion

                    #region Location
           , must => must.Bool(boo => boo
                                    .Should(sh => sh
                                        .DisMax(d => d
                                            .Queries(qs => qs
                                                    .Raw(locationMultiMatch)
                                                , qs => qs.Bool(ib => ib
                                                    .Must(mu => mu

                           .Raw(locationExactMatch)
                                                    )
                                                )
                                                , qs => qs.Bool(ib => ib
                                                    .Must(should => should
                                                        .Match(m => m

                 .Field("location.city.name")

            //.Type(TextQueryType.boolean)

           .Operator(Operator.Or)

           .Query((locationCityStateCountry[0] + " " + 
             locationCityStateCountry[1]).Replace(",", "").Trim())

               .Analyzer("standard")
                                                        )
                                                        , should => 
                 should.MatchPhrasePrefix(m => m

                .Field("location.region.name")
                                                            .Query(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(locationCityStateCountry[1].ToLower().Trim()))
                                                            .Analyzer("standard")
                                                        )
                                                    ).Boost(8.0)
                                                    .Should(should => should
                                                            .MatchPhrase(m => m
                                                            .Field("location.city.name")
                                                               // .Fuzziness(Fuzziness.EditDistance(0)) //cant set 0.1!
                                                            .Query((locationCityStateCountry[0] + " " + locationCityStateCountry[1]).Replace(",", "").Trim())
                                                            .Analyzer("standard")
                                                        )
                                                        , should => should.MatchPhrase(m => m
                                                            .Field("location.city.name")
                                                               // .Fuzziness(Fuzziness.EditDistance(0)) //cant set 0.1!
                                                            .Query(locationCityStateCountry[0].Replace(",", "").Trim())
                                                            .Analyzer("standard")
                                                        )
                                                        , should => should.MatchPhrasePrefix(m => m
                                                            .Field("location.region.name")
                                                            .Query(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(locationCityStateCountry[1].ToLower().Trim()))
                                                            .Analyzer("standard")
                                                        )
                                                        , should => should.Match(m => m
                                                            .Field("location.county.name")
                                                                .Operator(Operator.Or)
                                                                .Fuzziness(Fuzziness.EditDistance(0)) //cant set 0.1!
                                                            .Query((locationCityStateCountry[1] + " " + locationCityStateCountry[2]).Replace(",", "").Trim())
                                                            .Analyzer("standard")
                                                        )
                                                        , should => should.Match(m => m
                                                            .Field("location.country.name")
                                                                .Operator(Operator.Or)
                                                                .Fuzziness(Fuzziness.EditDistance(0)) //cant set 0.1!
                                                                .Query((locationCityStateCountry[1] + " " + locationCityStateCountry[2] + locationCityStateCountry[3]).Replace(",", "").Trim())
                                                            .Analyzer("standard")
                                                        )
                                                        ).MinimumShouldMatch(Math.Max(locations.Length, 2))
                                                    .Boost(4.0)
                                                )
                                            )
                                        )
                                       , sh => sh.DisMax(di => di
                                            .Queries(qi => qi
                                                .Bool(ib => ib
                                                    .Should(should => should
                                                            .Raw(locationExactMatch)
                                                        ).MinimumShouldMatch(numLocations)
                                                )
                                                 , qi => qi.Bool(ib => ib
                                                    .Must(mu => mu
                                                            .Raw(locationExactAll)
                                                        , mu => mu.Bool(bo => bo
                                                            .Should(should => should
                                                                    .Raw(locationExactDupes)
                                                                ).MinimumShouldMatch(numLocations)
                                                        )
                                                    )
                                                )
                                            )
                                        )
                                    )
                                )

                    #endregion

                    #region Primary Name
             , must => must.MatchPhrase(m => m
                                .Field("last_name")
                                .Query(lastNameSoundex)
                                .Analyzer("my_analyzer")
                                   // .Fuzziness(Fuzziness.EditDistance(1))

                                .Boost(0.5)
                                )
                            , must => must.Bool(bo => bo
                                .Should(sh => sh
                                    // new wildcard search
                                    .DisMax(d => d
                                        .Boost(100)
                                        .Queries(qs => qs
                                                .MatchPhrase(m => m   // exact match -- highest boost
                                                .Field("last_name")
                                                .Query(lastName)
                                                .Analyzer("standard")
                                                .Boost(100.0)
                                            )
                                             , qs => qs.MatchPhrase(m => m   // exact alternate name match -- highest boost
                                                .Field("alternate_names")
                                                .Query(lastName)
                                                .Analyzer("standard")
                                                .Boost(1.0)
                                            )
                                             , qs => qs.ConstantScore(c => c
                                                    .Filter(qu => qu
                                                    .QueryString(m => m // wildcard match 
                                                        .Query(lastName)
                                                        .DefaultField("last_name")
                                                    )
                                                )
                                                .Boost(0.9)
                                            )
                                              , qs => qs.ConstantScore(c => c
                                                    .Filter(qu => qu
                                                    .QueryString(m => m // wildcard match on alt names
                                                        .Query(lastName)
                                                        .DefaultField("alternate_names")
                                                    )
                                                )
                                                .Boost(0.5)
                                            )
                                             , qs => qs.ConstantScore(c => c
                                                    .Filter(qu => qu
                                                    .Match(m => m   // fuzzy match -- least boost
                                                        .Field("last_name")
                                                        .Query(lastName)
                                                            .Fuzziness(Fuzziness.EditDistance(1))
                                                        .Analyzer("standard")
                                                        .PrefixLength(1)
                                                    )
                                                )
                                                .Boost(0.01)
                                            )
                                             , qs => qs.ConstantScore(c => c
                                                    .Filter(qu => qu
                                                    .Match(m => m   // fuzzy match on alternate names -- least boost
                                                        .Field("alternate_names")
                                                        .Query(lastName)
                                                            .Fuzziness(Fuzziness.EditDistance(1))
                                                        .Analyzer("standard")
                                                        .PrefixLength(1)
                                                    )
                                                )
                                                .Boost(0.001)
                                            )
                                        )
                                    )
                                      , sh => sh.DisMax(d => d
                                        .Boost(0.1)
                                        .Queries(qs => qs
                                                .MatchPhrase(m => m   // exact match -- highest boost
                                                .Field("first_name")
                                                .Query(firstName)
                                                .Analyzer("standard")
                                                .Boost(100.0)
                                            )
                                            , qs => qs.ConstantScore(c => c
                                                    .Filter(qu => qu
                                                    .QueryString(m => m // wildcard match 
                                                        .Query(firstName)
                                                        .DefaultField("first_name")
                                                    )
                                                )
                                                .Boost(0.1)
                                            )
                                            , qs => qs.ConstantScore(c => c
                                                    .Filter(qu => qu
                                                    .Match(m => m   // fuzzy match -- least boost
                                                        .Field("first_name")
                                                        .Query(firstName)
                                                            .Fuzziness(Fuzziness.EditDistance(1))
                                                        .Analyzer("standard")
                                                        .PrefixLength(1)
                                                    )
                                                )
                                                .Boost(0.01)
                                            )
                                        )
                                    )
                                        , sh => sh.DisMax(d => d
                                        .Queries(qs => qs
                                                .MatchPhrase(m => m   // exact
                                                .Field("last_name")
                                                    .Query(lastNameExact)
                                                   // .Fuzziness(Fuzziness.EditDistance(0))
                                                .Analyzer("standard")
                                                .Boost(100)
                                            )
                                            , qs => qs.MatchPhrase(m => m   // exact
                                                .Field("alternate_names")
                                                .Query(lastNameExact)
                                                   // .Fuzziness(Fuzziness.EditDistance(0))
                                                .Analyzer("standard")
                                                .Boost(0.1)
                                            )
                                        )
                                    )
                                        , sh => sh.DisMax(d => d
                                        .Boost(100)
                                        .Queries(qs => qs
                                            .QueryString(m => m // wildcard match 
                                                .Query(lastNameExactWildcard)
                                                .DefaultField("last_name")
                                                .Analyzer("standard")
                                                // .FuzzyMinSim(0.0)
                                                .Boost(100)
                                            )
                                            , qs => qs.QueryString(m => m // wildcard match 
                                                .Query(lastNameExactWildcard)
                                                .DefaultField("alternate_names")
                                                .Analyzer("standard")
                                                //   .FuzzyMinSim(0.0)
                                                .Boost(0.1)
                                            )
                                        )
                                    )
                                        , sh => sh.DisMax(d => d
                                        .Queries(qs => qs
                                                .MatchPhrase(m => m   // exact
                                                .Field("first_name")
                                                .Query(firstNameExact)
                                                   //.Fuzziness(Fuzziness.EditDistance(0))
                                                .Analyzer("standard")
                                                .Boost(2.0)
                                            )
                                        )
                                    )
                                        , sh => sh.DisMax(d => d
                                        .Boost(0.1)
                                        .Queries(qs => qs
                                            .QueryString(m => m // wildcard match 
                                                .Query(firstNameExactWildcard)
                                                .DefaultField("first_name")
                                                .Analyzer("standard")
                                                //      .FuzzyMinSim(0.0)
                                                .Boost(0.5)
                                                    .DefaultOperator(Operator.And)
                                            )
                                        )
                                    )
                                    ).MinimumShouldMatch(numNames)
                            )
                    #endregion

                    #region Relationships
         , must => must.Raw(getSoundex(must, relationshipsLastSoundex))
                        // new wildcard search
                           , must => must.Raw(getDisMaxName(must, 
              "relationships.last_name", relationshipsLast))
                           , must => must.Raw(getDisMaxName(must, 
               "relationships.first_name", relationshipsFirst))
                           , must => must.Raw(getDisMaxNameExact(must, 
            "relationships.first_name", relationshipsFirstExact))
                           , must => must.Raw(getDisMaxNameExact(must, 
             "relationships.last_name", relationshipsLastExact))
                    #endregion
                , must => must.Raw(keywordstring)
                            )
                        )
                    ).Should(sh => sh
                                        .Match(m => m
                                            .Field("record_type.name.raw")
                                            .Query(recordType)
                                            .Boost(100)))
                    .Filter(f => f
                        .Bool(b => b.Must(a => a
                            .Bool(bm => bm.Should(
                                sh => sh.Range(r => r
                                    .Field("start_year")
                                    .GreaterThanOrEquals(startYear)
                                    .LessThanOrEquals(endYear)
                                ),
                                sh => sh.Range(r => r
                                    .Field("end_year")
                                    .GreaterThanOrEquals(startYear)
                                    .LessThanOrEquals(endYear)
                                    )
                            )),
                            a => a.Range(r => r
                                .Field("image_id")
                                .GreaterThanOrEquals(dHasImages)
                            ),
                            a => a.Terms(t => t.Field("collection.id").Terms(freeCollectionIds)),
                            a => a.Terms(t => t.Field("collection.id").Terms(favorites)),
                            //TODO: update for nested Attribute type
                            a => a.Term(t => t.Field("volume_id").Value(volumeId)),
                            a => a.Term(t => t.Field("page_id").Value(pageId)),
                            a => a.Term(t => t.Field("page_name.raw").Value(pageName)),
                            a => a.Term(t => t.Field("collection.name.raw").Value(databaseName)),
                            a => a.Term(t => t.Field("record_type.name.raw").Value(recordTypeExact)),
                            a => a.Term(t => t.Field("collection.category.name.raw").Value(category))
                        )
                    )
                )
            );

2 个答案:

答案 0 :(得分:0)

我们只是升级到6.x,并遇到了这个问题。对我们来说,似乎只有在必须数组中时才会触发错误:

必须:[{}]#<=错误

删除其中的{},尽管在Bool节中仍然有很多{}子句,但我没有更多错误。

必须:[]#<=好

答案 1 :(得分:0)

我们只是升级到6.x,并遇到了这个问题。对我们来说,似乎只有在必须数组中时才会触发错误:

必须:[{}]#<=错误

删除其中的{},尽管在Bool节中仍然有很多{}子句,但我没有更多错误。

必须:[]#<=好

更新:空查询和过滤器也很糟糕

查询:{}

过滤器:{}