Operational commands for elasticsearch indexes (core concepts, operational statements, and paging)

Keywords: Database SQL less JSON

Article Directory

1. Description of put, post, delete, get methods

method describe
PUT Establish
POST Create, modify
DELETE delete
GET query

2. Data Models and Core Concepts

1. Data Model
Index: An index consisting of multiple Document s, equivalent to a database in a relational database
Type: Index type, which supports only one in 6.x and will be gradually removed, equivalent to tables in relational databases
Document: Document, consisting of multiple Field s, equivalent to rows in a relational database
Field: A field, including field names and values, that corresponds to a field in a relational database (columns)
(2) Document management: Document is the smallest data unit of ES

  • Raw data
    _source: original JSON format document
  • document metadata
    _Index: index name
    _Type: index type
    _id: document number
    _Version: Document version number for concurrency control
    _score: Rating in search results

3. Basic operation of index

Note: Index precedes / can add or exclude

1. Create an index

PUT/Index Name/~Type Name~/Document id {Requestor}
For example:
PUT test/type/1
{
  "name":"tom",
  "interest":"play"
}

2. View all current indexes

GET _cat/indices

3. Modify (update) the document

Method one: (The value will be overwritten if it is not passed)

#The same document id submission allows overwriting to complete modifications
PUT /test/type/1
{
  "name":"tom",
  "interest":"learn"
}

Method 2: (More flexibility by simply editing updated data)

POST /test/type/1/_update
{
  "doc": {
  "interest":"dance"
  }
}

4. Delete

#Delete specified number document
DELETE test/type/1
#Delete entire index
DELETE test

5. Add data

PUT test/type/1
{
  "name":"tom",
  "interest":"learn",
  "gender":"male",
  "age":"18"
}

6. Bulk operation data bulk

#Batch operation steps
PUT _bulk
{"index/create/update/delete":{"_index":"...","_type":"...","_id":"..."}}
{"field1":"value1",......}
#Take inserting data as an example
PUT _bulk
{"create":{"_index":"student01","_type":"student","_id":"1"}}
{"id":"1","name":"zhangsan","join_date":"2014","interest":"basketball"}
{"create":{"_index":"student01","_type":"student","_id":"2"}}
{"id":"2","name":"join","join_date":"2017","interest":"boxing"}
{"create":{"_index":"student01","_type":"student","_id":"3"}}
{"id":"3","name":"John Kerry","join_date":"2016","interest":"cooking"}

8. Bulk Read Documents

Mode 1:

GET student01/student/_mget
{"docs":[{"_id":"1"},{"_id":"2"}]}

Mode 2:

GET student01/_mget
{"docs":[{"_type":"student","_id":"1"},{"_type":"student","_id":"2"}]}

Mode 3: (Tables with different indexes can be read)

GET _mget
{"docs":[{"_index":"student01","_type":"student","_id":"1"},{"_index":"test","_type":"type","_id":"1"}]}

9. Search for data

1) Simple Query
1. Search for specified number of data

GET test/type/1/

(2) Conditional queries (when there are multiple pieces of data in the index)

GET test/type/_search?q=name:tom

2) Complex Query
1 match query

  • match_all: return all documents
GET test/_search
{
    "query": {
        "match_all": {}
    }
}
  • Match: Boolean match query (partitions the query string and constructs a Boolean query based on the result of partitioning)
GET student01/_search
{
    "query": {
        "match": {
          "name":"John Kerry"
        }
    }
}
  • match_phrase: phrase matching query (for word breaking in query string, field values must match all word breaks in turn, note that the position of each word break cannot be changed)
GET student01/_search
{
    "query": {
        "match_phrase": {
          "name":"John Kerry"
        }
    }
}
  • match_phrase_prefix: Phrase prefix matching query (similar to match_phrase, but the last participle matches as a prefix)
GET student01/_search
{
    "query": {
       "match_phrase_prefix": {
          "name":"John Ke"
        }
    }
}
  • multi_match: multi-field match query
GET student01/_search
{
     "query": {
        "multi_match": {
          "query": "John like cooking",
          "fields": ["name","interest"]
        }
    }
}

(term query: matching fields according to exact words stored in the inverted index)

GET student01/_search
{
     "query": {
         "term":{
          "name":"john"    #Note that values are all lowercase
        }
    }
}

(3) terms query (Term query: matching fields with multiple entries according to exact words stored in the inverted index)

GET student01/_search
{ "query": {
        "terms":{
          "name":["john","da"]   #Note that values are all lowercase
        }
    }
}

(4) range Query (range Query)

gt greater than
gte greater than or equal to
lt less than
lte is less than or equal to!

GET student01/_search
{"query": {
     "range" : {
            "join_date" : {
                "gte" : 2016,
                "lte" : 2017
            }
        }
    }
}

fliter filter (filter is placed in bool query)

GET student01/_search
{"query": {
  "bool": {
  "filter": {
     "range": {
        "join_date": {
          "gte": 2016
        }
      }
  }
  }
}
}

Sort sort

GET student01/_search
{"query": {
  "match_all": {
  }
  },
  "sort": {
    "id":"desc"
  }
}

bool query (compound query clause)

  • List itemmust is equivalent to sql query where and, all conditions must be met where name = join and interest = xxx
  • Should is equivalent to or inside sql query where, all conditions must be met where name = 1 or interest = xxx [minimum_should_match represents the minimum matching should condition)
  • should is equivalent to not inside sql query where
#Please query the Student Information Index "student01" for documents that meet the following requirements
#Name "john"
#Enrollment after 2016
#Hobby "boxing"
GET student01/student/_search
{"query":{
  "bool": {
    "must": [
      {"match": {
      "name.keyword": "join"
    }},{
      "match": {
        "interest": "boxing"
      }
    }
    ],
    "filter": {
      "range": {
        "join_date": {
          "gte": 2016
        }
      }
    }
  }
}
}

4. Paging

1. Light Paging (from+size)

Method 1: size: Page capacity from: Data subscript starts from 0, indicating which data to start from

GET student01/_search?size=5&from=2

Method 2:

GET student01/_search
{
  "query": {
    "match_all": {}
  },
  "size": 5,
  "from": 2
}

2. Deep Paging

1 Return to scroll_id with first page content, scroll_id 5 minutes valid

GET stu/_search?scroll=5m
{
  "from": 0,
  "size": 5,
  "query": {"match_all": {}}
}

(2) According to scroll_id keeps getting the next page

GET _search/scroll
{
  "scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAANy......",
  "scroll": "5m"
}

Posted by Nukeum66 on Tue, 23 Jun 2020 09:06:32 -0700