Summaries/Databases/ElasticSearch/Index.md

3.0 KiB

title updated created
Index 2021-05-04 14:58:11Z 2021-05-04 14:58:11Z

Indexes

Create index without mapping

{
	PUT <index>
}

Delete index

{
	DELETE <index>
}

Mapping is the process of defining how a document, and the fields it contains, are stored and indexed.

Mapping

{
    GET /<index>/_mapping
}

{
    GET /<index>/_mapping/field/<fieldname>
}

create a new index with mapping (example)

  • Can not change the number of shards after index iz created
  • re-index is possible (worstcase)
  • replicas can be added later

Types of fields

  • object (may contain inner objects; json docs are hierarchocal in nature)
  • nested
PUT /items
{
    "settings": {
    "index": {
        "number_of_shards": <int>,
        "number_of_replicas": <int>
        }
    },
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "production_date": {
        "type": "date"
      },
      "location": {
        "type": "geo_point"
      },
      "max_spots": {
          "type": "integer"
      },
      "description": {
          "type": "text"
      }
    }
  }
}

Field datatypes

Elasticsearch Query DSL

  PUT /catalog 
  {
    "settings": {
      "index": {
         "number_of_shards": <int>,
         "number_of_replicas": <int>
       }
      },
  "mappings": {
    "properties": {
    "speaker": {"type": "keyword"},
    "play_name": {"type": "keyword"},
    "line_id": {"type": "integer"},
    "description": {"type": "text"}
    "speech_number": {"type": "integer"}
    }
  }
}

Adding a type mapping in an existing index

merged into the existing mappings of the _doc type

  PUT /<index>/_mapping
  {
    "properties": {
        "name": {
           "type": "text"    
         }
     }
  }

Create NESTED mapping, insert data and query

DELETE /developer

PUT /developer
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "skills": {
        "type": "nested",
        "properties": {
          "language": {
            "type": "keyword"
          },
          "level": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

POST /developer/_doc/101
{
  "name": "john Doe",
  "skills": [
    {
      "language": "ruby",
      "level": "expert"
    },
    {
      "language": "javascript",
      "level": "beginner"
    }
  ]
}


GET /developer/_search
{
  "query": {
    "nested": {
      "path": "skills",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "skills.language": "ruby"
              }
            },
            {
              "match": {
                "skills.level": "expert"
              }
            }
          ]
        }
      }
    }
  }
}

Create OBJECT mapping, insert data and query