当前位置: 首页 > 知识库问答 >
问题:

带有嵌套字段增强的查询不返回Elasticsearch的结果

姜晨
2023-03-14

我遇到了一些实际的问题,比如使用字段增强来处理ElasticSearch。我已经看过关于这个主题的Nest文档,但它们没有特别大的帮助,所以我的代码实际上是基于这个问题的解决方案:使用Nest Field Boosting进行弹性搜索。

如果运行以下查询,我会得到一个结果,正如预期的那样:

var matches =
    _client.Search<SearchableMerchant>(
        s => s.From((page - 1) * pageSize)
            .Size(pageSize)
            .QueryString("*test*")
            .MinScore(1)
    );

然而,如果我尝试使用场增强,使用以下方法,我没有得到匹配:

var matches =
            _client.Search<SearchableMerchant>(
                s => s.From((page - 1) * pageSize)
                    .Size(pageSize)
                    .Query(q => q
                        .Boosting(bq => bq
                            .Positive(pq => pq
                                .CustomScore(cbf => cbf
                                    .Query(cbfq => cbfq
                                        .QueryString(
                                            qs => qs
                                            .OnFieldsWithBoost(d => d
                                                .Add("opportunities.acquirerLocationMID", Math.Pow(2, 17))
                                                .Add("opportunities.amexMID", Math.Pow(2, 16))
                                                .Add("opportunities.epayMID", Math.Pow(2, 16))
                                                .Add("v1MerchantId", Math.Pow(2, 16))
                                                .Add("locatorId", Math.Pow(2, 15))
                                                .Add("opportunities.opportunityLocatorId", Math.Pow(2, 14))
                                                .Add("businessName", Math.Pow(2, 13))
                                                .Add("searchablePhone", Math.Pow(2, 12))
                                                .Add("address.postCodeDetails.postCode.postCode", Math.Pow(2, 11))
                                                .Add("contacts.contact.searchableEmailAddress", Math.Pow(2, 11))
                                                .Add("contacts.contact.searchableMainPhone", Math.Pow(2, 10))
                                                .Add("contacts.contact.searchableMobilePhone", Math.Pow(2, 10))
                                                .Add("contacts.contact.fullName", Math.Pow(2, 9))
                                                .Add("contacts.contact.surname", Math.Pow(2, 8))
                                                .Add("contacts.contact.firstName", Math.Pow(2, 7))
                                                .Add("searchableAddress", Math.Pow(2, 6))
                                                .Add("ownershipUser.username", Math.Pow(2, 5))
                                                .Add("ownershipUser.searchableFullName", Math.Pow(2, 4))
                                                .Add("ownershipUser.lastName", Math.Pow(2, 3))
                                                .Add("ownershipUser.firstName", Math.Pow(2, 2))
                                                .Add("opportunities.depositAccount", Math.Pow(2, 1))
                                                .Add("opportunities.depositIban", Math.Pow(2, 1))
                                                .Add("opportunities.feesAccount", Math.Pow(2, 1))
                                                .Add("opportunities.feesIban", Math.Pow(2, 1))
                                                //  TODO: Company registration number - somewhere in legal methinks
                                            )
                                            .Query(
                                                "*test*"
                                            )
                                        )
                                    )
                                )
                            )
                            .Negative(nq => nq
                                .Filtered(nfq => nfq
                                    .Query(qq => qq.MatchAll())
                                    .Filter(f =>
                                        f.Missing("opportunities.acquirerLocationMID")
                                        && f.Missing("opportunities.amexMID")
                                        && f.Missing("opportunities.epayMID")
                                        && f.Missing("v1MerchantId")
                                        && f.Missing("locatorId")
                                        && f.Missing("opportunities.opportunityLocatorId")
                                        && f.Missing("businessName")
                                        && f.Missing("searchablePhone")
                                        && f.Missing("address.postCodeDetails.postCode.postCode")
                                        && f.Missing("contacts.contact.searchableEmailAddress")
                                        && f.Missing("contacts.contact.searchableMainPhone")
                                        && f.Missing("contacts.contact.searchableMobilePhone")
                                        && f.Missing("contacts.contact.fullName")
                                        && f.Missing("contacts.contact.surname")
                                        && f.Missing("contacts.contact.firstName")
                                        && f.Missing("searchableAddress")
                                        && f.Missing("ownershipUser.username")
                                        && f.Missing("ownershipUser.searchableFullName")
                                        && f.Missing("ownershipUser.lastName")
                                        && f.Missing("ownershipUser.firstName")
                                        && f.Missing("opportunities.depositAccount")
                                        && f.Missing("opportunities.depositIban")
                                        && f.Missing("opportunities.feesAccount")
                                        && f.Missing("opportunities.feesIban")
                                    )
                                )
                            )
                            .NegativeBoost(0.01)
                        )
                    )
                    .MinScore(1)
            );

