ElasticSearch learning

The version of each tool must be the same

The installation of the ElasticSearch

1. Download the installation package and decompress it

Select * from config where elasticsearch.yml = >

http.cors.enabled: true
http.cors.allow-origin: "*"
Copy the code

3. Open the elasticSearch. bat file in bin

4, http://127.0.0.1:9200/

5. An index in ES is a database

ElasticSearch – head installed

1. Download and decompress (data display tool)

2. In the installation path CNPM install

3, NPM run start

4, http://localhost:9100/

Kibana installation

1. Unpack

2. Sinicization

Add in E:\ElasticSearch\ Kibana-7.6.1-windows-x86_64 \config\kibana.yml

i18n.locale: "zh-CN"
Copy the code

3. Start Kibana.bat

4, test,

ES Core Concepts

An overview of the

Es is document-facing! Everything is JSON

Relational DB ES
The database The index
table Types (mapping types)
line Documents (a data record, the smallest unit of ES search)
field fields

Physical design

Es divides each index into shards behind the scenes, and each shard is migrated between different servers in the cluster

The default cluster name is ElasticSearch

Logic design

Contains multiple documents in an index type. When indexing a document, it can be found by index -> Type -> document ID. Id doesn’t have to be an integer; it’s actually a string.

Inverted index: the key to improve retrieval efficiency

The original data Index list (inverted index)
Blog Post ID Tag (Token) The label Blog Post ID
1 python python 1,2,3
2 python linux 3, 4
3 Linux, python
4 linux

IK participle

Unzip the installation package into the plugins folder in ES

Then use The Kibana test

Ik_max_word finer-grained partition

Ik_smart is the least sharded

But there’s a problem:

At this point, you need to manually add Zhan Xiaonan to the dictionary.

Add dict to ik/config file (UTF-8)

And modify the configuration in ikAnalyzer.cfg.xml

And then restart es

Rest-style basic operations on indexes

1, create an index. If no type is specified, es will set the default type

PUT/index name /~ Type name ~/ document ID {request body}Copy the code

/2. Create index only

3. Get index information

The GET index nameCopy the code

GET _cat for more informationCopy the code

4. Update indexes

POST/Index name /~ Type name ~/ Document ID /_updateCopy the code

5, drop index

The DELETE index nameCopy the code

Operations on documents

Simple search

GET Index name/type name /_search? Q = Query condition GET Index name/type name /_search? q=name:spbCopy the code

Complex search

GET index name/type name /_search {"query": {"match": {// Query conditions
			"name": "Crazy god"}},// Result filter, only display fields
    "_source": ["name"."desc"]}// All queriesGET index name/type name /_search {"query": {"match_all": {}}}Copy the code

Sort: ==

== paging: ==

Must == and should == or must_not ==! = = =

== Filter conditions ==

== Exact query: ==

Term: Precisely searched directly by the term process specified by the inverted index. (Just change match to term)

Match: can be resolved using a word splitter.

The two types text (which will be parsed and split by the parser) and keyword (which will not be parsed by the parser)

== Highlight query: ==

Integrated SpringBoot

Custom version

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

Copy the code

Operations on indexes:

ElasticSearchClientConfig.java

package com.spb.compare.config;


import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticSearchClientConfig {

    // The object needs to be injected into Spring
    //
      
    @Bean
    public RestHighLevelClient restHighLevelClient(a){
        RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(
                new HttpHost("127.0.0.1" , 9200 , "http")));
        returnclient; }}Copy the code

The test class:

package com.spb.compare;

import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
class CompareApplicationTests {

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    // Test index creation CreateIndexRequest
    @Test
    void testCreate(a) throws IOException {
        // create index request
        CreateIndexRequest request = new CreateIndexRequest("kuang_index");
        //2. Create a request for IndicesClient and get a response
        CreateIndexResponse createIndexResponse =
                client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse.index());
    }

    // Test GetIndexRequest to see if it exists
    @Test
    void testExistIndex(a) throws IOException {
        GetIndexRequest request = new GetIndexRequest("kuang_index");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }


    // Test index drop
    @Test
    void testDeleteIndex(a) throws IOException{
        DeleteIndexRequest request = new DeleteIndexRequest("kuang_index");
        / / delete
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
        // If true is returned, the deletion succeeds
        System.out.println(delete.isAcknowledged());
    }

    @Test
    void contextLoads(a) {}}Copy the code

Operations on documents

package com.spb.compare;

import com.alibaba.fastjson.JSON;
import com.spb.compare.entity.TestUser;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

