1. Create an index
/**
* Create an index
*
*@paramIndexName
*/
public static voidCreateIndex (String indexname) {
Delete before inserting to avoid error
Boolean flag =Client.admin (). Indices (). Exists (New Indicesexistsrequest (). Indices (new string[]{indexname}). Actionget (). Isexists () ;
client.admin (). Indices (). Preparedelete (IndexName). Execute (). Actionget () ;
"
client.admin (). Indices (). Preparecreate (IndexName). Get () ;
//client.admin (). Indices (). Preparecreate (IndexName)
//. Setsettings (Settings.builder ()
//. Put (" Index.number_of_shards ", 3)
//. Put (" Index.number_of_replicas ", 2)
//)
//. get ();
}
2. Create mapping
/*** Create mapping, modify as required * *@paramIndexName *@paramTypeName*/ Public Static voidcreatemapping (String indexname, String typeName) {Xcontentbuilder MappingType=NULL; Try{MappingType=Jsonbuilder (). StartObject (). StartObject (typeName). Startob Ject ("Properties"). StartObject ("id"). Field ("Type", "integer"). Field ("Store", "yes")). EndObject (). StartObject ("Name"). Field ("Type", "string"). Field ("Store", "yes"). Field ("Analyzer", "Ik_smart"). Field ("Search_analyzer", "Ik_ Smart). EndObject (). StartObject ("Updatedate"). Field ("Type", "Date"). Field ("Store", "Yes"). EndObject (). EndObject (). EndObject (). EndObject (); } Catch(IOException e) {logger.error ("Build company mapping Failed", E); } putmappingrequest Mapping=requests.putmappingrequest (IndexName). Type (typeName). Source (MappingType); Client.admin (). Indices (). putmapping (mapping). Actionget (); Client.admin (). Indices (). Preparerefresh (). get (); //client.close ();}
Elasticsearch Adding mapping