.OnFieldsWithBoost(d=>d.add(“businessName”,Math.pow(2,13))

与此相反?

.OnFieldsWithBoost(d => d
    .Add(m => m.businessName, Math.Pow(2, 13))

我问的原因是我有一些子属性,我想在集合中增加。例如,opportunities.opportunityLocatorId。Opportunities显然是集合,我希望匹配集合中任何对象的opportunityLocatorId字段具有匹配值的位置。

我还将.negativeboost值提高到1(即,没有影响,所以任何结果都不应该被过滤到低于1的分数,因为开始的分数不是那么低),但同样,没有骰子。

下面是我的索引的内容,这样您就可以看到businessname字段应该与第二个查询匹配'test',就像它与第一个查询一样:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "merchantv2",
      "_type" : "searchablemerchant",
      "_id" : "00000000-0000-0000-0000-000000000000",
      "_score" : 1.0,
      "_source":{"merchantGuid":"00000000-0000-0000-0000-000000000000","v1MerchantId":0,"locatorId":"0","address":{"addressGuid":"00000000-0000-0000-0000-000000000000","postCodeDetails":{"postCodeKey":0,"postalDistrict":{"postalDistrictKey":0,"postalDistrict":""},"postalLocation":"0","latitude":0.0,"longitude":0.0,"townName":"None","countyKey":0,"countryKey":0,"postCode":{"postCodeKey":0,"postCode":" 0"}},"county":{"countyKey":0,"countyName":"","countryKey":0,"recStatus":3,"countryKeyValue":0},"countryKey":0,"addressTypeKey":0,"updateDate":"0001-01-01T00:00:00+00:00","createdDate":"2016-01-07T19:46:28.4463+00:00"},"searchableAddress":" 0","searchablePhone":"","searchableFax":"","businessName":"","contacts":[],"opportunities":[{"opportunityGuid":"00000000-0000-0000-0000-000000000000","merchantGuid":"00000000-0000-0000-0000-000000000000","location":{"locationGuid":"00000000-0000-0000-0000-000000000000","tradingAddress":{"verified":false,"addressGuid":"00000000-0000-0000-0000-000000000000","postCodeDetails":{"postCodeKey":0,"postalDistrict":{"postalDistrictKey":0,"postalDistrict":""},"postalLocation":"0","latitude":0.0,"longitude":0.0,"townName":"None","countyKey":0,"countryKey":0,"postCode":{"postCodeKey":0,"postCode":" 0"}},"county":{"countyKey":0,"countyName":"","countryKey":0,"recStatus":3,"countryKeyValue":0},"countryKey":0,"addressTypeKey":0,"updateDate":"0001-01-01T00:00:00+00:00","createdDate":"2016-01-07T19:46:28.4463+00:00"}},"opportunityLocatorId":"000000"}]}
    }, {
      "_index" : "merchantv2",
      "_type" : "searchablemerchant",
      "_id" : "5f55fe61-ca65-e411-93f3-0cc47a07ef4a",
      "_score" : 1.0,
      "_source":{"merchantGuid":"5f55fe61-ca65-e411-93f3-0cc47a07ef4a","locatorId":"PM227Z02","address":{"addressGuid":"5c55fe61-ca65-e411-93f3-0cc47a07ef4a","houseNumber":"242","streetName":"Acklam Road","houseName":"","flatAptSuite":"","townName":"London","postCodeDetails":{"postCodeKey":1,"postalDistrict":{"postalDistrictKey":2782,"postalDistrict":"W10"},"postalLocation":"5JJ","latitude":51.52094651,"longitude":-0.20149990,"townName":"London","countyKey":0,"countryKey":224,"postCode":{"postCodeKey":1,"postCode":"W10 5JJ"}},"county":{"countyKey":626,"countyName":"Kensington And Chelsea","countryKey":224,"recStatus":1,"countryKeyValue":224},"countryKey":224,"addressTypeKey":0,"updateDate":"0001-01-01T00:00:00+00:00","createdDate":"2016-01-07T19:46:28.4653+00:00"},"searchableAddress":"242 Acklam Road, London, Kensington And Chelsea, W10 5JJ","searchablePhone":"+44 2031954484","searchableFax":"","businessName":"Test Merchant","contacts":[],"opportunities":[]}
    } ]
  }
}