@SpringBootTest
class CompareApplicationTests {

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    // Test index creation CreateIndexRequest
    @Test
    void testCreate(a) throws IOException {
        // create index request
        CreateIndexRequest request = new CreateIndexRequest("kuang_index");
        //2. Create a request for IndicesClient and get a response
        CreateIndexResponse createIndexResponse =
                client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse.index());
    }

    // Test GetIndexRequest to see if it exists
    @Test
    void testExistIndex(a) throws IOException {
        GetIndexRequest request = new GetIndexRequest("kuang_index");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }


    // Test index drop
    @Test
    void testDeleteIndex(a) throws IOException{
        DeleteIndexRequest request = new DeleteIndexRequest("kuang_index");
        / / delete
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
        // If true is returned, the deletion succeeds
        System.out.println(delete.isAcknowledged());
    }

    // Test adding documents
    @Test
    void testAddDocument(a) throws IOException {
        // Create an object
        TestUser testUser = new TestUser("Said the Mad God." , 20);
        // Create the request
        IndexRequest request = new IndexRequest("kuang_index");
        // rule put kuang_index/_doc/id
        request.id("1");
        request.timeout("1s");

        // The data is put into a JSON format
        IndexRequest source = request.source(JSON.toJSONString(testUser), XContentType.JSON);

        // The client sends the request
        IndexResponse index = client.index(request, RequestOptions.DEFAULT);

        System.out.println(index.toString());
        System.out.println(index.status());
    }

    // Get the document and check if it exists
    @Test
    void testIsExists(a) throws IOException {
        GetRequest getRequest = new GetRequest("kuang_index"."1");
        // Don't get the context of the returned _source
        getRequest.fetchSourceContext(new FetchSourceContext(false));
        getRequest.storedFields("_none_");

        boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    // Get the document information
    @Test
    void testGetDocument(a) throws IOException {
        GetRequest getRequest = new GetRequest("kuang_index"."1");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        // Print the contents of the document
        System.out.println(getResponse.getSourceAsString());
        System.out.println(getResponse);
    }


    // Update document information
    @Test
    void testUpdateDocument(a) throws IOException {
        UpdateRequest updateRequest = new UpdateRequest("kuang_index"."1");
        updateRequest.timeout("1s");
        TestUser testUser = new TestUser("Su Pengbo".18);
        // Update the rule
        updateRequest.doc(JSON.toJSONString(testUser) , XContentType.JSON);
        / / execution
        UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);

        System.out.println(updateResponse.status());

    }


    // Delete the document information
    @Test
    void testDeleteDocument(a) throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("kuang_index"."1");
        deleteRequest.timeout("1s");
        DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(deleteResponse.status());
    }

    // Batch insert data
    @Test
    void testBulkRequest(a) throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10s");

        ArrayList<TestUser> userList = new ArrayList<>();
        userList.add(new TestUser("spb1" , 3));
        userList.add(new TestUser("spb2" , 3));
        userList.add(new TestUser("spb3" , 3));
        userList.add(new TestUser("spb4" , 3));
        userList.add(new TestUser("spb5" , 3));
        userList.add(new TestUser("spb6" , 3));
        userList.add(new TestUser("spb7" , 3));

        for (int i = 0; i < userList.size(); i++) {
            // Update and delete
            bulkRequest.add(new IndexRequest("kuang_index")
                    .id(""+(i+1))
                    .source(JSON.toJSONString(userList.get(i)) , XContentType.JSON));
        }

        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkResponse.hasFailures());Success is achieved if false is returned


    }


    / / query
    //searchRequest searchRequest
    //SearchSourceBuilder Conditional construct
    //HighlightBuilder component highlight
    //TermQueryBuilder for precise queries
    // MatchAllQueryBuilder matches all
    // XXX QueryBuilder implements the query
    @Test
    void testSearch(a) throws IOException {
        // Search requests
        SearchRequest searchRequest = new SearchRequest("kuang_index");
        // Component search criteria
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // Query criteria can be implemented using the QueryBuilders tool
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name"."spb1");

        sourceBuilder.query(termQueryBuilder);
        / / paging

        sourceBuilder.from();
        sourceBuilder.size();

        sourceBuilder.timeout(new TimeValue(60 , TimeUnit.SECONDS));

        // Submit the condition to the request
        searchRequest.source(sourceBuilder);

        // Execute the request
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(searchResponse.getHits()));
        System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
        for(SearchHit documentFields : searchResponse.getHits()) { System.out.println(documentFields.getSourceAsMap()); }}@Test
    void contextLoads(a) {}}Copy the code