Introduction and basic operation of ElasticSearch

Introduction

What is ElasticSearch

ElasticSearch​Short ES, is Apache Lucenebuild-based 开源搜索引擎, and is currently the most popular 企业级搜索引擎. Lucene本身就可以被认为迄今为止性能最好的一款开源搜索引擎工具包, but lucene's API is relatively complex and requires deep search theory. It is difficult to integrate into practical applications. ES是采用java语言编写,提供了简单易用的RestFul API,开发者可以使用其简单的RestFul API,开发相关的搜索功能,从而避免lucene的复杂性.

Core idea

index

An index is a collection of documents with somewhat similar characteristics . For example, you could have an index for product data, an index for order data, and an index for user data. An index is identified by a name (must be all lowercase letters) , and when we want to index, search, update and delete documents in this index, we must use this name.

map

Mapping is the process of defining how a document and the fields it contains are stored and indexed . In the default configuration, ES can automatically create a mapping based on the inserted data, or manually create a mapping. Mapping mainly includes field names, field types, etc.

document

A document is a piece of data stored in an index. A document is the smallest unit that can be indexed . Documents in ES are represented by lightweight JSON format data.

basic operation

index

  1. Indexes in ES have health status: green(healthy), yellow(index available, risky), red(index unavailable)
  2. primaryBy default, ES will create a backup index and an index for the index when creating the index

create

1. Create an index

PUT /索引名

⚠️Note :

  • primaryBy default, ES will create a backup index and an index for the index when creating the index
  • default isyellow

2. Create an index to configure index fragmentation

PUT /goods
{
  "settings": {
    "number_of_shards": 1, 
    "number_of_replicas": 0 
  }
}
  • At this point the index status is green

⚠️Notice:

Versions above ElasticSearch 5.x use text and keyword as string types instead of string types in previous versions.

  • String - text: used for full-text indexing, this type of field will be segmented by a tokenizer, and finally used to build an index
  • String-keyword: No word segmentation, only the complete value of the field can be searched, only used for filtering

textIf the index is created but the mapping result is not set, and then a document of type string is inserted, the field will be mapped to and keywordtype by default

POST /test/_doc
{
  "name":"黄凯宇"
}

ElasticSearch will create the mapping for you by default

{
    
    
  "test" : {
    
    
    "mappings" : {
    
    
      "properties" : {
    
    
        "name" : {
    
    
          "type" : "text",
          "fields" : {
    
    
            "keyword" : {
    
    
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

Based on this mapping, you can not only Filedperform full-text search on the field, but also Filed.keywordrealize keyword search and data aggregation without word segmentation through the field.

Inquire

GET /_cat/indices?v

delete

DELETE /索引名
DELETE /*     #  *代表通配符,代表所有索引

map

Specify the field name and field type in the index. Generally, in the real development environment, the mapping is created manually, which is more in line with the business scenario

create

  • String type: keyword (keyword, keyword), text (a piece of text)
  • Number type: integer long
  • Decimal type: float double
  • Boolean type: boolean
  • Date type: date
PUT /products
{
  "settings": {
    "number_of_replicas": 0,
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "id":{
        "type": "integer"
      },
      "title":{
        "type": "keyword"
      },
      "price":{
        "type": "double"
      },
      "create_at":{
        "type": "date"
      },
      "description":{
        "type": "text"
      }
    }
  }
}
  

Inquire

Query the mapping of an index

GET /索引名/_mapping

document

Add to

Add document operation_—_manually specified_id

POST /products/_doc/1
{
  "id":1,
  "title":"小浣熊",
  "price":0.5,
  "created_at":"2022-04-08",
  "description":"小浣熊真好吃"
}

Automatic generated_id

#添加文档操作 自动生成id  pD5UB4ABiB8dDekOnyy9
POST /products/_doc/
{
  "id":2,
  "title":"猪猪侠",
  "price":0.5,
  "created_at":"2022-04-08",
  "description":"5毛钱一包"
}

Check

Query based on id

GET /索引名/_doc/id

delete

Delete based on id

DELETE /索引名/_doc/id

renew

Delete the original document and add it again (only the updated fields are left after the update)

PUT /索引名/_doc/id
{
   "字段名":"更新后值"
}

Update the document on the basis of the original

POST  /索引名/_doc/id/_update
{
	"doc":{
		"字段名":"更新后值"
	}
}

batch operation

  • It should be noted that no matter how complex the content of the document is, it must be on one line and cannot be broken

batch increase

POST /products/_doc/_bulk

{"index":{"_id":2}}
  {"id":2,"title":"泡面","price":4,"created_at":"2022-04-08","description":"泡面真难吃"}
{"index":{"_id":3}}
  {"id":3,"title":"玉米肠","price":5,"created_at":"2022-04-08","description":"玉米肠真好吃"}

Bulk add/update/delete

POST /products/_doc/_bulk
{"index":{"_id":4}}
  {"id":4,"title":"沙拉","price":6,"created_at":"2022-04-09","description":"不错不错"}
{"update":{"_id":3}}
  {"doc":{"title":"玉米肠+蛋"}}
{"delete":{"_id":2}}

Guess you like

Origin blog.csdn.net/qq_50596778/article/details/125359768