ElasticSearch

  • ElasticSearch is an open source full text search engine that can quickly store, search, and analyze massive amounts of data. SpringBoot provides retrieval functionality through integration with Spring Data ElasticSearch
  • ElasticSearch is a distributed search service that provides RESTful apis. Based on Lucene, ElasticSearch uses multi-shard (sharding) to ensure data security and provides automatic resharding

Use ElasticSearch

  • See the ElasticSearch usage documentation
  • The sample
For the employee directory, we do the following: 1. Each employee indexes a document that contains all information about the employee. 2. Each document will be of type Employee. 3. This type is in index Megacorp. 4. The index is stored in our Elasticsearch cluster.Copy the code
PUT /megacorp/employee/1
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
Copy the code
  • The path/megacorp/employee/1Contains three pieces of information:
    • Megacorp: indicates the index name
    • Employee: type name
    • 1: indicates the ID of a specific employee

Integration of ElasticSearch

  • To introduce the spring – the boot – starter – data – elasticsearch
		<! SpringData ElasticSearch = SpringData ElasticSearch;
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
Copy the code
  • Install ElasticSearch for Spring Data
  • Application. Yml configuration
  • ElasticsearchRepository SpringBoot automatic configuration, ElasticsearchTemplate, Client
  • SpringBoot supports two technologies to interact with ElasticSearch by default:
    • Jest:
      • Default does not take effect
      • Use when you need to import the Jest toolkit: import IO. Searchbox. Client. JestClient;
      Index. Builder(article).index("indexName").type("news").build(); 3. Search search. Builder (json) addIndex (" indexName "). The addType (" news "). The build (); . Perform jetClient. Excute ()Copy the code
    • SpringData ElasticSearch:SpringData ElasticSearch use
      • Client: need to configure the node information: clusterNodes, clusterName
      • ElasticsearchTemplate: Elasticsearch operation
      • Create ElasticsearchRepository interface inherited operation method to operate Elasticsearch
      1. In the application. The properties in the configuration clusterNodes clusterName (version adapter) spring. Data. Elasticsearch. Cluster - nodes = localhost: 9300 spring.data.elasticsearch.cluster-name=elasticsearch 2. Two USES: ElasticsearchTemplate, ElasticsearchRepository ElasticsearchRepository 2.1 2.2 @document (indexName="indexName",type="book"); ElasticsearchRepository :S save(S var1); Iterable<S> saveAll(Iterable<S> var1); Optional<T> findById(ID var1); boolean existsById(ID var1); Iterable<T> findAll(); Iterable<T> findAllById(Iterable<ID> var1); long count(); void deleteById(ID var1); void delete(T var1); void deleteAll(Iterable<? extends T> var1); void deleteAll(); Query expression annotation @query ()Copy the code