Search engine Elasticsearch REST API Learning

Source: Internet
Author: User

Elasticsearch provides developers with a set of restful interfaces based on the HTTP protocol, and only needs to construct a rest request and parse the JSON returned by the request for access to the Elasticsearch server. Elasticsearch API interface is rich in functionality, including clustering, monitoring, deployment management, etc., also contains commonly used documents, index operations, this article is mainly using the Postman tool for document operations related to the API to do the learning record.

New Document

Put: HTTP://LOCALHOST:9200/STORE/PRODUCT/1//Specify ID, presence is updated, not present new

Post mode: http://localhost:9200/store/product//auto-Generate ID

Where store indicates that index,product represents type, the number 1 represents the primary key of document, the primary key can be any form, or the primary key is not specified, Elasticsearch will automatically generate a unique primary key. However, the request must be submitted in post mode.

Test:

Return results in _id: auto-generated primary key, _shards: For partition information, total:2 for two partitions, successful:1 for successfully assigned a copy. _version:1 represents the version number of the document, each time it is modified, and the version number is automatically increased.

View data in Elasticseach-head:

Query based on PRIMARY key

Get mode: Http://localhost:9200/store/product/nMWgs2UBQISnN0EoTXZL Elasticserach provides a restful API that is easy to understand in this form of interface.

The returned results in _source represent the returned Doucment (document) class tolerance, and several others are Elasticsearch document structure fields. If you need only _source content that does not require additional structure fields, you can also add the attribute "/_source" to the request URL, returning only the source portion of the volume, request: Http://localhost:9200/store/product/1/_source

Update documents based on primary key

In Elasticsearch Chinese documents are immutable and cannot be modified. If you want to update an existing document, you need to rebuild the index or replace it, update the interface with the new document interface, using the/index/type/key form. Elasticsearch is implemented by marking the old document as deleted and adding a new document. Documents marked for deletion will no longer be able to operate directly, but it will not disappear immediately, and elasticsearch will clean up those documents that have been marked for deletion in the background.

If you know exactly what part of the document is being modified, you can also update the data by adding "/_update" after the request URL above and using post, although this looks like a direct update and actually re-creates a new document. The process is to first build the JSON data from the document you are working on, modify the JSON data based on the fields that you requested to modify, and then mark the document as deleted and add a new document. The request is as follows:

If you modify the document concurrently, you can use the version field for the real optimistic lock, the revision of the target document is versioned and the incoming inconsistency, then the modification fails, and the request url:http://localhost:9200/store/product/1/_update?version=4

Delete based on primary key

Use delete mode to request format Host/index/type/key

Search Documents

Elasticsearch provides a powerful search function, the search parameters can be placed on the request URL, can also be placed in the body, request the basic format: Host/index/type/_search

    1. Get mode + URL parameter for full-text search

The _score in the returned results is the concept of a search engine, indicating relevance, the higher the score, the higher the match between this document and the search criteria keyword.

2. Full-text search using the match expression

3. Elasticsearch also supports paging queries, specifying page numbers and page sizes using the From and size two parameters.

Refer to the official documentation for more detailed instructions:

English Document: www.elastic.co/guide/cn/elasticsearch/guide/current/index.html

English documents:

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.