34. springboot——springboot and retrieval (1)

1. Search

Our applications often need to add search functions, and the open source ElasticSearch is currently the first choice for full-text search engines.
He can quickly store, search and analyze massive data. Spring Boot integrates Spring Data ElasticSearch
Provide us with very convenient search function support; 
 
Elasticsearch is a distributed search service that provides Restful API, the bottom layer is based on Lucene,
Multi-shard (sharding) method is used to ensure data security and provide automatic resharding.
GitHub and other large sites also use ElasticSearch as their search service,

二、ElasticSearch 

1. Use docker to download ElasticSearch 

2. Run ElasticSearch 

docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name es1 4f7e4c61f09d

The -e here is to set the memory when initializing, and when it is not set, it needs a larger memory when turned on! ! !

test:

The above picture and text indicates that the service is successfully opened! !

3. Elasticsearch related usage and usage

 

Elasticsearch is document-oriented, meaning it stores entire objects or documents. Elasticsearch not only stores documents,
And _index the content of each document so that it can be retrieved. In Elasticsearch, you index documents,
Retrieval, sorting and filtering-not for row and column data. This is a completely different way of thinking about data,
This is why Elasticsearch can support complex full-text search.

 

JSON editing

Elasticsearch uses JavaScript Object Notation or  JSON  as the serialization format of the document.
JSON serialization is supported by most programming languages, and has become the standard format in the field of NoSQL. It's simple, concise, and easy to read
 
An Elasticsearch cluster can contain multiple indexes, and each index can contain multiple types.
 These different types store multiple documents, and each document has multiple attributes.

For the employee directory, we will do the following:
  • Each employee indexes a document that contains all information about the employee.
  • Each document will be of employee type.
  • This type is located in the index megacorp.
  • The index is saved in our Elasticsearch cluster.
megacorp
Index name
employee
type name
1
ID of a specific employee
test:

 

The corresponding at this time:

 

At this time, two pieces of data are added:

 

 

Retrieve documents

At present, we have stored some data in Elasticsearch, and then we can focus on implementing the business needs of the application.
The first requirement is that individual employee data can be retrieved.
This is very simple in Elasticsearch. Simply perform an HTTP GET request and specify the address of the document-index library,
Type and ID. Use these three pieces of information to return the original JSON document:
GET /megacorp/employee/1

 

HEAD is to check whether it exists
DELETA is to delete
 
Changing the HTTP command from PUT to GET can be used to retrieve documents. Similarly,
You can use the DELETE command to delete the document, and use the HEAD command to check whether the document exists.
If you want to update an existing document, just PUT again.

 

Lightweight search

A GET is quite simple, you can directly get the specified document. Now try some more advanced features,
For example, a simple search!
The first attempt was almost the simplest search. We use the following request to search for all employees: GET / megacorp / employee / _search
    192.168.1.118:9200/megacorp/employee/_search
Note: The returned result not only tells which documents are matched, but also contains the entire document itself: all the information needed to display the search results to the end user.
 

Guess you like

Origin www.cnblogs.com/lyh233/p/12688030.html