[英]Boosting field prefix match in Elasticsearch
有沒有辦法在字段后面的術語匹配中提高前綴字段匹配的分數? 大多數Elasticsearch / Lucene文檔似乎都關注術語而不是字段。
例如,當搜索femal*
,我希望Female
等級高於Microscopic examination of specimen from female
。 有沒有辦法在查詢方面這樣做,或者我需要做一些事情,比如創建一個由第一個單詞組成的單獨字段?
要做到這一點,你可以使用一個bool -query和一個should
在span_first -query中加權的查詢,而后者又有一個span_multi
這是一個可以玩的可運行的例子: https : //www.found.no/play/gist/8107157
#!/bin/bash
export ELASTICSEARCH_ENDPOINT="http://localhost:9200"
# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"type"}}
{"title":"Female"}
{"index":{"_index":"play","_type":"type"}}
{"title":"Female specimen"}
{"index":{"_index":"play","_type":"type"}}
{"title":"Microscopic examination of specimen from female"}
'
# Do searches
# This will match all documents.
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"query": {
"prefix": {
"title": {
"prefix": "femal"
}
}
}
}
'
# This matches only the two first documents.
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"query": {
"span_first": {
"end": 1,
"match": {
"span_multi": {
"match": {
"prefix": {
"title": {
"prefix": "femal"
}
}
}
}
}
}
}
}
'
# This matches all, but prefers the one's with a prefix match.
# It's sufficient that either of these match, but prefer that both matches.
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"query": {
"bool": {
"should": [
{
"span_first": {
"end": 1,
"match": {
"span_multi": {
"match": {
"prefix": {
"title": {
"prefix": "femal"
}
}
}
}
}
}
},
{
"match": {
"title": {
"query": "femal"
}
}
}
]
}
}
}
'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.