【问题标题】:Using autocomplete with email in elastic search doesn't work在弹性搜索中使用电子邮件自动完成功能不起作用
【发布时间】:2017-04-12 07:52:32
【问题描述】:

我有一个定义了以下映射的字段:

"my_field": {
    "properties": {
        "address": {
            "type": "string",
            "analyzer": "email",
            "search_analyzer": "whitespace"
        }
    }
}

我的电子邮件分析器如下所示:

{
    "analysis": {
        "filter": {
            "email_filter": {
                "type": "edge_ngram",
                "min_gram": "3",
                "max_gram": "255"
            }
        },
        "analyzer": {
            "email": {
                "type": "custom",
                "filter": [
                    "lowercase",
                    "email_filter",
                    "unique"
                ],
                "tokenizer": "uax_url_email"
            }
        }
    }
}

当我尝试搜索电子邮件 ID 时,例如 test.xyz@example.com

搜索 tes、test.xy 等术语不起作用。但如果我搜索 test.xyz 或 test.xyz@example.com,它工作正常。我尝试使用我的电子邮件过滤器分析令牌,它可以正常工作

例如。打http://localhost:9200/my_index/_analyze?analyzer=email&text=test.xyz@example.com

我明白了:

{
    "tokens": [{
        "token": "tes",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.x",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.xy",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.xyz",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.xyz@",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.xyz@e",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.xyz@ex",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.xyz@exa",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.xyz@exam",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.xyz@examp",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.xyz@exampl",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.xyz@example",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.xyz@example.",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.xyz@example.c",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.xyz@example.co",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }, {
        "token": "test.xyz@example.com",
        "start_offset": 0,
        "end_offset": 20,
        "type": "word",
        "position": 0
    }]
}

所以我知道标记化是有效的。但是在搜索时,它无法搜索部分字符串。

例如。寻找http://localhost:9200/my_index/my_field/_search?q=test,结果显示没有命中。

我的索引详情:

{
    "my_index": {
        "aliases": {
            "alias_default": {}
        },
        "mappings": {
            "my_field": {
                "properties": {
                    "address": {
                        "type": "string",
                        "analyzer": "email",
                        "search_analyzer": "whitespace"
                    },
                    "boost": {
                        "type": "long"
                    },
                    "createdat": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"
                    },
                    "instanceid": {
                        "type": "long"
                    },
                    "isdeleted": {
                        "type": "integer"
                    },
                    "object": {
                        "type": "string"
                    },
                    "objecthash": {
                        "type": "string"
                    },
                    "objectid": {
                        "type": "string"
                    },
                    "parent": {
                        "type": "short"
                    },
                    "parentid": {
                        "type": "integer"
                    },
                    "updatedat": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1480342980403",
                "number_of_replicas": "1",
                "max_result_window": "100000",
                "uuid": "OUuiTma8CA2VNtw9Og",
                "analysis": {
                    "filter": {
                        "email_filter": {
                            "type": "edge_ngram",
                            "min_gram": "3",
                            "max_gram": "255"
                        },
                        "autocomplete_filter": {
                            "type": "edge_ngram",
                            "min_gram": "3",
                            "max_gram": "20"
                        }
                    },
                    "analyzer": {
                        "autocomplete": {
                            "type": "custom",
                            "filter": [
                                "lowercase",
                                "autocomplete_filter"
                            ],
                            "tokenizer": "standard"
                        },
                        "email": {
                            "type": "custom",
                            "filter": [
                                "lowercase",
                                "email_filter",
                                "unique"
                            ],
                            "tokenizer": "uax_url_email"
                        }
                    }
                },
                "number_of_shards": "5",
                "version": {
                    "created": "2010099"
                }
            }
        },
        "warmers": {}
    }
}

【问题讨论】:

  • 对于搜索你有 "search_analyzer": "whitespace" 分析器。删除它并进行映射
  • @Backtrack 我相信这是正确的。检查stackoverflow.com/a/15932838/1465701。除非我在这里遗漏了什么,否则我认为这应该是正确的行为。
  • 您的映射中有错字,analyser 应为 analyzer
  • @val,鹰眼 :)
  • 如果解决了它应该可以工作。需要删除、重新创建索引并重新索引数据。

标签: elasticsearch


【解决方案1】:

好的,一切看起来都正确,除了您的查询。

您只需要像这样在查询中指定address 字段,它就会起作用:

http://localhost:9200/my_index/my_field/_search?q=address:test

如果您未指定 address 字段,则查询将在默认情况下搜索分析器为 standard_all 字段上运行,因此您没有找到任何内容。

【讨论】:

  • 太棒了,很高兴它有帮助!
猜你喜欢
  • 1970-01-01
  • 2017-06-14
  • 2015-03-22
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多