Elastic Search Analyzer at search time not working

  1. Create the Index with Query-Time Analyzer Only:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>PUT /local_persons
{
"settings": {
"analysis": {
"analyzer": {
"person_search_analyzer": {
"type": "custom",
"char_filter": ["remove_special_chars"],
"filter": ["lowercase"],
"tokenizer": "whitespace"
}
},
"char_filter": {
"remove_special_chars": {
"type": "pattern_replace",
"pattern": "[^a-zA-Z0-9]",
"replacement": ""
}
}
}
}
}
</code>
<code>PUT /local_persons { "settings": { "analysis": { "analyzer": { "person_search_analyzer": { "type": "custom", "char_filter": ["remove_special_chars"], "filter": ["lowercase"], "tokenizer": "whitespace" } }, "char_filter": { "remove_special_chars": { "type": "pattern_replace", "pattern": "[^a-zA-Z0-9]", "replacement": "" } } } } } </code>
PUT /local_persons
{
  "settings": {
    "analysis": {
      "analyzer": {
        "person_search_analyzer": {
          "type": "custom",
          "char_filter": ["remove_special_chars"],
          "filter": ["lowercase"],
          "tokenizer": "whitespace"
        }
      },
      "char_filter": {
        "remove_special_chars": {
          "type": "pattern_replace",
          "pattern": "[^a-zA-Z0-9]",
          "replacement": ""
        }
      }
    }
  }
}
  1. Indexed the data with special characters:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>PUT /local_persons/_doc/1
{
"id": 1,
"firstName": "Re'mo",
"lastName": "D'souza",
"email": "[email protected],
"dateOfBirth": "1973-01-01",
"isActive": 1
}
</code>
<code>PUT /local_persons/_doc/1 { "id": 1, "firstName": "Re'mo", "lastName": "D'souza", "email": "[email protected], "dateOfBirth": "1973-01-01", "isActive": 1 } </code>
PUT /local_persons/_doc/1
{
  "id": 1,
  "firstName": "Re'mo",
  "lastName": "D'souza",
  "email": "[email protected],
  "dateOfBirth": "1973-01-01",
  "isActive": 1
}

Now searching for the person at the query time:

Approach-1: Search using query_string (with analyzer at query time)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>GET /local_persons/_search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "remo",
"fields": ["firstName"],
"analyzer": "person_search_analyzer"
}
},
{
"query_string": {
"query": "dsouza",
"fields": ["lastName"],
"analyzer": "person_search_analyzer"
}
}
]
}
}
}
</code>
<code>GET /local_persons/_search { "query": { "bool": { "must": [ { "query_string": { "query": "remo", "fields": ["firstName"], "analyzer": "person_search_analyzer" } }, { "query_string": { "query": "dsouza", "fields": ["lastName"], "analyzer": "person_search_analyzer" } } ] } } } </code>
GET /local_persons/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "remo",
            "fields": ["firstName"],
            "analyzer": "person_search_analyzer"
          }
        },
        {
          "query_string": {
            "query": "dsouza",
            "fields": ["lastName"],
            "analyzer": "person_search_analyzer"
          }
        }
      ]
    }
  }
}

Approach 2: Using the match query

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>GET /local_persons/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"firstName": {
"query": "remo",
"analyzer": "person_search_analyzer"
}
}
},
{
"match": {
"lastName": {
"query": "dsouza",
"analyzer": "person_search_analyzer"
}
}
}
]
}
}
}
</code>
<code>GET /local_persons/_search { "query": { "bool": { "must": [ { "match": { "firstName": { "query": "remo", "analyzer": "person_search_analyzer" } } }, { "match": { "lastName": { "query": "dsouza", "analyzer": "person_search_analyzer" } } } ] } } } </code>
GET /local_persons/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "firstName": {
              "query": "remo",
              "analyzer": "person_search_analyzer"
            }
          }
        },
        {
          "match": {
            "lastName": {
              "query": "dsouza",
              "analyzer": "person_search_analyzer"
            }
          }
        }
      ]
    }
  }
}

But both of the approaches above are giving empty results.

Elastic is not returning response because it has consider ‘standard’ as default analyzer for indexing as you didn’t defined index mapping and analyzer.

You can execute below API to get info about what is indexed in ES:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>POST local_persons/_analyze
{
"text":["Re'mo"],
"analyzer" : "standard"
}
</code>
<code>POST local_persons/_analyze { "text":["Re'mo"], "analyzer" : "standard" } </code>
POST local_persons/_analyze
{
  "text":["Re'mo"],
  "analyzer" : "standard"
}

