The Index templates | Elasticsearch Reference [7.8] | Elastic

  • There are two types of templates, index templates and component templates. Component templates are reusable building blocks that configure mappings, settings, and aliases. You use component templates to construct index templates, they aren’t directly applied to a set of indices. Index templates can contain a collection of component templates, as well as directly specify settings, mappings, and aliases.

Index templates are divided into two parts: Index templates and Component Templates.

component templates

Component templates are reusable building blocks for configuring mappings, Settings, and aliases

index templates

An index template can contain a collection of component templates or specify Settings, mappings, and aliases directly

Component is the smallest template. Index can be composed_of multiple components, eg:

PUT _component_template/component_template1

PUT _component_template/component_template2

PUT _index_template/template_1
{
  .
  "composed_of": ["component_template1"."other_component_template"]..
}
Copy the code

We need to solve the template problem of the Custom Analyzer we talked about last time, so for this problem, use the ass or use the Component.

However……

PUT /_component_template/template_1
{
  "template": {
    "settings" : {
        "number_of_shards" : 1}},"version": 123
}

{
  "error": "Incorrect HTTP method for uri [/_component_template/template_1?pretty] and method [PUT], allowed: [POST]"."status": 405
}
Copy the code

I rubbed my eyes and read the wrong API. It is the current version (currently 7.8) and I am using 7.4

Forget about components for now, write an example:

PUT _template/template_ngram_analyzer
{
  "index_patterns": ["*_v*"."* _ *"].Define which indexes are affected by template
  "settings": {
    "analysis": {
      "analyzer": {
        "ngram_one_word": {
          "type": "custom"."tokenizer": "ngram".Min =1 Max =2
          "filter": [
            "lowercase"]}}}}}Copy the code
  • Since our ES index has some rules, index_Patterns can be written a little more arbitrarily, leaving out the Stop filter and testing ngram to see if it works

It was very smooth to set up our custom tokenizer: ngram_one_word

Check out the setting:

Confirming my earlier conjecture about nGram participles:

I said before after thinking based on the official website document that I could not understand that min=1 Max =1 could be accurately matched. It does not matter if the business is fuzzy, I will test it myself:

  • Verify the old version, sure enough, there is a problem

  • Verifying the new version

However, the result is almost the same as the old version:

Custom Analyzer: Ngram (ngram, Ngram, Ngram, Ngram, Ngram) 🎉 👏

  • I later learned that the ngram usage was not a match query, but a match_phrase query that kept the query order the same so that 8930 was not searched for 8899.

Spring-data-elasticsearch support for _template

First, search some source code to see if _template is supported. If it is supported, it is best not to support its own implementation. Avoid duplicate wheels.

Spring Data writing

/ / pseudo code
public static final void createIndexTemp(a) {
		RestHighLevelClient client = EsClient.getClient();
		try {
			PutIndexTemplateRequest request = new PutIndexTemplateRequest("nGram_one_word")
			List<String> indexPatterns = new ArrayList<String>();
			indexPatterns.add("*_v*");
			indexPatterns.add("* _ *");
			request.patterns(indexPatterns);
			
			 
			Map<String, Object> settings = new HashMap<>();
			
			// complate setting ......
			request.settings(settings);
			
			System.out.println(client.indices().putTemplate(request, RequestOptions.DEFAULT));
			
		} catch (Exception e) {
			// TODO: handle exception
		} finally{ EsClient.close(client); }}Copy the code

ES of writing:

/ / pseudo code
public void createTemplate(a) {
        String templateName = "template_ngram_analyzer";
				String NGRAM_ONE_WORD = "{your string json ... }";
				PutIndexTemplateRequest request = new PutIndexTemplateRequest(templateName);
        request.source(NGRAM_ONE_WORD, XContentType.JSON);
        // Force create, but do not force update, save an exists
        request.create(true);
        elasticsearchRestTemplate.execute(client -> client.indices().putTemplate(request,RequestOptions.DEFAULT));
    }
Copy the code

Delete the original index template and use Java Client to test it again.

Github Archive: github.com/pkwenda/Blo…