ElasticSearch

  • ElasticSearch is an open source full-text search engine that allows you to quickly store, search and analyze huge amounts of data. SpringBoot provides search support by integrating Spring Data ElasticSearch
  • ElasticSearch is a distributed search service that provides RESTful APIs. The bottom layer is based on Lucene. It uses multiple shards (sharding) to ensure data security and provides automatic resharding

    Use ElasticSearch

  • Refer to the Elasticsearch usage documentation
  • The sample

    For the employee directory, we will do the following: 1. Each employee indexes a document that contains all the information of the employee. 2. Each document will be of type Employee. 3. This type is in the index MEGACORP. 4. This index is saved in our Elasticsearch cluster.
    PUT /megacorp/employee/1
    {
      "first_name" : "John",
      "last_name" :  "Smith",
      "age" :        25,
      "about" :      "I love to go rock climbing",
      "interests": [ "sports", "music" ]
    }
  • The path /megacorp/employee/1 contains three parts of information:

    • Megacorp: Index name
    • Employee: Type name
    • 1: ID of a specific employee

      Integration of ElasticSearch

  • To introduce the spring – the boot – starter – data – elasticsearch

    <! Springframework. boot</ grouppid > -- SpringData ElasticSearch --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
  • Install the corresponding version of ElasticSearch for Spring Data
  • Application. Yml configuration
  • ElasticsearchRepository SpringBoot automatic configuration, ElasticsearchTemplate, Client
  • By default, SpringBoot supports two technologies to interact with Elasticsearch:

    • Jest:

      • Default does not work
      • 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 ()
    • SpringData ElasticSearch:SpringData ElasticSearch is used

      • Client: need to configure the node information: clusterNodes, clusterName
      • ElasticsearchTemplate: Elasticsearch operation
      • }}}}}}}}}}}}}}}}}}}}

        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") build index: BookRespository.index (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()