在Elasticsearch中提升字段前缀匹配

时间:2013-12-23 17:51:40

标签: lucene elasticsearch

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

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

1 个答案:

答案 0 :(得分:7)

要做到这一点,你可以例如使用bool - should查询来衡量span_first - 查询,而后者又有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"
                        }
                    }
                }
            ]
        }
    }
}
'