spring boot中創建ElasticSearch索引


JAVA 8

Spring Boot 2.5.3

Elasticsearch-7.14.0(Windows)

---

 

目錄

創建索引1:只使用@Document注解

創建索引2:@Document注解 + @Setting注解

創建索引3:使用@Setting注解的settingPath屬性

settingPath屬性的json文件怎么編寫?

創建索引4:使用@Setting注解的多種屬性

sort屬性測試

useServerConfiguration屬性測試

 

創建索引1

只使用@Document注解

// 只使用 org.springframework.data.elasticsearch.annotations.Document 注解
@Document(indexName = "news")
@Data
public class News {

    //...
    
}

啟動應用,輸出下面的日志:

org.elasticsearch.client.RestClient : request [HEAD http://localhost:9200/news?ignore_throttled=false&ignore_unavailable=false&expand_wildcards=open%2Cclosed&allow_no_indices=false] returned 1
org.elasticsearch.client.RestClient : request [PUT http://localhost:9200/news?master_timeout=30s&timeout=30s] returned 1

Kibana執行 GET /news:

{
  "news" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "_class" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "refresh_interval" : "1s",
        "number_of_shards" : "1",
        "provided_name" : "news",
        "creation_date" : "1631870238379",
        "store" : {
          "type" : "fs"
        },
        "number_of_replicas" : "1",
        "uuid" : "tNNIub-dTqqPnKq6uiwdlA",
        "version" : {
          "created" : "7140099"
        }
      }
    }
  }
}

添加、查詢數據成功。

 

創建索引2

@Document + @Setting,第一種方式

# 使用@Setting注解
@Document(indexName = "news")
@Setting(shards = 2, replicas = 2, refreshInterval = "2s", indexStoreType = "mmapfs")
@Data
public class News {
}

Kibana執行 GET /news:

{
  "news" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "_class" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "refresh_interval" : "2s",
        "number_of_shards" : "2",
        "provided_name" : "news",
        "creation_date" : "1631870945579",
        "store" : {
          "type" : "mmapfs"
        },
        "number_of_replicas" : "2",
        "uuid" : "nSXP0GowT4eCHmHJS-kKGQ",
        "version" : {
          "created" : "7140099"
        }
      }
    }
  }
}

添加、查詢數據成功。

 

創建索引3

@Document + @Setting,第2種方式,使用@Setting的settingPath屬性

@Document(indexName = "news")
@Setting(settingPath = "indices/news.setting.json")
@Data
public class News {
}

news.setting.json 文件位於 /src/main/resources/indices下,配置內容如下:

{
	"number_of_shards": 2,
	"refresh_interval": "2s",
	"analyze": {
		"max_token_count": 500
	}
}

Kibana執行 GET /news:

{
  "news" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "_class" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "refresh_interval" : "2s",
        "number_of_shards" : "2",
        "provided_name" : "news",
        "analyze" : {
          "max_token_count" : "500"
        },
        "creation_date" : "1631871387436",
        "number_of_replicas" : "1",
        "uuid" : "zEDrWnOSQkG_necQitELUw",
        "version" : {
          "created" : "7140099"
        }
      }
    }
  }
}

添加、查詢數據成功。

 

settingPath屬性的json文件怎么編寫?

請看官方文檔,Index Modules,索引模塊,寫的明明白白。

https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html

上面這篇 index-modules.html 正是一個開頭,其中包括其它各種索引的配置的鏈接。

文中提到,索引可以單獨配置——不使用ES全局配置吧,而且 索引級別的配置 分為2種:

static 靜態的 只能在配置創建時 或 對已關閉索引使用,比如,分片數(index.number_of_shards);

dynamic 動態的 可以在索引使用中通過 update-index-settings API 更新,比如,副本數(number_of_replicas);

這兩種配置不和任何 索引模塊關聯。

官方提示,更改索引配置很危險。這樣的話,事先規划好,盡量不改吧

 

除了上面的,還有 其它索引模塊 可以配置:

Analysis 重點 分詞器等

Index shard allocation

Mapping 重點

Merging

Similarities

Slowlog

Store

Translog

History retention

Indexing pressure

Index lifecycle management

官文中有詳情,大家可以自行查閱。

其中2個被我標記為 重點,屬於常用配置吧(目前的經驗)。

