Elasticsearch Adding mapping

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.