乐闻世界logo
搜索文章和话题

How do you create an index mapping in Elasticsearch?

1个答案

1

Creating index mappings in Elasticsearch is a critical step because it defines the data types for fields in the index and how they are processed. Below are the steps for creating index mappings, along with a specific example:

Step 1: Design the Mapping

First, determine your data structure, including the data types for each field. Elasticsearch supports various data types, such as text, keyword, date, and integer.

Step 2: Create the Index and Mapping Using a PUT Request

You can create an index and define its mapping using Elasticsearch's REST API. Use a PUT request to specify the index name and provide the JSON definition for the mapping.

Step 3: Verify the Mapping Creation

After creation, use a GET request to confirm that the mapping was created as expected.

Example

Suppose we need to create an index for a simple blog system, including article titles, content, and publication date. You can follow these steps:

  1. Define the Mapping:

    The data types for each field are:

    • title: text
    • content: text
    • publish_date: date
  2. Create the Index and Mapping:

    Use the following PUT request to create the index and its mapping:

    sh
    PUT /blog { "mappings": { "properties": { "title": { "type": "text" }, "content": { "type": "text" }, "publish_date": { "type": "date", "format": "yyyy-MM-dd" } } } }
  3. Verify the Mapping:

    Send a GET request to check the mapping:

    sh
    GET /blog/_mapping

This process ensures that data is correctly processed and stored during indexing, and proper data type and format configurations will enhance search accuracy and performance.

2024年8月13日 21:34 回复

你的答案