如何在 Elasticsearch 中将这些词分开写入的数据中按一起写的词进行搜索?
How to search by words written together among data where these words are written apart in Elasticsearch?
我有文档,比方说,该文档的 1 个字段 - name。名称可以由几个分开书写的单词组成,例如:
{
"name": "first document"
},
{
"name": "second document"
}
我的目标是能够按字符串搜索这些文档:
firstdocument, seconddocumen
如您所见,搜索字符串写错了,但如果我们从文档名称中删除空格,它们仍然与这些文档匹配。这个问题可以通过创建另一个具有相同字符串但没有空格的字段来解决,但它看起来像是额外的数据,除非没有其他方法可以做到这一点。
我需要类似这样的东西:
GET /_analyze
{
"tokenizer": "whitespace",
"filter": [
{
"type":"shingle",
"max_shingle_size":3,
"min_shingle_size":2,
"output_unigrams":"true",
"token_separator": ""
}
],
"text": "first document"
}
反之亦然。我需要将其应用于搜索文本而不是搜索对象(文档名称),这样我就可以在搜索文本中找到拼写错误的文档。应该怎么做?
我建议使用 multi-fields 和分析器来删除空格。
分析仪
"no_spaces": {
"filter": [
"lowercase"
],
"char_filter": [
"remove_spaces"
],
"tokenizer": "standard"
}
字符过滤器
"remove_spaces": {
"type": "pattern_replace",
"pattern": "[ ]",
"replacement": ""
}
字段映射
"name": {
"type": "text",
"fields": {
"without_spaces": {
"type": "text",
"analyzer": "no_spaces"
}
}
}
查询
GET /_search
{
"query": {
"match": {
"name.without_spaces": {
"query": "seconddocumen",
"fuzziness": "AUTO"
}
}
}
}
编辑:
补全: remove_spaces
过滤器的替代方法可能是 shingle 过滤器:
"analysis": {
"filter": {
"shingle_filter": {
"type": "shingle",
"output_unigrams": "false",
"token_separator": ""
}
},
"analyzer": {
"shingle_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"shingle_filter"
]
}
}
}
我有文档,比方说,该文档的 1 个字段 - name。名称可以由几个分开书写的单词组成,例如:
{
"name": "first document"
},
{
"name": "second document"
}
我的目标是能够按字符串搜索这些文档:
firstdocument, seconddocumen
如您所见,搜索字符串写错了,但如果我们从文档名称中删除空格,它们仍然与这些文档匹配。这个问题可以通过创建另一个具有相同字符串但没有空格的字段来解决,但它看起来像是额外的数据,除非没有其他方法可以做到这一点。
我需要类似这样的东西:
GET /_analyze
{
"tokenizer": "whitespace",
"filter": [
{
"type":"shingle",
"max_shingle_size":3,
"min_shingle_size":2,
"output_unigrams":"true",
"token_separator": ""
}
],
"text": "first document"
}
反之亦然。我需要将其应用于搜索文本而不是搜索对象(文档名称),这样我就可以在搜索文本中找到拼写错误的文档。应该怎么做?
我建议使用 multi-fields 和分析器来删除空格。
分析仪
"no_spaces": {
"filter": [
"lowercase"
],
"char_filter": [
"remove_spaces"
],
"tokenizer": "standard"
}
字符过滤器
"remove_spaces": {
"type": "pattern_replace",
"pattern": "[ ]",
"replacement": ""
}
字段映射
"name": {
"type": "text",
"fields": {
"without_spaces": {
"type": "text",
"analyzer": "no_spaces"
}
}
}
查询
GET /_search
{
"query": {
"match": {
"name.without_spaces": {
"query": "seconddocumen",
"fuzziness": "AUTO"
}
}
}
}
编辑:
补全: remove_spaces
过滤器的替代方法可能是 shingle 过滤器:
"analysis": {
"filter": {
"shingle_filter": {
"type": "shingle",
"output_unigrams": "false",
"token_separator": ""
}
},
"analyzer": {
"shingle_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"shingle_filter"
]
}
}
}