Basic query operation of elasticsearch

Posted by sciencebear on Sun, 06 Feb 2022 22:29:28 +0100

Basic operation of es

1. Create es_db index, and set the default word segmentation method of the index to ik_max_word

PUT /es_db
{
  "settings": {
    "index": {
      "analysis.analyzer.default.type": "ik_max_word"
    }
  }
}

2. Basic operations for index

GET /es_db
DELETE /es_db

3. Add document

PUT /es_db/_doc/1
{
  "name": "Zhang San",
  "sex": 1,
  "age": 23,
  "address": "Guangzhou Tianhe Park"
}
PUT /es_db/_doc/2
{
  "name": "Zhang San",
  "sex": 0,
  "age": 43,
  "address": "Guangzhou Tianhe Tangxia"
}
PUT /es_db/_doc/3
{
  "name": "Li Si",
  "sex": 0,
  "age": 18,
  "address": "Shenzhen Longgang Park"
}
PUT /es_db/_doc/4
{
  "name": "LISS",
  "sex": 0,
  "age": 51,
  "address": "Baiyun Mountain, Guangzhou"
}
PUT /es_db/_doc/4
{
  "name": "LISS",
  "sex": 0,
  "age": 52,
  "address": "Baiyun Mountain, Guangzhou"
}
PUT /es_db/_doc/5
{
  "name": "Guangzhou people",
  "sex": 0,
  "age": 100,
  "address": "Guangzhou"
}

4. Basic operations of querying documents

GET /es_db/_doc/_search

GET /es_db/_doc/_search?q=age:52

GET /es_db/_doc/_search?q=age:<=20

GET /es_db/_doc/_search?q=age:>20

GET /es_db/_doc/_search?q=age[40 TO 50]

GET /es_db/_doc/_search?q=age[0 TO 150]&from=2&size=2

GET /es_db/_doc/_search?_source=name,age

GET /es_db/_doc/_search?_source=name,age&sort=age:desc

5. Batch query based on multiple document IDS

GET /es_db/_doc/_mget
{
  "ids":["1", "2"]
}

DSL language advanced query

1. Perform an accurate query based on a field. term query will not perform word segmentation query on the field, but will use accurate matching

Similar to SQL: select * from student where name = 'Zhang San'

POST /es_db/_doc/_search
{
  "query": {
    "term": {
      "name": "Zhang San"
    }
  }
}

2. ik word segmentation test

POST _analyze
{
  "analyzer": "ik_max_word",
  "text": "Guangzhou Park"
}

3. Fuzzy query match according to the remark information. Match will perform word segmentation query according to the word splitter of the field

Similar to SQL: select * from user where (address like '% Guangzhou%' or address like '% Park%') limit 0,5

POST /es_db/_doc/_search
{
  "from": 0,
  "size": 5,
  "query": {
    "match": {
      "address": "Guangzhou Park"
    }
  }
}

4. Multi field fuzzy matching query and accurate query_ match

Similar to SQL: select * from student where name like '% Guangzhou%' or address like '% Guangzhou%'

POST /es_db/_doc/_search
{
  "query": {
    "multi_match": {
      "query": "Guangzhou",
      "fields": ["name", "address"]
    }
  }
}

5. Range query

  • Range: range keyword
  • gte: greater than or equal to
  • lte: less than or equal to
  • gt: greater than
  • lt: less than

Similar to SQL: select * from user where age between 20 and 30

POST /es_db/_doc/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 20,
        "lte": 30
      }
    }
  }
}

6. Paging, output field, sorting and comprehensive query

Similar to SQL: select name, age, address from user where age between 10 and 50 order by age desc limit 0,5

POST /es_db/_doc/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 10,
        "lte": 50 
      }
    }
  },
  "from": 0,
  "size": 5,
  "_source": ["name", "age", "address"],
  "sort": {
    "age": "desc"
    
  }
}

7. The filter query method does not calculate the correlation score and sort the results. Therefore, the efficiency will be higher and the query results can be cached

POST /es_db/_doc/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "age": 23
        }
      }
    }
  }
}

8. Get document mapping

GET /es_db/_mapping

9. Batch acquisition according to document ID

GET /es_db/_mget
{
  "docs": [
    {
      "_type": "_doc",
      "_id": 1
    },
    {
      "_type": "_doc",
      "_id": 4
    }
    ]
}

GET /es_db/_doc/_mget
{
    "docs": [
    {
      "_id": 1
    },
    {
      "_id": 4
    }
    ]
}

10. Batch query the data of different document ID S under different indexes

GET _mget
{
  "docs": [
    {
      "_index": "es_db",
      "_type": "_doc",
      "_id": 4
    },
    {
      "_index": "es_db_second",
      "_type": "_doc",
      "_id": 1
    }
    ]
}

11. Batch operation documents

Batch writing of documents is performed through_ bulk API

Request method: POST

Request address:_ bulk

Request parameters: Pass_ bulk operation documents generally have at least two lines of parameters (or even lines of parameters)

The first line of parameters specifies the type of operation and the object of the operation

(index,type and id)

The second row of parameters is the data of the operation

The parameter settings are as follows:

{"actionName":{"_index":"indexName", "_type":"typeName","_id":"id"}} 2 {"field1":"value1", "field2":"value2"}

actionName: indicates the operation type, mainly including create,index,delete and update

Batch create document create

POST _bulk
{"create": {"_index": "es_db", "_type": "_doc", "_id": 6}}
{"name": "Cao Cao", "sex": 0, "age": 30, "address": "Xu Chang"}
{"create": {"_index": "es_db", "_type": "_doc", "_id": 7}}
{"name": "Cao Pi", "sex": 0, "age": 20, "address": "Xu Chang"}

If the original file does not exist, it is created; If the original document exists, it will be replaced (full modification of the original document)

POST _bulk
{"index": {"_index": "es_db", "_type": "_doc", "_id": 8}}
{"name": "Cao Zhi", "sex": 0, "age": 30, "address": "Xu Chang"}
{"index": {"_index": "es_db", "_type": "_doc", "_id": 7}}
{"name": "Cao Pi", "sex": 0, "age": 20}

Batch delete

POST _bulk
{"delete": {"_index": "es_db", "_type": "_doc", "_id": 8}}
{"delete": {"_index": "es_db_second", "_type": "_doc", "_id": 1}}

12. Data matching query without specifying fields_ String, including AND and OR conditions (word segmentation will be performed to vaguely match the data of all fields)

POST /es_db/_search
{
  "query": {
    "query_string": {
      "query": "Guangzhou OR Cao Pi"
    }
  }
}

summary

1. match

For fuzzy matching, you need to specify the field name and segment the query criteria; In other words, match is a partial matching fuzzy query. The query conditions are relatively loose.

2. term

term is an accurate query and does not perform word segmentation on query criteria;

This query method is equivalent to match when querying the information of a single word without word segmentation, and the query result is the same;

3. match_phase

Word segmentation will be performed on the query criteria, but the query result needs to include all word segmentation of the query criteria in the same order;

For example, if "hello world" is queried, the query result must contain "hello" and "world", and the Hello must precede the world and be continuous, "world hello" is not satisfied, and "hello that world" is not satisfied;

4. query_string

Similar to match, but match needs to specify the field name, query_string is a search in all fields, with a wider range.

Topics: Big Data ElasticSearch search engine ELK