相比於注解上的直接配置,通過JSON文件來配置會更全面。

 

創建索引4

2、3混合使用時,會成功嗎?成功的話,使用哪個的配置?

外3、文件中2。

注,外面注解上的直接配置都改為 3了

// News.java
@Setting(shards = 3, replicas = 3, refreshInterval = "3s", indexStoreType = "mmapfs", 
	settingPath = "indices/news.setting.json")
    
/*
news.setting.json 文件內容:
{
	"number_of_shards": 2,
	"number_of_replicas": 2,
	"refresh_interval": "2s",
	"analyze": {
		"max_token_count": 500
	},
	"store": {
		"type": "hybridfs"
	}
}
*/

Kibana執行 GET /news:

{
  "news" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "_class" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "refresh_interval" : "2s",
        "number_of_shards" : "2",
        "provided_name" : "news",
        "analyze" : {
          "max_token_count" : "500"
        },
        "creation_date" : "1631871707807",
        "store" : {
          "type" : "hybridfs"
        },
        "number_of_replicas" : "2",
        "uuid" : "uZt7irx9RhKMoYQtuqEt1A",
        "version" : {
          "created" : "7140099"
        }
      }
    }
  }
}

根據上面的響應可以看到,以json文件中為准。

添加、查詢數據成功。

 

將settingPath放到最前方,仍然如此:來自博客園

@Setting(settingPath = "indices/news.setting.json", 
	shards = 3, replicas = 3, refreshInterval = "3s", indexStoreType = "mmapfs")

 

Setting注解 中還有幾個 sort開頭的字段、useServerConfiguration 的試驗后文介紹。

 

sort屬性測試

sort測試1:啟動服務失敗,,沒有@Field注解

@Document(indexName = "news")
@Setting(
	sortFields = { "id" },
	sortOrders = {Setting.SortOrder.asc}
)
@Data
public class News {

	@Id
	private String id;
    
}

啟動APP失敗:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: 
Constructor threw exception; nested exception is java.lang.IllegalStateException: Required annotation 
interface org.springframework.data.elasticsearch.annotations.Field not found for id!

 來自博客園

sort測試2:啟動服務失敗,,id字段上添加@Field失敗,不允許

給id字段添加 @Field
	@Id
	@Field
	private String id;
    
啟動APP失敗:
Constructor threw exception; nested exception is java.lang.IllegalArgumentException: 
field type Auto not allowed for sortField



更改:使用 title
@Setting(
	sortFields = { "title" },
	sortOrders = {Setting.SortOrder.asc}
)
@Data
public class News {
	@Field
	private String title;
}

啟動失敗:
Constructor threw exception; nested exception is java.lang.IllegalArgumentException: 
field type Auto not allowed for sortField

 

注:上面的type Auto 市值@Field的type屬性,在org.springframework.data.elasticsearch.annotations.FieldType 中定義

 

sort測試3:參考官網的配置,設置@Field的type屬性

id、title兩個字段:
@Setting(
	sortFields = { "id", "title" },
	sortOrders = {Setting.SortOrder.asc, Setting.SortOrder.asc}
)
@Data
public class News {

	@Id
	@Field(type=FieldType.Keyword)
	private String id;

	/**
	 * 標題
	 */
	@Field(type=FieldType.Keyword)
	private String title;
    
}

啟動APP成功!

Kibana執行 GET /news ,可以看到:來自博客園

properties下已經有 id、title兩個節點了;

settings下也有一個sort節點了;

{
  "news" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "_class" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        },
        "id" : {
          "type" : "keyword"
        },
        "title" : {
          "type" : "keyword"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "refresh_interval" : "1s",
        "number_of_shards" : "1",
        "provided_name" : "news",
        "creation_date" : "1631965403582",
        "sort" : {
          "field" : [
            "id",
            "title"
          ],
          "order" : [
            "asc",
            "asc"
          ]
        },
        "store" : {
          "type" : "fs"
        },
        "number_of_replicas" : "1",
        "uuid" : "ZyHlxd1sQAuTnR4by-lReg",
        "version" : {
          "created" : "7140099"
        }
      }
    }
  }
}

配置成功。

添加、查詢數據成功。來自博客園

 

再次執行 Kibana的 GET /news:可以看到,添加數據后,settings下的sort節點沒有變化,,但是,mappings下為其它字段建立了一些properties——都要比id、title的復雜很多(默認配置)!

