How to easily and quickly integrate Elasticsearch file retrieval with java springboot

preface:

  • Elasticsearch is a Lucene based search server. It provides a distributed multi-user full-text search engine based on RESTful web interface. Elasticsearch is developed in Java and released as an open source under the Apache license terms. It is a popular enterprise search engine. Designed for cloud computing, it can achieve real-time search, stable, reliable, fast, easy to install and use.
  • We build a website or application and add search function, but it is very difficult to complete the creation of search work. We want the search solution to run fast. We want to have a zero configuration and a completely free search mode. We want to simply use JSON to index data through HTTP. We want our search server to be always available. We want to start from one and expand to hundreds. We want real-time search, we want simple multi tenant, and we want to build a cloud solution. Therefore, we use Elasticsearch to solve all these problems and more other problems that may arise.

1. Preparation

  • Install Elasticsearch and visualization tool Kibana before microservice integration es; Note that the installed versions must be the same, not different versions.

  • It is recommended to use docker for installation. Please move to other posts on how to use docker and docker container installation.
    Installation steps es installation and configuration.

  • Create index and type after Kibana installation:

  • This is the relevant index instruction:

    GET /_cat/indices/?v # query all indexes

    DELETE user # deletes the index

    PUT /user # add index

    PUT /user/userinfo/_mapping # increases the mapping relationship of index types (equivalent to building tables in the database)
    {
    "properties": {
    "id":{
    "type": "long",
    "store": false
    },
    "name":{
    "type": "text",
    "analyzer": "ik_smart",
    "search_analyzer": "ik_smart",
    "store": false
    },
    "city":{
    "type": "text",
    "analyzer": "ik_smart",
    "search_analyzer": "ik_smart",
    "store": false
    },
    "age":{
    "type": "long",
    "store": false
    },
    "remark":{
    "type": "text",
    "analyzer": "ik_smart",
    "search_analyzer": "ik_smart",
    "store": false
    }
    }
    }

2. Introduce dependency

 		 <!--es-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

3. Configuration file configuration

spring:
  application:
    name: test
  data:
    elasticsearch:
      cluster-name: my-application #elasticsearch. Node name of YML configuration
      cluster-nodes: 10.10.0.233:9300 #es installed ip and port

4. Prepare the mapping of the es index type corresponding to the entity class

package com.bwwl.hive.model;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.io.Serializable;

/**
 *@author liuxingying
 *@description
 *@date 2020/9/29
 */
@Data
@Document(indexName = "user",type = "userinfo")  //Corresponding index library and type
public class Userinfo implements Serializable {

    @Id
    private long id;
    @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_smart")
    private String name;
    @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_smart")
    private String city;
    @Field(type = FieldType.Long)
    private Integer age;
    @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_smart")
    private String remark;
}

5. Prepare dao

package com.bwwl.hive.dao;

import com.bwwl.hive.model.Userinfo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

@Repository
//Userinfo corresponds to entity Long corresponds to primary key
public interface ElasticsearchDao extends ElasticsearchRepository<Userinfo,Long> {
}

6. Import database data into es

package com.bwwl.hive.service.impl;

import com.bwwl.hive.dao.ElasticsearchDao;
import com.bwwl.hive.model.Userinfo;
import com.bwwl.hive.service.ElasticsearchService;
import com.bwwl.hive.service.GetData;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *@author liuxingying
 *@description
 *@date 2020/9/29
 */
@Service
public class ElasticsearchServiceImpl implements ElasticsearchService {

    @Autowired
    private GetData getData;
    @Autowired
    private ElasticsearchDao dao;
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    /** @description Import data
     * @params []
     * @return void
     * @author lxy
     * @date 2020/9/29 11:06
     **/
    @Override
    public void importData(){
       //Find out all the data from mysql. Generally speaking, you can find out that it is a javabean object first, and then convert it into the index object userinfo. I use the userinfo object directly here
        List<Userinfo> user = getData.getAll();
        //Import to es
        dao.saveAll(user);
    }
 }

7. Construction of query conditions and implementation of retrieval

package com.bwwl.hive.service.impl;

import com.bwwl.hive.dao.ElasticsearchDao;
import com.bwwl.hive.model.Userinfo;
import com.bwwl.hive.service.ElasticsearchService;
import com.bwwl.hive.service.GetData;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *@author liuxingying
 *@description
 *@date 2020/9/29
 */
@Service
public class ElasticsearchServiceImpl implements ElasticsearchService {

    @Autowired
    private GetData getData;
    @Autowired
    private ElasticsearchDao dao;
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    /** @description Import data
     * @params []
     * @return void
     * @author lxy
     * @date 2020/9/29 11:06
     **/
    @Override
    public void importData(){
        List<Userinfo> user = getData.getAll();
        dao.saveAll(user);
    }

    /** @description es search
     * @params [parm]
     * @return java.util.List<com.bwwl.hive.model.Userinfo>
     * @author lxy
     * @date 2020/9/29 11:32
     **/
    @Override
    public List<Userinfo> search(String parm) {

        //TODO build query object is used to encapsulate various query conditions NativeSearchQueryBuilder
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();

        nativeSearchQueryBuilder.withQuery(QueryBuilders.queryStringQuery(parm).field("name"));

        //Execute query es
        AggregatedPage<Userinfo> page = elasticsearchTemplate.queryForPage(nativeSearchQueryBuilder.build(), Userinfo.class);
        
        //Query result set
        List<Userinfo> list = page.getContent();
        //Total number of query results
        long totalElements = page.getTotalElements();
        //Total pages of query results
        long totalPages = page.getTotalPages();
        return list;
    }
}
  • Finally, just call the interface in the controller layer, and a simple es search will be set up. I only realize simple query and search here. There are many things that have not been realized, such as sorting, paging, range limitation, highlighting and aggregation query. There are many api implementations of ES search methods. You can go to the official website to see how to use it. Welcome to explore.

Tags: Java Back-end

Posted by Nymphetamine on Tue, 19 Apr 2022 10:08:22 +0930