ElasticSearch - 使用缩写搜索

时间:2017-05-31 14:44:03

标签: elasticsearch nest

我正在尝试设置现有/自定义分析器,以便使用缩写进行搜索。例如,如果文本字段为" Bank Of America",则搜索BOfA或BOA,BofA等应匹配此记录。

我该怎么做?请帮忙。

2 个答案:

答案 0 :(得分:1)

您可以使用synonym filter令牌作为自定义分析器。

例如以下映射

{
"settings": {
    "analysis": {
        "analyzer": {
            "my_analyzer": {
                "tokenizer": "standard",
                "filter": ["lowercase", "synonym_filter"]
            }
        },
        "filter": {
            "synonym_filter": {
                "type": "synonym",
                "synonyms": [
                    "bank of america,boa"
                ],
                "expand": true
            }
        }
    }
},
"mappings": {
    "document": {
        "properties": {
            "text": {
                "type": "text",
                "analyzer": "my_analyzer",
                "fielddata": true
            }
        }
    }
}

}

当然,您可以向列表中添加更多内容或使用同义词文件。

对于查询用例BOfA或BOA,BofA - 可以使用两种方法。

1)这些可能组合的更多同义词

 "synonyms": [
                    "bank of america,boa"
                    "bank of america,bofa"
                ]

2)或保持完整的abrevations并使用模糊查询

{
  "query": {
    "match": {
      "text" : {
        "query": "bofa",
        "fuzziness": 2
      }
    }
  }
}

您需要使用synoyms为ES提供abrevations。

答案 1 :(得分:0)

我发现正在使用pattern_replace进行处理:

GET /_analyze
{
  "tokenizer": "keyword",
  "filter": [
    {
      "type": "pattern_replace",
      "pattern": "(\\B.)",
      "replacement": ""
    },
    {
      "type": "pattern_replace",
      "pattern": "(\\s)",
      "replacement": ""
    },
    "uppercase",
    {
      "type": "ngram",
      "min_gram": 3,
      "max_gram": 5
    }
  ],
  "text": "foxes jump lazy dogs"
}

产生:

{
  "tokens": [
    {
      "token": "FJL",
      "start_offset": 0,
      "end_offset": 20,
      "type": "word",
      "position": 0
    },
    {
      "token": "FJLD",
      "start_offset": 0,
      "end_offset": 20,
      "type": "word",
      "position": 0
    },
    {
      "token": "JLD",
      "start_offset": 0,
      "end_offset": 20,
      "type": "word",
      "position": 0
    }
  ]
}