Moment For Technology

The ElasticSearch ElasticSearch ElasticSearch ElasticSearch ElasticSearch ElasticSearch ElasticSearch ElasticSearch ElasticSearch

Posted on Dec. 3, 2022, 8:54 a.m. by Tushar Chopra
Category: The back-end Tag: The back-end elasticsearch

"This is the third day of my Participation in the August More Text Challenge. Check out the details:August is more challenging"

The previous article describes how to build an ElasticSearch environment. This section describes how to use basic ElasticSearch syntax.

Introduction to ElasticSearch

1. Default word segmentation

Es default word segmentation is standard, English word segmentation by space, but Chinese word segmentation by character. As follows:

POST _analyze
  "analyzer": "standard"."text": "I love You Zoe"
Copy the code

2. Ik participle

2.1 Coarsely grained word segmentation

Ik_smart: will do the coarsest grained split.

POST _analyze
  "analyzer": "ik_smart"."text": "I love You Zoe"
Copy the code

2.2 Fine-grained word segmentation

Ik_max_word: will split the text in the finest granularity.

POST _analyze
  "analyzer": "ik_max_word"."text": "I love You Zoe"
Copy the code

2, A Resultful idea

1. "What is Resultful?"

A Resultful style is a software architecture style, rather than a standard, that provides a set of design principles and constraints. It is mainly used for the interaction between the client and the server. Is based on the HTTP protocol implementation. The purpose is to improve the scalability of the system, reduce the coupling between applications, and facilitate the framework of distributed processors. Software based on this style can be simpler and more hierarchical.

  • GET, corresponding to SELECT: queries from the server. You can distinguish the query mode based on the request parameters on the server.
  • POST, corresponding to Create: Create a new resource on the server and call insert.
  • PUT, corresponding to the update operation: Update the resource on the server and call the update operation.
  • DELETE, corresponding to the DELETE operation, deletes the resource from the server, calls the DELETE statement.

2. Why use Resultful?

  • Resource oriented
  • Lightweight, directly based on HTTP
  • Data description is simple, generally in XML, JSON to do data exchange

Basic Operations for ElasticSearch

For example, the type of ElasticSearch is similar to a table in a relational database. Earlier versions of ElasticSearch specify a specific table. Later versions of ElasticSearch change it to _doc. The higher version has marked _doc as obsolete (#! Deprecation: [types removal] Specifying types in search requests is deprecated.)

1. Index operation

Index can be understood as a relational database.

1.1 Creating an Index

PUT/Index name

PUT /moe_db
Copy the code

1.2 Querying the Index

GET/Index name

GET /moe_db
Copy the code

1.3 Deleting an Index

DELETE/Index name

DELETE /moe_db
Copy the code

2. Document operations

A document can be understood as a Row of data in a relational database.

2.1 Adding a Document

PUT/Index name/type/ID

PUT /moe_db/_doc/1
  "name": "Wu Mou Fan"."sex": 1."age": 30."address": "A district in Beijing"."remark": "Canadian, hahaha."

PUT /moe_db/_doc/2
  "name": "Pig"."sex": 1."age": 28."address": "Taiwan"."remark": "Hehe hehe"
Copy the code

2.2 Modifying documents

PUT/Index name/type/ID

Update if the ID exists.

PUT /moe_db/_doc/1
  "name": "Zhang"."sex": 1."age": 30."address": "Chaoyang District, Beijing"."remark": "Ha-ha-ha."
Copy the code

2.3 Querying Documents

GET/Index name/type/ID

GET /moe_db/_doc/1
Copy the code

2.4 Deleting a Document

DELETE/Index name/type/ID

DELETE /moe_db/_doc/2
Copy the code

3. Perform simple query operations

3.1 Querying all documents of the current type

To query all documents, the corresponding relational database is to query all table records.

select * from xxx;

GET/Index name/type /_search

GET /moe_db/_doc/_search
Copy the code

3.2 Search Criteria

3.2.1 Equal Query

Select * from relational database where age = 30 and age = 30;

select * from xxx where age = 30;

GET/Index name/type /_search? q=:**

GET /moe_db/_doc/_search? q=age:30
Copy the code

3.2.2 Greater than query

Example Query records whose age is older than 18. GET/Index name/type /_search? q=age:**

GET /moe_db/_doc/_search? q=age:18
Copy the code

3.2.3 Less than query

Example Query records that are younger than 35.

GET/Index name/type /_search? q=age:**

GET /moe_db/_doc/_search? q=age:30
Copy the code

3.3 Range Query

Select * from _search where age is 25 to 30 Q =*[TO **] Note: TO must be uppercase.

select * from xxx where age between 25 and 30;

GET/Index name/type /_search? q=***[25 TO 26]

GET /moe_db/_doc/_search? q=age[25 TO 30]
Copy the code

3.4 Batch Query

Example Batch query _mget based on multiple ids

GET/Index name/type / _mGET

GET /moe_db/_doc/_mget
{"ids": [1.2]}
Copy the code

3.5 Paging Query


GET/Index name/type /_search? from=0size=10

GET /moe_db/_doc/_search? from=0size=10
Copy the code

3.6 Querying Records of a Specific Field

GET/Index name/type /_search? _source= field, field

GET /moe_db/_doc/_search? _source=name,ageCopy the code

3.7 the sorting

GET/Index name/type /_search? Sort desc = field

GET /moe_db/_doc/_search? sort=age:descCopy the code


This article begins with an introduction to the ES default word participle and how to use the IK word participle. Then I introduced the SIMPLE, Resultful style basic operation API of ES. First learn simple query, then further study ES complex query.

Welcome to follow the wechat official account (MarkZoe) to learn from each other and communicate with each other.

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.