[Recipes] Elasticsearch Analysis Demo - Text and Keyword Types

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: 

  1. "address1" is of "text" type (and use the "standard" analyzer, which is the default).

  2. "address2" is of "keyword" type

  3. "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

  1. Change the analyzer for "address3" to "simple" and execute Query 5.
    1. Hint: You will have to delete index and create again, as mappings cannot be updated.

Recipe Tags: 

Learn Serverless from Serverless Programming Cookbook

Contact

Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About

CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream