Skip to content

Instantly share code, notes, and snippets.

@marceloserpa
Created August 15, 2021 22:23
Show Gist options
  • Save marceloserpa/284aed147631d89e05a0b5e15a47957e to your computer and use it in GitHub Desktop.
Save marceloserpa/284aed147631d89e05a0b5e15a47957e to your computer and use it in GitHub Desktop.

Create mapping

PUT profile 
{
  "mappings": {
    "doc": {
      "properties": { 
        "name":     { "type": "text"  }, 
        "age":      { "type": "integer" }  
      } 
    }
  }
}

Check mapping

GET profile/_mapping

Create documents to test

POST profile/doc/1
{
  "name":     "Marcelo", 
  "age":       31
}
POST profile/doc/2
{
  "name":     "john", 
  "age":       25
}

Update the index mapping

PUT profile/_mapping/doc 
{
      "properties": { 
        "name": { 
          "type": "text", 
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        }, 
        "age":      { "type": "integer" }  
      }       
}

Check the new mapping for index

GET profile/_mapping

Create new document:

POST profile/doc/3
{
  "name":     "novo usuario", 
  "age":       40
}

This query should return user 3 because this document was create using a new mapping that make possible to query by keyword:

GET profile/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "name.keyword": "novo usuario"
          }
        }
      ]
    }
  }
}

This query should no find user 1 because it was created when mapping does not have support to keyword for name:

GET profile/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "name.keyword": "marcelo"
          }
        }
      ]
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment