Search code examples
elasticsearchanalyzerelasticsearch-mapping

elasticsearch mapping analyzer - GET not getting result


I am trying to create an analyzer, which replaces special character with a whitespace and convert it into uppercase. then after, if I want to search with lowercase also it should work.

Mapping Analyzer:

soundarya@soundarya-VirtualBox:~/Downloads/elasticsearch-2.4.0/bin$ curl -XPUT 'http://localhost:9200/aida' -d '{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "char_filter": [
            "my_char_filter"
          ],
          "filter": [
            "uppercase"
            ]
        }
      },
      "char_filter": {
        "my_char_filter": {
          "type": "pattern_replace",
          "pattern": "(\\d+)-(?=\\d)",
          "replacement": "$1 "
        }
      }
    }
  }
}
'
{"acknowledged":true}


soundarya@soundarya-VirtualBox:~/Downloads/elasticsearch-2.4.0/bin$ curl -XPOST 'http://localhost:9200/aida/_analyze?pretty' -d '{
"analyzer":"my_analyzer",
"text":"My name is Soun*arya?jwnne&yuuk"
}'

It is tokenizing the words properly by replacing the special character with the whitespace. Now if I search a word from the text, it is not retrieving me any result.

soundarya@soundarya-VirtualBox:~/Downloads/elasticsearch-2.4.0/bin$ curl -XGET 'http://localhost:9200/aida/_search' -d '{
"query":{
"match":{
"text":"My"
}
}
}'

I am not getting any result out of the above GET query. Getting result like :

soundarya@soundarya-VirtualBox:~/Downloads/elasticsearch-2.4.0/bin$ curl -XGET 'http://localhost:9200/aida/_search' -d '{
"query":{
"match":{
"text":"my"
}
}
}'
{"took":5,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

Can anyone help me with this! Thank you!


Solution

  • You don't seem to have indexed any data after creating your index. The call to _analyze will not index anything but simply show you how the content you send to ES would be analyzed.

    First, you need to create your index by specifying a mapping in which you use the analyzer you've defined:

    curl -XPUT 'http://localhost:9200/aida' -d '{
      "settings": {
        "analysis": {
          "analyzer": {
            "my_analyzer": {
              "tokenizer": "standard",
              "char_filter": [
                "my_char_filter"
              ],
              "filter": [
                "uppercase"
                ]
            }
          },
          "char_filter": {
            "my_char_filter": {
              "type": "pattern_replace",
              "pattern": "(\\d+)-(?=\\d)",
              "replacement": "$1 "
            }
          }
        }
      },
      "mappings": {                        <--- add a mapping type...
        "doc": {
          "properties": {
            "text": {                      <--- ...with a field...
              "type": "string",
              "analyzer": "my_analyzer"    <--- ...using your analyzer
            }
          }
        }
      }
    }'
    

    Then you can index a new real document:

    curl -XPOST 'http://localhost:9200/aida/doc' -d '{
       "text": "My name is Soun*arya?jwnne&yuuk"
    }'
    

    Finally, you can search:

    curl -XGET 'http://localhost:9200/aida/_search' -d '{
      "query":{
        "match":{
          "text":"My"
        }
      }
    }'