使用 Elasticsearch 进行驼峰式搜索

CamelCase Search with Elasticsearch

我想配置 Elasticsearch,这样搜索 "JaFNam" 就会为 "JavaFileName" 创造一个好的分数。

我试图构建一个分析器,它结合了 CamelCase pattern 分析器和 edge_ngram 分词器。我认为这会创建如下术语:

J F N Ja Fi Na Jav Fil Nam Java File Name

但分词器似乎没有任何作用:我不断收到这些条款:

Java File Name

正确的 Elasticsearch 配置应该是什么样的?


示例代码:

curl -XPUT    'http://127.0.0.1:9010/hello?pretty=1' -d'
{
  "settings":{
    "analysis":{
      "analyzer":{
        "camel":{
          "type":"pattern",
          "pattern":"([^\p{L}\d]+)|(?<=\D)(?=\d)|(?<=\d)(?=\D)|(?<=[\p{L}&&[^\p{Lu}]])(?=\p{Lu})|(?<=\p{Lu})(?=\p{Lu}[\p{L}&&[^\p{Lu}]])",
          "filters": ["edge_ngram"]
        }
      }
    }
  }
}
'
curl -XGET    'http://127.0.0.1:9010/hello/_analyze?pretty=1' -d'
{
  "analyzer":"camel",
  "text":"JavaFileName"
}'

结果:

{
  "tokens" : [ {
    "token" : "java",
    "start_offset" : 0,
    "end_offset" : 4,
    "type" : "word",
    "position" : 0
  }, {
    "token" : "file",
    "start_offset" : 4,
    "end_offset" : 8,
    "type" : "word",
    "position" : 1
  }, {
    "token" : "name",
    "start_offset" : 8,
    "end_offset" : 12,
    "type" : "word",
    "position" : 2
  } ]
}

你的分析器定义不正确。你需要一个 tokenizer 和一个 filter 数组,因为你的分析器不起作用。试试这样:

{
  "settings": {
    "analysis": {
      "analyzer": {
        "camel": {
          "tokenizer": "my_pattern",
          "filter": [
            "my_gram"
          ]
        }
      },
      "filter": {
        "my_gram": {
          "type": "edge_ngram",
          "max_gram": 10
        }
      },
      "tokenizer": {
        "my_pattern": {
          "type": "pattern",
          "pattern": "([^\p{L}\d]+)|(?<=\D)(?=\d)|(?<=\d)(?=\D)|(?<=[\p{L}&&[^\p{Lu}]])(?=\p{Lu})|(?<=\p{Lu})(?=\p{Lu}[\p{L}&&[^\p{Lu}]])"
        }
      }
    }
  }
}