【问题标题】:Elastic search edge_ngram match query on _all being ignoredElasticsearch edge_ngram 匹配查询所有被忽略
【发布时间】:2016-05-06 23:28:51
【问题描述】:

我正在使用弹性搜索number: "1.5.2",并且正在尝试实现 edge_ngram 自动完成搜索。我有以下映射:

curl -XPUT 'localhost:8080/users' -d '{
    "settings": {
        "analysis": {
            "filter": {
                "edge_ngram_filter": {
                    "type": "edge_ngram",
                    "min_gram": 2,
                    "max_gram": 10
                }
            },
            "analyzer": {
                "edge_ngram_analyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase",
                        "asciifolding",
                        "edge_ngram_filter"
                    ]
                },
                "whitespace_analyzer": {
                    "type": "custom",
                    "tokenizer": "whitespace",
                    "filter": [
                        "lowercase",
                        "asciifolding"
                    ]
                }
            }
        },
        "mappings": {
            "user": {
                "_all": {
                    "type":"string",
                    "index_analyzer": "edge_ngram_analyzer",
                    "search_analyzer": "whitespace_analyzer"
                },
                "properties": {
                    "id":{
                        "type": "integer",
                        "index": "no",
                        "include_in_all":false
                    },
                    "email": {
                        "type": "string"
                    },
                    "firstName": {
                        "type": "string"
                    },
                    "lastName": {
                        "type": "string"
                    }
                }
            }
        }
    }
}'

然后我索引一个“用户”文档:

curl -XPUT 'localhost:8080/users/user/1' -d '{
    "email": "a.smith@gmail.com",
    "firstName": "Alexander",
    "lastName": "Smith"
}'

当我运行以下查询时,没有返回任何内容:

curl -XGET 'localhost:8080/users/_search' -d '{
    "query": {
        "match":{
            "_all":{
                "query": "ale",
                "operator":"and"
            }
        }
    }
}'

为什么_all match查询在用户文档上不匹配?

【问题讨论】:

    标签: elasticsearch autocomplete


    【解决方案1】:

    您可以通过 edge_ngram 实现自动完成的功能,而无需覆盖_all 字段分析。这是通过将您定义的分析器的名称更改为 default_indexdefault_search 来完成的(如果需要,您可以 alias 它们以反映您的原始名称(“edge_ngram_analyzer”和“whitespace_analyzer”))。这是您的配置以及相关更改:

    curl -XPUT 'localhost:8080/users' -d '{
        "settings": {
            "analysis": {
                "filter": {
                    "edge_ngram_filter": {
                        "type": "edge_ngram",
                        "min_gram": 2,
                        "max_gram": 10
                    }
                },
                "analyzer": {
                    "default_index": {
                        "type": "custom",
                        "tokenizer": "standard",
                        "filter": [
                            "lowercase",
                            "asciifolding",
                            "edge_ngram_filter"
                        ]
                    },
                    "default_search": {
                        "type": "custom",
                        "tokenizer": "whitespace",
                        "filter": [
                            "lowercase",
                            "asciifolding"
                        ]
                    }
                }
            },
            "mappings": {
                "user": {
                    "properties": {
                        "id":{
                            "type": "integer",
                            "index": "no",
                            "include_in_all":false
                        },
                        "email": {
                            "type": "string"
                        },
                        "firstName": {
                            "type": "string"
                        },
                        "lastName": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    }'

    希望我能提供帮助:)

    【讨论】:

    • 这很好用。简单而优雅的解决方案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 2013-04-13
    • 2013-09-04
    • 2014-11-20
    相关资源
    最近更新 更多