Integrating Elasticsearch with Java SpringBoot

Keywords: Java ElasticSearch Spring Boot

Integrating Elasticsearch with Java SpringBoot

Create a project and import dependencies into pom.xml:

    <!-- SpringBoot Core package -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>Import version number</version>
    </dependency>
    
	<!-- SpringBoot test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- SpringBoot link es package-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        <version>Import version number (to and) spring-boot Version matching (upper)</version> 
    </dependency>

application.yml configuration:

​spring:
	  data:
	    elasticsearch:
	    	# ip address: port number
	      cluster-nodes: "localhost:9300" 
	      cluster-name: "dawnopus-cluster"
	      repositories:
	        enabled: true

Entity class:

@Data
@Document(indexName = "searchindex", type = "bond")
public class Data {

	// @Id: key unique identifier
    @Id
    private String id;
    
    
    // @Field(type = FieldType.Text):  string
    @Field(type = FieldType.Text)
    private String name;
    
    }

Persistence layer (DAO)

import com.ruoyi.project.credit.es.domain.Data;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import org.springframework.data.domain.Pageable;

@Repository
public interface DataRepository extends ElasticsearchRepository<Data, String> {


	//Query by name
    List<Data> findByName(String name);


	//PageRequest pageRequest = PageRequest.of(0, 5) is queried by name;
    List<Data> findByName(String name,Pageable pageable);


	//The paging query is linked with And Or according to id And name
	List<Data> findByIdAndName(String id,String name,Pageable pageable);
}

service layer interface

public interface DataService{

	//Query by name
    List<Data> findByName(String name);

	//PageRequest pageRequest = PageRequest.of(0, 5) is queried by name;
    List<Data> findByName(String name,Pageable pageable);

	//The paging query is linked with And Or according to id And name
	List<Data> findByIdAndName(String id,String name,Pageable pageable);
}

service layer implementation

@Component
public class DataServiceImpl implements DataService{

	//Persistence layer (DAO)
    @Autowired
    private DataRepository dataRepository ;

	//Query by name
    @Override
    public List<Data> findByName(String name){
    return  dataRepository.findByName(name);
	}


	//Paging query by name      
    @Override
    public List<Data> findByName(String name,Pageable pageable){
   		
   		//Paging 0-5
 		PageRequest pageRequest = PageRequest.of(0, 5);
 		
        return dataRepository.findByName(name,pageRequest);
        
    }


	//The paging query is linked with And Or according to id And name
    @Override
    public List<Data> findByIdAndName(String id,String name,Pageable pageable){
    
       		//Paging 0-5
 		PageRequest pageRequest = PageRequest.of(0, 5);
 		
        return dataRepository.findByName(id,name,pageRequest);
    }

}

Creating a reasonable title is helpful to the generation of the directory

Directly input once #, and press space to generate level 1 title.
After entering twice #, and pressing space, a level 2 title will be generated.
By analogy, we support level 6 titles. It helps to generate a perfect directory after using TOC syntax.

How to change the style of text

Emphasize text emphasize text

Bold text bold text

Tag text

Delete text

Reference text

H2O is a liquid.

210 the result is 1024

Insert links and pictures

Link: link.

Picture:

Pictures with dimensions:

Centered picture:

Centered and sized picture:

Of course, in order to make users more convenient, we have added the image drag function.

How to insert a beautiful piece of code

go Blog settings Page, select a code slice highlighting style you like, and the same highlighted code slice is shown below

// An highlighted block
var foo = 'bar';

Generate a list that suits you

  • project
    • project
      • project
  1. Item 1
  2. Item 2
  3. Item 3
  • Planning tasks
  • Complete the task

Create a table

A simple table is created as follows:

projectValue
computer$1600
mobile phone$12
catheter$1

The setting content is centered, left and right

Use: ---------: Center
Use: --------- left
Usage -----------: right

First columnSecond columnThird column
First column text centeredThe text in the second column is on the rightThe text in the third column is left

SmartyPants

SmartyPants converts ASCII punctuation characters to "smart" printed punctuation HTML entities. For example:

TYPEASCIIHTML
Single backticks'Isn't this fun?''Isn't this fun?'
Quotes"Isn't this fun?""Isn't this fun?"
Dashes-- is en-dash, --- is em-dash– is en-dash, — is em-dash

Create a custom list

Markdown Text-to- HTML conversion tool Authors John Luke

How to create a footnote

A text with footnotes. 1

Annotations are also essential

Markdown converts text to HTML.

KaTeX mathematical formula

You can render LaTeX mathematical expressions using KaTeX:

Gamma formula display Γ ( n ) = ( n − 1 ) ! ∀ n ∈ N \Gamma(n) = (n-1)!\quad\forall n\in\mathbb N Γ (n)=(n−1)! ∀ N ∈ N is through Euler integral

Γ ( z ) = ∫ 0 ∞ t z − 1 e − t d t   . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,. Γ(z)=∫0∞​tz−1e−tdt.

You can find more information about LaTeX mathematical expressions here.

New Gantt chart features to enrich your articles

  • For Gantt chart syntax, refer to here,

UML diagram

UML diagrams can be used for rendering. Mermaid For example, a sequence diagram is generated as follows:

This will produce a flowchart.:

  • For Mermaid syntax, see here,

FLowchart

We will still support the flowchart of flowchart:

  • For Flowchart syntax, refer to here.

Export and import

export

If you want to try this editor, you can edit it at will in this article. When you have finished writing an article, find the article export in the upper toolbar and generate a. md file or. html file for local saving.

Import

If you want to load an. md file you have written, you can select the import function in the upper toolbar to import the file with the corresponding extension,
Continue your creation.

  1. Explanation of footnotes ↩︎

Posted by powerspike on Wed, 03 Nov 2021 18:55:50 -0700