我使用的是Windows7上的Elasticsearch 1.7.1和Nest 1.7.1(是的,我知道,但这是客户端使用的)和.NET 4.5.1。

嗯...直觉结果是正确的。下面是elasticsearch日志文件中出现的示例:

[2016-01-08 10:14:01,534][DEBUG][action.search.type       ] [Rocket Racer] All shards failed for phase: [query]
org.elasticsearch.search.SearchParseException: [user][4]: from[0],size[20]: Parse Failure [Failed to parse source [{
  "from": 0,
  "size": 20,
  "min_score": 1.0,
  "query": {
    "boosting": {
      "positive": {
        "custom_score": {
          "query": {
            "query_string": {
              "query": "*test*",
              "fields": [
                "opportunities.acquirerLocationMID^131072",
                "opportunities.amexMID^65536",
                "opportunities.epayMID^65536",
                "v1MerchantId^65536",
                "locatorId^32768",
                "opportunities.opportunityLocatorId^16384",
                "businessName^8192",
                "searchablePhone^4096",
                "address.postCodeDetails.postCode.postCode^2048",
                "contacts.contact.searchableEmailAddress^2048",
                "contacts.contact.searchableMainPhone^1024",
                "contacts.contact.searchableMobilePhone^1024",
                "contacts.contact.fullName^512",
                "contacts.contact.surname^256",
                "contacts.contact.firstName^128",
                "searchableAddress^64",
                "ownershipUser.username^32",
                "ownershipUser.searchableFullName^16",
                "ownershipUser.lastName^8",
                "ownershipUser.firstName^4",
                "opportunities.depositAccount^2",
                "opportunities.depositIban^2",
                "opportunities.feesAccount^2",
                "opportunities.feesIban^2"
              ]
            }
          }
        }
      },
      "negative": {
        "filtered": {
          "query": {
            "match_all": {}
          },
          "filter": {
            "bool": {
              "must": [
                {
                  "missing": {
                    "field": "opportunities.acquirerLocationMID"
                  }
                },
                {
                  "missing": {
                    "field": "opportunities.amexMID"
                  }
                },
                {
                  "missing": {
                    "field": "opportunities.epayMID"
                  }
                },
                {
                  "missing": {
                    "field": "v1MerchantId"
                  }
                },
                {
                  "missing": {
                    "field": "locatorId"
                  }
                },
                {
                  "missing": {
                    "field": "opportunities.opportunityLocatorId"
                  }
                },
                {
                  "missing": {
                    "field": "businessName"
                  }
                },
                {
                  "missing": {
                    "field": "searchablePhone"
                  }
                },
                {
                  "missing": {
                    "field": "address.postCodeDetails.postCode.postCode"
                  }
                },
                {
                  "missing": {
                    "field": "contacts.contact.searchableEmailAddress"
                  }
                },
                {
                  "missing": {
                    "field": "contacts.contact.searchableMainPhone"
                  }
                },
                {
                  "missing": {
                    "field": "contacts.contact.searchableMobilePhone"
                  }
                },
                {
                  "missing": {
                    "field": "contacts.contact.fullName"
                  }
                },
                {
                  "missing": {
                    "field": "contacts.contact.surname"
                  }
                },
                {
                  "missing": {
                    "field": "contacts.contact.firstName"
                  }
                },
                {
                  "missing": {
                    "field": "searchableAddress"
                  }
                },
                {
                  "missing": {
                    "field": "ownershipUser.username"
                  }
                },
                {
                  "missing": {
                    "field": "ownershipUser.searchableFullName"
                  }
                },
                {
                  "missing": {
                    "field": "ownershipUser.lastName"
                  }
                },
                {
                  "missing": {
                    "field": "ownershipUser.firstName"
                  }
                },
                {
                  "missing": {
                    "field": "opportunities.depositAccount"
                  }
                },
                {
                  "missing": {
                    "field": "opportunities.depositIban"
                  }
                },
                {
                  "missing": {
                    "field": "opportunities.feesAccount"
                  }
                },
                {
                  "missing": {
                    "field": "opportunities.feesIban"
                  }
                }
              ]
            }
          }
        }
      },
      "negative_boost": 0.01
    }
  }
}]]
    at org.elasticsearch.search.SearchService.parseSource(SearchService.java:747)
    at org.elasticsearch.search.SearchService.createContext(SearchService.java:572)
    at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:544)
    at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:306)
    at org.elasticsearch.search.action.SearchServiceTransportAction$5.call(SearchServiceTransportAction.java:231)
    at org.elasticsearch.search.action.SearchServiceTransportAction$5.call(SearchServiceTransportAction.java:228)
    at org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:559)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at html" target="_blank">java.lang.Thread.run(Unknown Source)
Caused by: org.elasticsearch.index.query.QueryParsingException: [user] No query registered for [custom_score]
    at org.elasticsearch.index.query.QueryParseContext.parseInnerQuery(QueryParseContext.java:303)
    at org.elasticsearch.index.query.BoostingQueryParser.parse(BoostingQueryParser.java:63)
    at org.elasticsearch.index.query.QueryParseContext.parseInnerQuery(QueryParseContext.java:305)
    at org.elasticsearch.index.query.IndexQueryParserService.innerParse(IndexQueryParserService.java:382)
    at org.elasticsearch.index.query.IndexQueryParserService.parse(IndexQueryParserService.java:281)
    at org.elasticsearch.index.query.IndexQueryParserService.parse(IndexQueryParserService.java:276)
    at org.elasticsearch.search.query.QueryParseElement.parse(QueryParseElement.java:33)
    at org.elasticsearch.search.SearchService.parseSource(SearchService.java:731)
    ... 9 more

那我做错了什么?有没有人知道如何修复第二个查询,这是elasticsearch显然不喜欢的?有什么办法能让那些坏人离开Nest吗?我希望有一个异常,但这并没有发生--它只是以一个空的match集合静默返回,并且集合上没有任何属性表明有问题。

感激地得到的任何帮助。

共有1个答案

梁丘霖
2023-03-14

自定义分数查询在Elasticsearch 0.90.4中已不推荐使用,在Elasticsearch 1.x中已删除。为了向后兼容,它保留在NEST中。相反,您应该使用函数score Query。

但是,NEST应该通过IsValid属性指示发生错误,在本例中,该属性应该为false。默认情况下,Nest1.x不会引发Elasticsearch异常。可以通过在ConnectionSettings上设置ThrowOnElasticSearchServerExceptions()来启用此行为。

附带说明:在术语的开头使用通配符(例如*test)通常是不好的做法,因为这会导致索引中的每个术语都被检查。您可能需要研究修改映射,并使用类似于nGram标记器的东西来代替。

 类似资料:
  • 我正在努力学习GraphQL( 我只得到一级字段,比如: 以下是我的模式: 我的解析器: 知道我做错了什么吗?

  • 问题内容: 这是我在elasticsearch中存储在索引上的数据类型。我必须找到包含主要成分牛肉(且重量小于1000)和成分-(辣椒粉且重量小于250),(橄榄油和重量小于300)以及所有其他成分类似的食谱。 索引的映射是 我的查询是 但这给了Null。有人可以帮我吗?我认为我没有正确使用嵌套查询 问题答案: 试试这个:

  • 我有以下POJO 在下面的类中,我想直接检索给定人员的嵌套字段“cars”,其名称作为参数传递,但它返回一个具有null值的列表。 但是,如果在上面的searchHits代码中,我将类更改为Person,如下所示,我将返回pojo Person,其中只填写属性“cars”(所有其他属性检索为null): 但是如何直接检索嵌套的属性车?

  • 我是Elasticsearch的新手,我提出了一个问题,Elasticsearch嵌套查询是否只能为嵌套字段返回匹配的嵌套文档。 对于示例,我有一个名为的类型,其中嵌套字段名为 和嵌套查询 我需要的是搜索有提到足球的评论的博客文章,每个博客文章的评论数与足球相匹配(在例子中它数为1,因为另一个评论刚刚提到篮球)。 然而,Elasticsearch似乎总是返回完整的文档,所以我如何才能实现它,或者我

  • 我有一个带有嵌套映射的索引。我想预先形成一个查询,该查询将返回以下内容:给我搜索词中每个单词出现在一个或多个嵌套文档中的所有文档。 以下是索引: 以下是我尝试的最新查询: 例如,如果我搜索单词“食物和水”,我希望每个单词至少出现在嵌套文档中。即使只有一个单词存在,当前搜索也会返回文档 谢谢你的帮助! 更新:正如克里斯托所建议的,解决方案有效。现在我有以下问题。 这是我的索引: 我要执行的查询是,如

  • 我正在努力编写一个具有多个和/或条件的弹性搜索查询。 它的基本逻辑如下: 正如docs中提到的,如果一个文档只满足must子句,那么它就被认为是匹配的。在这种情况下,should子句起到增强作用,即如果文档除了mouth子句之外还匹配一个或多个should子句,那么它将具有更高的相关性得分,假设cond2、cond3和cond4是计算相关性得分的查询。 问题是我只想要至少匹配OR条件之一的文档。