Search code examples
elasticsearchfull-text-searchanalyzercamelcasing

CamelCase Search with Elasticsearch


I want to configure Elasticsearch, so that searching for "JaFNam" will create a good score for "JavaFileName".

I'm tried to build an analyzer, that combines a CamelCase pattern analyzer with an edge_ngram tokenizer. I thought this would create terms like these:

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

But the tokenizer seems not to have any effect: I keep getting these terms:

Java File Name

What would the correct Elasticsearch configuration look like?


Example code:

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"
}'

results in:

{
  "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
  } ]
}

Solution

  • You analyzer definition is not correct. you need a tokenizer and an array of filter, as it is your analyzer doesn't work. Try like this instead:

    {
      "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}]])"
            }
          }
        }
      }
    }