在Elasticsearch中提升字段前缀匹配

Tom*_*ris 5 lucene elasticsearch

有没有办法在字段后面的术语匹配中提高前缀字段匹配的分数?大多数Elasticsearch/Lucene文档似乎都关注术语而不是字段.

例如,在搜索时femal*,我希望Female排名高于Microscopic examination of specimen from female.有没有办法在查询方面这样做,或者我需要做一些事情,比如创建一个由第一个单词组成的单独字段?

Ale*_*vik 7

要做到这一点,你可以使用一个bool -query与a should来衡量span_first -query,而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"
                        }
                    }
                }
            ]
        }
    }
}
'
Run Code Online (Sandbox Code Playgroud)