使用searchbox中的JestClient創建攜帶settings和mappings的索引

在項目開發中,經常會遇到在創建索引的時候,直接把mapping和setting創建到索引中,使用jestClient api時如何創建index,如下代碼:

import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.indices.CreateIndex;
private static JestClient jestClient;
	public static void main(String [] args) throws Exception {
		String mapping="";
		/**
		 * mapping={
	"settings":{
		"analysis":{
			"analyzer":{
				"my_analyzer":{
					"tokenizer":"my_tokenizer"
				}
			},
			"tokenizer":{
				"my_tokenizer":{
					"type":"ngram"
				}
			}
		}
	},
	"mappings":{
		"test_type":{
			"properties":{
				"field1":{
					"type":"keyword"
				},
				"field2":{
					"type":"byte"
				},
				"my_join_field":{
					"type":"join",
					"relations":{
						"my_parent":"my_child"
					}
				}
			}
		}
	}
}
		 */
		CreateIndex createIndex=new CreateIndex.Builder("test").settings(mapping).build();
		JestResult jestResult=jestClient.execute(createIndex);
		System.out.println(jestResult.getJsonString());
    }

參考:https://github.com/searchbox-io/Jest/blob/master/jest/src/test/java/io/searchbox/indices/CreateIndexIntegrationTest.java

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章