Problem:
Need to demo basic usage of analyzers with "text" and "keyword" types.
Solution Summary:
We will demo how a match or match_phrase query work with "text" field, "keyword field" and with custom analyzer. We will only see basic use cases; for advanced use cases with relevance scoring, check the beyond basics book.
Prerequisites:
Working Elasticsearch cluster and Kibana.
if employee_analyzerdemo available:
DELETE employee_analyzerdemo
Solution Steps:
Initial Setup
Create index:
PUT employee_analyzerdemo
Create a document:
PUT employee_analyzerdemo/_mapping/_doc
{
"properties": {
"address1": {
"type": "text"
},
"address2": {
"type": "keyword"
},
"address3": {
"type": "text",
"analyzer": "whitespace"
}
}
}
Note:
-
"address1" is of "text" type (and use the "standard" analyzer, which is the default).
-
"address2" is of "keyword" type
-
"address3" is of type "text" and uses the "whitespace" analyzer.
Verify Mapping:
GET employee_analyzerdemo/_mapping/_doc
Response:
{
"employee_analyzerdemo": {
"mappings": {
"_doc": {
"properties": {
"address1": {
"type": "text"
},
"address2": {
"type": "keyword"
},
"address3": {
"type": "text",
"analyzer": "whitespace"
}
}
}
}
}
}
Add a document:
PUT employee_analyzerdemo/_doc/1?pretty
{
"address1": "Bangalore, India",
"address2": "Bangalore, India",
"address3": "Bangalore, India"
}
Verify document with:
GET employee_analyzerdemo/_doc/1
Query 1 - Match Query
Replace address with address1, address2 and address3:
GET employee_analyzerdemo/_search
{
"query": {
"match": {
"address": "bangalore"
}
}
}
Response (address = address1)
1 document matched.
Response (address = address2)
0 document matched.
Response (address = address3)
0 document matched.
Query 2 - Match Phrase Query
Replace address with address1, address2 and address3:
GET employee_analyzerdemo/_search
{
"query": {
"match_phrase": {
"address": "bangalore"
}
}
}
Response (address = address1)
1 document matched.
Response (address = address2)
0 document matched.
Response (address = address3)
0 document matched.
Query 3 - Match Phrase Query
Replace address with address1, address2 and address3:
GET employee_analyzerdemo/_search
{
"query": {
"match_phrase": {
"address": "Bangalore, India"
}
}
}
Response (address = address1)
1 document matched.
Response (address = address2)
1 document matched.
Response (address = address3)
1 document matched.
Query 4 - Match Phrase Query
Replace address with address1, address2 and address3:
GET employee_analyzerdemo/_search
{
"query": {
"match_phrase": {
"address": " Bangalore, India "
}
}
}
Response (address = address1)
1 document matched.
Response (address = address2)
0 document matched.
Response (address = address3)
1 document matched.
Query 5 - Match Phrase Query
Replace address with address1, address2 and address3:
GET employee_analyzerdemo/_search
{
"query": {
"match_phrase": {
"address": "bangalore, india"
}
}
}
Response (address = address1)
1 document matched.
Response (address = address2)
0 document matched.
Response (address = address3)
0 document matched.
TODO
- Change the analyzer for "address3" to "simple" and execute Query 5.
- Hint: You will have to delete index and create again, as mappings cannot be updated.
Recent comments