[英]Boosting elasticsearch results with NEST when a secondary field is a specific value
[英]Query with Nest field boosting returning no results from Elasticsearch
我在使用字段增強與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)
);
我意識到這段代碼的結構可能更好,但是現在我只想讓字段提升查詢正常工作-我待會兒整理一下。
這是我嘗試過的一些方法:
Nest文檔對是否可以使用帶有屬性名稱的OnFieldsWithBoost保持沉默。 即可以嗎?
.OnFieldsWithBoost(d => d .Add(“ businessName”,Math.Pow(2,13))
反對呢?
.OnFieldsWithBoost(d => d
.Add(m => m.businessName, Math.Pow(2, 13))
我問的原因是我有一些要提升集合中的子屬性。 例如, opportunities.opportunityLocatorId
。 機會顯然是集合,我想匹配該集合中任何對象具有其opportunityLocatorId
字段都具有匹配值的位置。
這適用於字段-您可以使用lambda或字符串-但它可以與boosting一起使用嗎?
不知道,但是我已經嘗試了兩種方法,將查詢的范圍縮小到只包含一個對businessName
的增強,因為這是應該與字符串“ test”匹配的字段,但仍然沒有結果返回。
我還嘗試擺脫.Negative
子句,以防萬一它匹配了不該匹配的東西。 在.Positive
子句中列出的任何字段中都找不到匹配項的情況下,可以消除任何查詢。 仍然沒有結果。
我還將.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":[]}
} ]
}
}
我在.NET 4.5.1上的Windows 7上使用Elasticsearch 1.7.1和Nest 1.7.1(是的,我知道,但這是客戶端使用的)。
我也嘗試捕獲Web API和elasticsearch之間的流量,但無濟於事。 可能是配置問題,但是Fiddler或Wireshark / npcap都無法捕獲這兩者都在本地計算機上運行的流量,因此我看不到實際的請求被發送到elasticsearch,我懷疑這會有所幫助。 基本上,我想知道是否有任何錯誤從Elasticsearch回來,表明Nest被吞噬了。
好吧...直覺是正確的。 這是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 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集合靜默返回,並且該集合上沒有任何屬性表明發生了問題。
非常感謝任何幫助。
謝謝!
巴特
自定義分數查詢在Elasticsearch 0.90.4中已棄用,在Elasticsearch 1.x中已刪除。 保留在NEST中以實現向后兼容性。 相反,您應該使用功能得分查詢 。
但是NEST應該已經通過IsValid
屬性指示發生了錯誤,在這種情況下應該為false
。 默認情況下,NEST 1.x不會引發Elasticsearch異常。 您可以通過在ConnectionSettings
上設置ThrowOnElasticsearchServerExceptions()
來啟用此行為。
旁注:在術語的開頭(例如*test
)使用通配符通常是不好的做法,因為這將導致檢查索引中的每個單個術語。 您可能需要研究修改映射,並使用類似nGram令牌生成器的方法。
事實證明,我要做的事情很簡單,我只是在錯誤的兔子洞里消失了一段時間。 例如,這是我應用了字段增強的multi_match
查詢:
curl -XGET http://localhost:9200/merchantv2/_search -d '
{
"query": {
"multi_match": {
"query": "test",
"type": "phrase_prefix",
"fields" : ["businessName^3", "address.streetName"]
}
}
}'
在這種情況下,我增強了businessName
字段,以使在其中找到的匹配項比在address.streetName
中找到的匹配項重要三倍。 似乎工作正常。
以下是相關文檔的鏈接: https : //www.elastic.co/guide/zh-cn/elasticsearch/reference/1.7/query-dsl-multi-match-query.html (為此,他建議使用Val)一個不同的問題)。
感謝您的指點!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.