John Everden
John Everden

Reputation: 166

Elasticsearch 7.2 create index with mapping and custom analyzer php

I'm trying to override the elastic search analyzer so exact match emails are returned for an autocomplete I'm working on. I'm currently using the PHP library for elastic search but most of the question is in JSON as it's easier for me to work directly with JSON rather than nested PHP arrays.

I've found some old examples for previous versions I presume but have had no luck creating an index with both mapping and a custom analyzer in version 7.2

  $queryStr= <<<'EOD'
    {
       "index":"my_db",
       "body":{
          "settings":{

             "analysis":{
                "analyzer":{
                   "my_email_analyzer":{
                      "type":"custom",
                      "tokenizer":"uax_url_email",
                      "filter":[
                         "lowercase",
                         "stop"
                      ]
                   }
                }
             }

          },
          "mapping":{
             "properties":{

                "ak_first_name":{
                   "type":"text",
                   "fields":{
                      "keyword":{
                         "type":"keyword",
                         "ignore_above":256
                      }
                   }
                },
                "ak_last_name":{
                   "type":"text",
                   "fields":{
                      "keyword":{
                         "type":"keyword",
                         "ignore_above":256
                      }
                   }
                },


                "uID":{
                   "type":"text",
                   "fields":{
                      "keyword":{
                         "type":"keyword",
                         "ignore_above":256
                      }
                   }
                },
                "email":{
                   "type":"text",
                   "fields":{
                      "keyword":{
                         "type":"string",
                         "analyzer":"my_email_analyzer"
                      }
                   }
                }
             }
          }
       }
    }
    EOD;


    $this->client->indices()->create($queryStr);

Upvotes: 1

Views: 1459

Answers (1)

Val
Val

Reputation: 217554

You have two small typos in your JSON:

  • mapping should read mappings
  • string should read text

Here goes:

$queryStr= <<<'EOD'
{
   "index":"my_db",
   "body":{
      "settings":{

         "analysis":{
            "analyzer":{
               "my_email_analyzer":{
                  "type":"custom",
                  "tokenizer":"uax_url_email",
                  "filter":[
                     "lowercase",
                     "stop"
                  ]
               }
            }
         }

      },
      "mappings":{                                     <--- here
         "properties":{
           ...

            "email":{
               "type":"text",
               "fields":{
                  "keyword":{
                     "type":"text",                    <---- here
                     "analyzer":"my_email_analyzer"
                  }
               }
            }

Upvotes: 1

Related Questions