Elasticsearch Search API uses one: Create an index and specify the mapping of the index type

Source: Internet
Author: User

Es default is the mapping of dynamically created indexes and index types. This is equivalent to not having to define the schema in SOLR, and it is convenient to index the file without specifying the index rules for each field. But sometimes the convenience represents the inflexible. For example, es default a field is to do participle, but we sometimes want to search matches the entire field but not. If there is statistical work to record the number of occurrences of each city. For the Name field, if the "New York" text is recorded, ES may split it into two words "new" and "York", counting the number of two words, instead of the "New York" we expected.

At this point, we need to define mapping when we create the index. Assuming the index is called Index_name, the name of the index type is index_type, and the mapping file is written as follows ( Note: Mapping file, Index_type must be exactly the same as the index type in the actual index. ):

{"Index_type": {"Properties": {"ID": {"type": "string", "index": "Not_analyzed"}, "NAME": {"type": "String", " Fields ": {" NAME ": {" type ":" String "}," raw ": {" type ":" string "," index ":" Not_analyzed "}}}}

The above file says that we define its mapping for Index_type, the index type. The point is to map the name field to two, one to name the index analysis, and the other to not analyze raw, which will not split the phrase New York. So when we do the search, we can do the term aggregation for the Name.raw field and get the number of occurrences in all cities. Term Aggregation's The rest method requests are written as follows:

{"Query": {"Match_all": {}}, "aggregations": {"Cityaggs": {"Terms": {"field": "Name.raw"} }  }}

Since I am using the Windows development machine, it is highly recommended to install Elasticsearch head for testing, which is much more convenient than the command line curl. the installation of Elasticsearch head is simple, go to the ES bin directory, execute

Plugin-install Mobz/elasticsearch-head


When specifically implemented, write Java code to create an index index_name, create an index type Index_type specify this mapping method as follows

Omit the Java code that reads the mapping file, and the contents are saved in the Mapping_json. Client client = new Transportclient (). addtransportaddress (New inetsockettransportaddress ("127.0.0.1", 9300)); Client.admin (). Indices (). Preparecreate ("Index_name"). Execute (). Actionget (); Client.admin (). Indices (). Prepareputmapping ("Index_name"). SetType ("Index_type"). SetSource (Mapping_json). Execute (). Actionget ();



Elasticsearch Search API uses one: Create an index and specify the mapping of the index type

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.