“This is the fourth day of my Participation in the August Challenge. Check out the details:August is more challenging”

The previous article introduced basic syntax for ElasticSearch. This section introduces the second part of ElasticSearch core syntax.

1. Batch operation of documents

1. Batch query document data

Obtain document data in batches using _mget.

1.1 The INDEX and type are not specified in the URL

GET _mget
{
  "docs": [{"_index": "moe_db"."_type": "_doc"."_id": 1
    },
    {
      "_index": "moe_db"."_type": "_doc"."_id": 2}}]Copy the code

The query results are as follows:

1.2 The INDEX is specified in the URL

GET /moe_db/_mget
{
  "docs": [{"_type": "_doc"."_id": 1
    },
    {
      "_type": "_doc"."_id": 2}}]Copy the code

1.3 Specify index and type in the URL

GET /moe_db/_doc/_mget
{
  "docs": [{"_id": 1
    },
    {
      "_id": 2}}]Copy the code

2. Operate document data in batches

You can use _bulk to operate documents in batches

2.1 Creating Data in Batches

Key command: create

POST _bulk
{"create": {"_index":"article"."_type":"_doc"."_id":1}}
{"id":1."title":"The State Media reviews the Game Industry"."content":August 3, 2021 news today, the central media "Economic Information Daily" published an article "Opium of the mind" has grown into hundreds of billions of industry "pointed out that at present, the phenomenon of minors' Internet addiction is widespread, and the impact of online games on the healthy growth of minors can not be underestimated. The article compared online games to a new "drug" and singled out Honor of Kings for criticism.."tags": ["Online games"."Teen"]."create_time":1628073182948}
{"create": {"_index":"article"."_type":"_doc"."_id":2}}
{"id":2."title":"People's Education Press urgent instructions: it is Wu Yifan!"."content":"Wu Yifan, the Pinyin name used in the textbook, is Wu Yifan in the Chinese version of the teacher's book. The pinyin name, which has been in use for 20 years since the first edition of the textbook was approved in 2001, has nothing to do with the artist, who only came to public attention in recent years.."tags": ["Annoying or not."."Everything is boring."]."create_time":1628073182948}
Copy the code

Here are the results:

2.2 Modifying Data in Batches

Key command: update

POST _bulk
{"update": {"_index":"article"."_type":"_doc"."_id":1}}
{"doc": {"title":"What's wrong with online education?"."content":"Among them, New Oriental, good future, high road intraday decline are 60%, as of writing, still fell more than 50%. It's worth noting that the stock was temporarily suspended from trading at $9.63 a share, down more than 53%. At the time of writing, Good Future has resumed trading, and since the resumption of trading, the stock has continued to decline, below $9 per share.."tags": ["Online Education"."Training"]}}
Copy the code

2.3 Deleting Data in Batches

Key command: delete

POST _bulk
{"delete": {"_index":"article"."_type":"_doc"."_id":1}}
Copy the code

Second, DSL query

1. What is DSL?

The full DSL name is short for Domain Specific Language. ElasticSearch can be queried using a JSON-based DSL. (Elasticsearch provides a ful1 Query DSL based on JSON to define queries)

2. DSL classification

DSL can be roughly divided into two types of processing, first a variety of queries, and then the query results aggregated statistics.

2.1 query query

2.1.1 Unconditional Query

By default, an unconditional query queries all.

  1. A:
GET /article/_doc/_search
Copy the code
  1. Method 2:
GET /article/_doc/_search
{
  "query": {
    "match_all": {}}}Copy the code

2.1.2 Conditional query

Conditional query can be divided into single – condition query and multi – condition query.

2.1.2.1 Single-condition query

1. Fuzzy query

In a narrow sense, match can be understood as fuzzy query. Match will perform word segmentation query according to the word segmentation device of this field

POST /article/_doc/_search
{
  "query": {
    "match": {
      "title": "Wu xx"}}}Copy the code

For details, see the match syntax on the official website

2. Precise query

Precise queries are generally as follows:

  • Term: a single condition is equal
  • Terms: The value of a single field in an array of values
  • Range: Indicates the value of the field within a range
  • Exists: Indicates whether the value of a field exists
  • Ids: Batch query by ID

Note: the query effect is not the same without the word segmentation!! For example, if the default word segmentation is not specified, the default word segmentation is standard. For English, the word segmentation is based on space, and for Chinese, the word segmentation is based on character.

POST /moe_db/_doc/_search
{
  "query": {
    "term": {
      "name": "admin"
    }
  }
}

POST /article/_doc/_search
{
  "query": {
    "term": {
      "title": "Wu"}}}Copy the code

2.1.2.2 Query by Multiple criteria

Multi – condition query is a complete query condition formed by combining multiple single – condition query statements.

  • Bool: the conditions have the relationship of and,or or not
    • Must: All the conditions must be met
    • Should: If one of the conditions is satisfied, the relationship between the conditions is or
    • Must_not: A relationship in which all conditions are not met, that is, the conditions are not
    • Filter: Does not calculate the correlation score. It does not calculate _score, that is, the correlation score, which is more efficient
  • Constant_score: The correlation score is not calculated

Third, summary

There are a lot of syntax for ElasticSearch queries, especially DSL. This article starts with the batch operation document command for ElasticSearch, and then introduces DSL queries. Considering the length and content of THE ElasticSearch DSL, we will introduce DSL queries in sections.

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