ElasticSearch index operations

This article uses DSL to operate ES and SpringBoot to operate ES.
More ElasticSearch entry-to-practice tutorials: Click to view

2.1 Index operation

ES index is a container for storing data, similar to a database.

2.1.1 Create index

1. DSL syntax

PUT indexname
{<!-- -->
  "settings": {<!-- -->
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {<!-- -->
    "properties": {<!-- -->
      "name1":{<!-- -->
         "type": "text"
      },
      "name2":{<!-- -->
        "type": "integer"
      }
    }
  }
}
1. Parameter description:

settings: Index information settings

number_of_shards: The number of primary shards for each index. This configuration cannot be modified after the index is created.

number_of_replicas: The number of replicas for each primary shard. This configuration can be modified at any time.

  • What is sharding?

Elasticsearch clusters allow the system to store data that exceeds the capacity of a single machine, which is achieved through shards. In an index, data (document) is sharded into multiple shards. In other words: each shard stores a portion of all data.
A shard is an instance of Lucene that is a complete search engine in its own right. Documents are stored into shards, but the application interacts directly with the index rather than with the shards.

  • What is a copy?

In order to solve the problem that a single machine cannot handle all requests when the access pressure is too high, the Elasticsearch cluster introduces the replica strategy. The replica strategy creates redundant copies of each shard in the index.

  • The purpose of a copy is as follows:
  1. Improve system fault tolerance
    When the machine where the shard is located goes down, Elasticsearch can use its replica to recover, thus avoiding data loss.

  2. Improve ES query efficiency
    When processing queries, ES will treat replica shards and primary shards fairly, and load-balance query requests to replica shards and primary shards.

mappings: index mapping definition

properties: field definition
Properties contains json configuration, key is the field name (custom name), value is a nested json, and type is the type of the specified field.

2. Java API

Some codes for creating indexes have been introduced in Chapter 1.

 //Get the client object from the Spring container
    @Autowired
    private RestHighLevelClient client;

    public Boolean createIndex(String indexName) {<!-- -->
        //Create an index request class, the constructor parameter is the index name
        CreateIndexRequest request = new CreateIndexRequest(indexName);
        //Set the source mapping string and directly copy the statement inside
        request.source(
                "{\
" +
                        " "settings": {\
" +
                        " "number_of_shards": 1,\
" +
                        " "number_of_replicas": 1\
" +
                        " },\
" +
                        " "mappings": {\
" +
                        " "properties": {\
" +
                        " "name1":{\
" +
                        " "type": "text"\
" +
                        " },\
" +
                        " "name2":{\
" +
                        " "type": "integer"\
" +
                        " }\
" +
                        " }\
" +
                        " }\
" +
                        "}",
                XContentType.JSON);
        try {<!-- -->
            //Call the create index syntax
            client.indices().create(request, RequestOptions.DEFAULT);
            return true;
        } catch (IOException e) {<!-- -->
            e.printStackTrace();
        }
        return false;
    }

Note that the method called here is source, not mapping

The mapping method only sets the field

The source method is called with the settings content.

2.1.2 Delete index

1. DSL syntax

DELETE indexname

Call execution, the following return result is successful

{<!-- --> -
  "acknowledged": true
}

2. Java API

 //Get the client object from the Spring container
    @Autowired
    private RestHighLevelClient client;

    @RequestMapping("/deleteIndex")
    public Boolean deleteIndex(String indexName) {<!-- -->
        //Delete the index request class, the constructor parameter is the index name
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
        try {<!-- -->
            //Call delete index syntax
            client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
            return true;
        } catch (IOException e) {<!-- -->
            e.printStackTrace();
        }
        return false;
    }

2.1.3 Turn on/off index

What is Elasticsearch on/off indexing?

Once the index is closed, the index can only display metadata information and cannot perform read or write operations.
Let’s talk about opening the index and it will be easier to understand. It is to open the closed index and allow read and write operations.

1. Close index

1. DSL syntax
POST indexname/_close

Call execution, the following return result is successful

{<!-- --> -
  "acknowledged": true,
  "shards_acknowledged": true,
  "indices": {<!-- --> -
    "indexname": {<!-- --> -
      "closed": true
    }
  }
}
2. Java API
 @RequestMapping("/closeIndex")
    public Boolean closeIndex(String indexName) {<!-- -->
        CloseIndexRequest closeIndexRequest = new CloseIndexRequest(indexName);
        try {<!-- -->
            client.indices().close(closeIndexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {<!-- -->
            e.printStackTrace();
        }
        return true;
    }

2. Turn on index

1. DSL syntax
POST indexname/_open

Call execution, the following return result is successful

{<!-- --> -
  "acknowledged": true,
  "shards_acknowledged": true
}
2. Java API
 @RequestMapping("/openIndex")
    public Boolean openIndex(String indexName) {<!-- -->
        OpenIndexRequest openIndexRequest = new OpenIndexRequest(indexName);
        try {<!-- -->
            client.indices().open(openIndexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {<!-- -->
            e.printStackTrace();
        }
        return true;
    }

2.1.4 Index alias

Index alias overview:

In ES, index aliases are like shortcuts or soft links that can point to one or more indexes.
What’s the use of aliases?

  1. Set an alias for multiple indexes, and you can use this alias to query data from multiple indexes at the same time.

  2. For example, in a project scenario, a new index needs to be created every day, and the program needs to query new index data. The program must change the accessed index name. If the user is not aware of switching, the code complexity will be high, and it will easily affect the service. Users have a certain influence. The more complex the process, the more likely BUG will appear.
    Then after you have an alias, you can add this alias to the index from the beginning. The program only focuses on accessing this alias. After creating a new index, switch the alias to the new index. The program does not need to change, but the new index is accessed later, and there is no need to Stop the application from running. Of course, this is just a scenario. In project development, aliases have many uses. We will introduce more about indexes in subsequent project explanations.

Fields also have aliases, and their usage is not very high. I won’t explain them in detail here.

1. Add alias

Create an index alias and associate the alias indexname_alias with the index indexname

1. DSL syntax
  1. Request method 1
PUT indexname/_alias/indexname_alias
  1. Request method 2
POST _aliases
{<!-- -->
  "actions": [
    {<!-- -->
      "add": {<!-- -->
        "index": "indexname",
        "alias": "indexname_alias"
      }
    }
  ]
}
2. Java API syntax
 @Autowired
    private RestHighLevelClient client;

    @RequestMapping("/addAlias")
    public Boolean addAlias(String indexName, String aliasName) {<!-- -->
        IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
        IndicesAliasesRequest.AliasActions aliasActions = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.ADD);
        aliasActions.index(indexName).alias(aliasName);
        indicesAliasesRequest.addAliasAction(aliasActions);
        try {<!-- -->
            client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {<!-- -->
            e.printStackTrace();
        }
        return true;
    }

The interface receives two parameters, indexName: index name, aliasName: alias to be added

Open the address calling interface
http://localhost:8080/addAlias?indexName=indexname & amp;aliasName=indexname_alias

2. Delete alias

Delete an index alias: Disassociate the alias indexname_alias from the index indexname

1. DSL syntax
  1. Request method 1
DELETE indexname/_alias/indexname_alias
  1. Request method 2
POST _aliases
{<!-- -->
  "actions": [
    {<!-- -->
      "remove": {<!-- -->
        "index": "indexname",
        "alias": "indexname_alias"
      }
    }
  ]
}
2. Java API syntax
 @Autowired
    private RestHighLevelClient client;

    @RequestMapping("/removeAlias")
    public Boolean removeAlias(String indexName, String aliasName) {<!-- -->
        IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
        IndicesAliasesRequest.AliasActions aliasActions = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.REMOVE);
        aliasActions.index(indexName).alias(aliasName);
        indicesAliasesRequest.addAliasAction(aliasActions);
        try {<!-- -->
            client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {<!-- -->
            e.printStackTrace();
        }
        return true;
    }

Just like adding, the interface receives two parameters, indexName: index name, aliasName: the alias to be added.

Open the address calling interface
http://localhost:8080/removeAlias?indexName=indexname & amp;aliasName=indexname_alias

3. Switch alias

Switching an alias is to perform add and delete operations in the same API. This operation is atomic, so there’s no need to worry about the short time the alias doesn’t point to an index.

Readers are asked to create two indexes indexname1 and indexname2 by themselves. In order to prevent unnecessary errors, please add an alias to indexname2 first.

1. DSL syntax
POST _aliases
{<!-- -->
  "actions": [
    {<!-- -->
      "add": {<!-- -->
        "index": "indexname1",
        "alias": "indexname_alias"
      }
    },
    {<!-- -->
      "remove": {<!-- -->
        "index": "indexname2",
        "alias": "indexname_alias"
      }
    }
  ]
}
2. Java API syntax
 @Autowired
    private RestHighLevelClient client;

    @RequestMapping("/removeAlias")
    public Boolean removeAlias(String indexName, String aliasName) {<!-- -->
        IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
        IndicesAliasesRequest.AliasActions aliasActions = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.REMOVE);
        aliasActions.index(indexName).alias(aliasName);
        indicesAliasesRequest.addAliasAction(aliasActions);
        try {<!-- -->
            client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {<!-- -->
            e.printStackTrace();
        }
        return true;
    }

Open the address calling interface
http://localhost:8080/changeAlias

4. View aliases

1. DSL syntax
  1. Query index by alias
GET _alias/indexname_alias

According to the returned results, there is this alias under the indexname index.

{<!-- --> -
  "indexname": {<!-- --> -
    "aliases": {<!-- --> -
      "indexname_alias": {<!-- --> -

      }
    }
  }
}
  1. Query aliases by index
GET indexname/_alias
  1. Check if the alias exists in the index
GET indexname/_alias/indexname_alias
2. Java API syntax
  1. Query index by alias
 @Autowired
    private RestHighLevelClient client;

    @RequestMapping("/selectIndexByAlias")
    public Map selectIndexByAlias(String aliasName) {<!-- -->
        GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliasName);
        // Specify the alias to view a certain index. If not specified, all aliases will be searched.
        getAliasesRequest.indices();
        try {<!-- -->
            GetAliasesResponse response = client.indices().getAlias(getAliasesRequest,RequestOptions.DEFAULT);
            Map<String, Set<AliasMetadata>> aliases;
            aliases = response.getAliases();
            return aliases;
        } catch (IOException e) {<!-- -->
            e.printStackTrace();
        }
        return null;
    }

Open the address calling interface, and the return result will appear on the page.
http://localhost:8080/selectIndexByAlias?aliasName=indexname_alias

  1. Query aliases by index
 @Autowired
    private RestHighLevelClient client;

    @RequestMapping("/selectIndexByAlias")
    public Map selectIndexByAlias(String aliasName) {<!-- -->
        GetAliasesRequest getAliasesRequest = new GetAliasesRequest();
        // Specify the alias to view a certain index. If not specified, all aliases will be searched.
        getAliasesRequest.indices(indexName);
        try {<!-- -->
            GetAliasesResponse response = client.indices().getAlias(getAliasesRequest,RequestOptions.DEFAULT);
            Map<String, Set<AliasMetadata>> aliases;
            aliases = response.getAliases();
            return aliases;
        } catch (IOException e) {<!-- -->
            e.printStackTrace();
        }
        return null;
    }

Open the address calling interface, and the return result will appear on the page.
http://localhost:8080/selectAliasByIndex?aliasName=indexname1

  1. Check if the alias exists in the index
 @Autowired
    private RestHighLevelClient client;

    @RequestMapping("/getAliasExist")
    public Boolean getAliasExist(String indexName,String aliasName) {<!-- -->
        GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliasName);
        // Specify the alias to view a certain index. If not specified, all aliases will be searched.
        getAliasesRequest.indices(indexName);
        try {<!-- -->
            return client.indices().existsAlias(getAliasesRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {<!-- -->
            e.printStackTrace();
        }
        return false;
    }

Open the address calling interface, and the return result will appear on the page.
http://localhost:8080/getAliasExist?indexName=indexname & amp;aliasName=indexname_alias

For more information, please see “ElasticSearch Getting Started to Practical Tutorial”: Click to view