{
  "news" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "_class" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        },
        "contentHtml" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "contentText" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "id" : {
          "type" : "keyword"
        },
        "postTime" : {
          "type" : "long"
        },
        "source" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "title" : {
          "type" : "keyword"
        },
        "url" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "refresh_interval" : "1s",
        "number_of_shards" : "1",
        "provided_name" : "news",
        "creation_date" : "1631965403582",
        "sort" : {
          "field" : [
            "id",
            "title"
          ],
          "order" : [
            "asc",
            "asc"
          ]
        },
        "store" : {
          "type" : "fs"
        },
        "number_of_replicas" : "1",
        "uuid" : "ZyHlxd1sQAuTnR4by-lReg",
        "version" : {
          "created" : "7140099"
        }
      }
    }
  }
}

 

疑問:

配置這個有什么好處呢?排除?提高查詢速度?TODO 

 

另外測試了 FieldType.Text ,也無法用來設置sort。

 

useServerConfiguration屬性測試

注,測試前刪除之前的索引

默認是 false,現設置為 true——使用ES服務器的配置,而不使用Java代碼中的配置?

@Setting的useServerConfiguration屬性測試

@Document(indexName = "news")
@Setting(useServerConfiguration = true,
	shards = 3, replicas = 3, refreshInterval = "3s", indexStoreType = "mmapfs",
	settingPath = "indices/news.setting.json",
	sortFields = { "id", "title" },
	sortOrders = {Setting.SortOrder.asc, Setting.SortOrder.asc},
	sortModes = {Setting.SortMode.min, Setting.SortMode.max}
)
@Data
public class News {
//...
}

啟動APP成功。

Kibana執行 GET /news:

mappings下有id、sort節點了,和@Field有關系吧。來自博客園

但settings下卻沒有sort節點了。

從settings下的配置來看,使用了 settingPath 中的配置。

{
  "news" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "_class" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        },
        "id" : {
          "type" : "keyword"
        },
        "title" : {
          "type" : "keyword"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "refresh_interval" : "2s",
        "number_of_shards" : "2",
        "provided_name" : "news",
        "analyze" : {
          "max_token_count" : "500"
        },
        "creation_date" : "1631966277191",
        "store" : {
          "type" : "hybridfs"
        },
        "number_of_replicas" : "2",
        "uuid" : "viPNAWZDT2iV-2BEjkd2bw",
        "version" : {
          "created" : "7140099"
        }
      }
    }
  }
}

 

Spring Data ES官文的說明:不發送任何配置參數,,難道配置文件中的不算?

不過,的確屏蔽了屬性中的配置——sort的配合沒有生效。來自博客園

去掉 settingPath屬性 進行測試:看起來真的使用了服務器的配置,直接忽略了@Setting的其它屬性的配置。

settings節點下的信息也變的很少了啊!

@Setting(useServerConfiguration = true,
	shards = 3, replicas = 3, refreshInterval = "3s", indexStoreType = "mmapfs",
	sortFields = { "id", "title" },
	sortOrders = {Setting.SortOrder.asc, Setting.SortOrder.asc},
	sortModes = {Setting.SortMode.min, Setting.SortMode.max}
)

Kibana 執行 GET /news:
{
  "news" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "_class" : {
          "type" : "keyword",
          "index" : false,
          "doc_values" : false
        },
        "id" : {
          "type" : "keyword"
        },
        "title" : {
          "type" : "keyword"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "news",
        "creation_date" : "1631966831563",
        "number_of_replicas" : "1",
        "uuid" : "mbnoBSH0Rk6STmegSHEZqg",
        "version" : {
          "created" : "7140099"
        }
      }
    }
  }
}

 

在Kibana中使用上面news.setting.json的配置:失敗了

失敗了!

根據錯誤信息,一個一個地減少,最終,沒有創建索引成功

和前面使用的成功建立索引的配置對比:來自博客園

這部分需要看看官方文檔才行!TODO

 

最后的問題:

索引到底是 使用前調用RESTful接口 建立好呢,還是在程序里面使用@Setting等注解來建立呢?

最佳實踐是什么?

 

》》》全文完《《《

 

mapping是干啥的?

官方文檔,真的太重要了!而且非常節約精時!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM