在Elasticsearch中提升字段前缀匹配

 石某俊 发布于 2023-02-08 14:05

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

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

1 个回答
  • 要做到这一点,你可以使用一个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"
                            }
                        }
                    }
                ]
            }
        }
    }
    '
    

    2023-02-08 14:07 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有