Response:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"tokens": [
{
"token": "re'mo",
"start_offset": 0,
"end_offset": 5,
"type": "<ALPHANUM>",
"position": 0
}
]
}
</code>
<code>{ "tokens": [ { "token": "re'mo", "start_offset": 0, "end_offset": 5, "type": "<ALPHANUM>", "position": 0 } ] } </code>
{
  "tokens": [
    {
      "token": "re'mo",
      "start_offset": 0,
      "end_offset": 5,
      "type": "<ALPHANUM>",
      "position": 0
    }
  ]
}

Lets see result of your analyzer:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>POST local_persons/_analyze
{
"text":["Re'mo"],
"analyzer" : "person_search_analyzer"
}
</code>
<code>POST local_persons/_analyze { "text":["Re'mo"], "analyzer" : "person_search_analyzer" } </code>
POST local_persons/_analyze
{
  "text":["Re'mo"],
  "analyzer" : "person_search_analyzer"
}

Response:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"tokens": [
{
"token": "remo",
"start_offset": 0,
"end_offset": 5,
"type": "word",
"position": 0
}
]
}
</code>
<code>{ "tokens": [ { "token": "remo", "start_offset": 0, "end_offset": 5, "type": "word", "position": 0 } ] } </code>
{
  "tokens": [
    {
      "token": "remo",
      "start_offset": 0,
      "end_offset": 5,
      "type": "word",
      "position": 0
    }
  ]
}

If you observe both the response then you can clearly see that both are giving different response and hence Elastic is not returning response.

So, you can define index mapping like below which will make sure that index and search time it will use same analyzer and will return expected result as well.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>PUT /local_persons1
{
"settings": {
"analysis": {
"analyzer": {
"person_search_analyzer": {
"type": "custom",
"char_filter": ["remove_special_chars"],
"filter": ["lowercase"],
"tokenizer": "whitespace"
}
},
"char_filter": {
"remove_special_chars": {
"type": "pattern_replace",
"pattern": "[^a-zA-Z0-9]",
"replacement": ""
}
}
}
},
"mappings": {
"properties": {
"firstName":{
"type": "text",
"analyzer": "person_search_analyzer"
},
"lastName":{
"type": "text",
"analyzer": "person_search_analyzer"
}
}
}
}
</code>
<code>PUT /local_persons1 { "settings": { "analysis": { "analyzer": { "person_search_analyzer": { "type": "custom", "char_filter": ["remove_special_chars"], "filter": ["lowercase"], "tokenizer": "whitespace" } }, "char_filter": { "remove_special_chars": { "type": "pattern_replace", "pattern": "[^a-zA-Z0-9]", "replacement": "" } } } }, "mappings": { "properties": { "firstName":{ "type": "text", "analyzer": "person_search_analyzer" }, "lastName":{ "type": "text", "analyzer": "person_search_analyzer" } } } } </code>
PUT /local_persons1
{
  "settings": {
    "analysis": {
      "analyzer": {
        "person_search_analyzer": {
          "type": "custom",
          "char_filter": ["remove_special_chars"],
          "filter": ["lowercase"],
          "tokenizer": "whitespace"
        }
      },
      "char_filter": {
        "remove_special_chars": {
          "type": "pattern_replace",
          "pattern": "[^a-zA-Z0-9]",
          "replacement": ""
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "firstName":{
        "type": "text",
        "analyzer": "person_search_analyzer"
      },
      "lastName":{
        "type": "text",
        "analyzer": "person_search_analyzer"
      }
    }
  }
}

Now both of your query will return the response even if you don’t provide analyzer in response.

Query:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>GET /local_persons1/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"firstName": {
"query": "remo"
}
}
},
{
"match": {
"lastName": {
"query": "dsouza"
}
}
}
]
}
}
}
</code>
<code>GET /local_persons1/_search { "query": { "bool": { "must": [ { "match": { "firstName": { "query": "remo" } } }, { "match": { "lastName": { "query": "dsouza" } } } ] } } } </code>
GET /local_persons1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "firstName": {
              "query": "remo"
            }
          }
        },
        {
          "match": {
            "lastName": {
              "query": "dsouza"
            }
          }
        }
      ]
    }
  }
}

Response:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.5753642,
"hits": [
{
"_index": "local_persons1",
"_id": "1",
"_score": 0.5753642,
"_source": {
"id": 1,
"firstName": "Re'mo",
"lastName": "D'souza",
"email": "[email protected]",
"dateOfBirth": "1973-01-01",
"isActive": 1
}
}
]
}
</code>
<code>{ "took": 2, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 0.5753642, "hits": [ { "_index": "local_persons1", "_id": "1", "_score": 0.5753642, "_source": { "id": 1, "firstName": "Re'mo", "lastName": "D'souza", "email": "[email protected]", "dateOfBirth": "1973-01-01", "isActive": 1 } } ] } </code>
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "local_persons1",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "id": 1,
          "firstName": "Re'mo",
          "lastName": "D'souza",
          "email": "[email protected]",
          "dateOfBirth": "1973-01-01",
          "isActive": 1
        }
      }
    ]
  }

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật