Elasticsearch using the rest API for full-text indexing

Source: Internet
Author: User

By adding search data through the rest API, reading the official documentation reveals that Elasticsearch supports dynamic mapping, but there are a lot of questions and listen slowly.

This article mainly tells about three points content:

1 Elasticsearch Common REST API

2 Elasticsearch When adding an index, the dynamic mapping error: mapperparsingexception[failed to parse]nested; Jsonparseexception

3 Elasticsearch Adding index data using the bulk command

ES REST API

The Elasticsearch supports HTTP request-response services, so the Curl command allows you to send HTTP requests and get JSON back content.

Common rest requests include the following:

Check ES cluster Status:

Curl Localhost:9200/_cat/health?v

Check the ES node status:

Curl Localhost:9200/_cat/nodes?v

 To query all indexes:

Curl Localhost:9200/_cat/indices?v

  Create an index (this is a problem under version 4.1, the next section ):

Curl-xput localhost:9200/index name/type/id-d {"name": "Xingoo"}

  To delete an index :

Curl-xdelete Localhost:9200/Index Name

  Query index:

Curl-xget localhost:9200/index name/type name/ID
ES Dynamic Mapping Error

The document in Elasticsearch is a fixed format and, normally, you should define the format of the document before importing the data.

However, Elasticsearch supports dynamic mapping, that is, if {"Test1": 123, "test2": "123"} will automatically parse the first field into a numeric number, and the second field resolves to a string type.

Then we try to execute the CREATE INDEX command under DOS:

As you can see, there is no index in the start ES, and after adding the index through the Curl command, the error is prompt:

Mapperparsingexception, it does not parse the name into a string, but instead parses it into a Boolean type that supports only true,false, or null. And the query index we found, at this time, IndexName has been established. At this point, the query map does not have any mapping information:

After experimentation, it is found that dynamic mappings only support Boolean and number long, all non-numbers, and Elasticsearch are automatically parsed into a Boolean type.

  In a nutshell, the official documents are dynamically mapped anyway! It can only recognize booleans and numbers. No matter how you try, I don't believe it anyway. (Version 1.7)

ES using bulk to add data

Dynamic mapping can't add data, don't worry! You can use the bulk command to add data within a JSON file.

  1 define the JSON data file:

{"Index": {"_index": "aaa123", "_id": 1}}{"name": "Xingoo", "Age":}{"index": {"_index": "Aaa123", " _id ": 2}}{" name ":" test111 "," Age ":} {" index ": {" _index ":" aaa123 "," _id ": 3}}{" name ":" Test222 "," Age ": ~{" index ": {" _index ":" aaa123 "," _id ": 4}}{" name ":" test333 "," Age ": 13}

Note that the JSON file name is arbitrarily specified, and the first line defines the index and some characters commonly used segments:

_index defines the name of the index, if you do not specify that you want to add the index name field in the Curl command

_type defines the type of the index, if you do not specify that you want to add the index type field to the Curl command

_ID defines the ID of the row's data and, if not specified, randomly generates a string of numbers.

  2 Execute command

Go to the directory where the JSON file is located, execute the command

Curl Localhost:9200/Index name/index type/_bulk?pretty--data-binary @data. JSON

Note that the following are:

If _index and _type are defined in the JSON file, then this can be turned off (even if the write will follow the build in the JSON file)

Curl Localhost:9200/_bulk?pretty--data-binary @data. JSON

Similarly, if you follow the above definition of _index but do not define _type, then the index is aaa123 and the type is the type specified in our Curl command.

You can see that although the index name bbb123 is specified above, the type is ccc123, but the name of the index named aaa123 is specified in the JSON file.

In the final index file, the index name is aaa123 and the type is ccc123.

Query the index status, you can find normal.

Elasticsearch using the rest API for full-text indexing

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.