I've been using ElasticSearch for over a year now, and I'll share with you what I've learned about it.Copy the code

What is the ElasticSearch

ElasticSearch = ElasticSearch

Elasticsearch is a Lucene-based search server. It provides a distributed multi - user - capable full - text search engine based on RESTful Web interface.Copy the code

The ability of ElasticSearch can be easily abstracted.

So what is Lucene?

Lucene is an open source full text search engine toolkit. ElasticSearch is a Lucene based search engine that is a jar package that you can use in your Own Java project for full text search.Copy the code

Basic concepts for ElasticSearch

ElasticSearch is an example of ElasticSearch.ElastciSearch 5.X and 6.X is the latest version of ElasticSearch now available in 7.15. In the 5.X version, you can have multiple types under an index. This is very similar to the MySQL library/table mapping. In 6.x, only one type can exist under an index. In 7.x, the type is removed, and the relationship with MySQL is as follows.

ElasticSearch does not have a library. Instead, it creates an index that stores data.

shards&replicas

The purpose of shards(sharding) is to evenly distribute index data on different data nodes, and the computing power of multiple machines can be utilized during query, thus improving query efficiency.Replicas back up Shards to prevent data loss. Usually, one or two replicas can be created, and the fragments and copies are stored on different nodes to prevent data loss caused by machine faults.

ElasticSearch installation

  1. ElasticSearch download

Click ElasticSearch. bat to unzip the elasticSearch. bat file

  1. Visual Interface Installation

Visual interface can choose more, the official kibana, cerebro, head third-party.

Kibana is more versatile and complex. Not very friendly to beginners, head function is simple, but the interface is too low. Cerebro is currently the better choice. But bloggers are already used to using Kibana, so here’s how to install it. Select the version of ElasticSearch, unzip it, and run bin/kibana.bat

Note: The installation is only for the Windows standalone installation, not the full cluster mode installation.

Operation command

Create indexes

To create an index, use the PUT operation. The following command creates an index named first_index with 3 fragments and 2 copies. Mapping can be understood as a field. We create two fields named name and age respectively

PUT first_index
{
	 "settings": {
                 "number_of_shards": 3."number_of_replicas": 2
	},
		"mappings": {
			"properties": {
				"name": {
					"type": "text"."index": "true"
				},
				"age": {
					"type": "integer"."index": "true"}}}}Copy the code
Insert data
POST first_index/_doc
{
	"name":"zhangsan"."age":15
}
Copy the code
Query data
GET first_index/_search
{
	"query": {
		"bool": {
			"must": {
				"match": {
					"name": "zhangsan"
				}
			}
		}
	}
}
Copy the code
Delete the data
DELETE first_index/_doc/ data ID DELETE first_index/_doc/WP0KEnwBpx7DePUf8yN6 first_index/_doc/_delete_by_query {"query": {"term": {"name":"zhangsan"}}}Copy the code

More operation commands are no longer introduced here, you can leave a message or study the official website.

The use of ElasticSearch in the project

Currently, Java projects are commonly accessed in the following three ways:

  1. REST Client
  2. Jest
  3. Spring Data

This article uses Spring Data as a demonstration to create projects and add dependencies

<groupId>org.example</groupId>
<artifactId>elastic_search_test</artifactId>
<version>1.0 the SNAPSHOT</version>
<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
</properties>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-parent</artifactId>
    <version>2.5.5</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>
Copy the code

Write entity, the field corresponds to the index field one by one, and the ID field is used to receive data data ID

@Document(indexName = "first_index")
public class User {
    @Id
    private String id;

    private String name;

    private Integer age;
    //getter and setter
}
Copy the code

Write the repository class

@Repository
public interface UserRepository extends ElasticsearchRepository<User.String> {}Copy the code

The configuration file

spring:
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: 127.0. 01.: 9300
      repositories:
        enabled: true
Copy the code

The test class

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class UserServiceTest {
    @Autowired
    private UserRepository repository;
    @Test
    public void selectUser(a){
        User user = repository.findById("Wv2ZFnwBpx7DePUf6SNU").get();
        Assert.assertNotNull("Query data is empty",user);
        Assert.assertTrue("Age mismatch with expectations.",user.getAge() == 15);
        Assert.assertTrue("Name does not match expectations"."zhangsan".equals(user.getName())); }}Copy the code

The test succeeded. This is the end of sharing this article. More on ElasticSearch later!