“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

I. Document batch operation

By multiple documents, I mean batch operating on multiple documents. Searching and querying documents will be discussed in a later section

1. Obtain document data in batches

(1) Index and type are not specified in the URL: GET Request address: _mGET

  • Docs: Document array parameter
  • _index: indicates the index
  • _type: specifies the type
  • _id: indicates the specified ID
  • _source: Specifies the field to be queried
GET _mget 
{ 
"docs": [ 
{ 
"_index": "es_db", 
"_type": "_doc", 
"_id": 1 
}, 
{ 
"_index": "es_db", 
"_type": "_doc", 
"_id": 2}}]Copy the code

The response results are as follows:

{
  "docs" : [
    {
      "_index" : "es_db",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 0,
      "_primary_term" : 1,
      "found" : true, "_source" : {"name" : "zhang SAN ", "sex" :1,
        "age" : 25"Address", "guangzhou tianhe park", "remark" : "Java developer"}}, {" _index ":" es_db ", "_type" : "_doc", "_id" : "2", "_version" :1,
      "_seq_no" : 1,
      "_primary_term" : 1,
      "found" : true, "_source" : {"name" : "sex" :1,
        "age" : 28, "address" : "Guangzhou Liwan Building ", "remark" :" Java assistant"}}]}Copy the code

/{{indexName}}/_mget: /{{indexName}}/_mget: /{{indexName}} Parameter Description Value index _type: specifies the type. _ID: specifies the ID. _source: specifies the field to be queried

GET /user/_mget 
{ 
"docs": [ 
{ 
"_type":"_doc", 
"_id": 3 
}, 
{ 
"_type":"_doc", 
"_id": 4}}]Copy the code

Returns the result (index_NOT_found_exception because I didn’t create the /user index) :

#! Deprecation: [types removal] Specifying types in multi get requests is deprecated.
{
  "docs" : [
    {
      "_index" : "user",
      "_type" : "_doc",
      "_id" : "3",
      "error" : {
        "root_cause" : [
          {
            "type" : "index_not_found_exception",
            "reason" : "no such index [user]",
            "resource.type" : "index_expression",
            "resource.id" : "user",
            "index_uuid" : "_na_",
            "index" : "user"
          }
        ],
        "type" : "index_not_found_exception",
        "reason" : "no such index [user]",
        "resource.type" : "index_expression",
        "resource.id" : "user",
        "index_uuid" : "_na_",
        "index" : "user"
      }
    },
    {
      "_index" : "user",
      "_type" : "_doc",
      "_id" : "4",
      "error" : {
        "root_cause" : [
          {
            "type" : "index_not_found_exception",
            "reason" : "no such index [user]",
            "resource.type" : "index_expression",
            "resource.id" : "user",
            "index_uuid" : "_na_",
            "index" : "user"
          }
        ],
        "type" : "index_not_found_exception",
        "reason" : "no such index [user]",
        "resource.type" : "index_expression",
        "resource.id" : "user",
        "index_uuid" : "_na_",
        "index" : "user"
      }
    }
  ]
}

Copy the code

GET address: /{{indexName}}/{{typeName}}/_mget /{{typeName}}/_mget Document array parameter _index: specifies the type. _ID: specifies the ID. _source: specifies the field to be queried

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

Returns the result

#! Deprecation: [types removal] Specifying types in multi get requests is deprecated.
{
  "docs" : [
    {
      "_index" : "es_db",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 0,
      "_primary_term" : 1,
      "found" : true, "_source" : {"name" : "zhang SAN ", "sex" :1,
        "age" : 25"Address", "guangzhou tianhe park", "remark" : "Java developer"}}, {" _index ":" es_db ", "_type" : "_doc", "_id" : "2", "_version" :1,
      "_seq_no" : 1,
      "_primary_term" : 1,
      "found" : true, "_source" : {"name" : "sex" :1,
        "age" : 28, "address" : "Guangzhou Liwan Building ", "remark" :" Java assistant"}}]}Copy the code

2. Operate document data in batches

Bulk data is written to documents in batches using the _BULK API. The request mode is as follows: POST Request Address: _BULK request parameters: The _BULK operation document contains at least two rows of parameters (or even rows of parameters). The first row of parameters specifies the operation type and the operation object (index,type, and ID). The second row is the operation data.

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

ActionName: Indicates the operation type, including create,index,delete, and update. (1) Create documents in batches

POST _bulk 
{"create":{"_index":"article", "_type":"_doc", "_id":3}} 
{"id":3, "title" : "li bai", "content" : "li bai 666", "tags" : [" Java ", "to like"], "create_time" :1554015482530} 
{"create":{"_index":"article", "_type":"_doc", "_id":4}} 
{"id":4, "title" : "li bai", "content" : "li bai NB", "tags" : [" Java ", "to like"], "create_time" :1554015482530} 
Copy the code

Returns the result

{
  "error" : {
    "root_cause" : [
      {
        "type" : "json_parse_exception",
        "reason" : "Invalid UTF-8 start byte 0xb1\n at [Source: (byte[])\"POST /_bulk? pretty=true HTTP/1.1\r\nx-forwarded-for: 127.0. 01.\r\nx-forwarded-port: 61337\r\nx-forwarded-proto: http\r\nx-forwarded-host: localhost:5601\r\ncontent-type: application/json\r\nhost: localhost\r\ntransfer-encoding: chunked\r\nConnection: close\r\n\r\n14d\r\n{\"create\":{\"_index\":\"article\", \"_type\":\"_doc\", \"_id\":3}}\n{\"id\":3Li bai, \ "title \" : \ '\ "and \" content \ ": \" li bai 666 \ ", \ "tags \" : [\ Java \ ", \ "oriented to like \ n \], \" create_time \ ":1554015482530}\n{\"create\":{\"_index\":\"article\", \"_type\":\"_doc\", \"_id\":4}}\n{\"id\":4Li bai, \ "title \" : \ '\ ", \ "c \ [truncated89 bytes]; line: 1.column: 3]"
      }
    ],
    "type" : "json_parse_exception",
    "reason" : "Invalid UTF- 8 - start byte 0xb1\n at [Source: (byte[])\"POST /_bulk?pretty=true HTTP/1.1\r\nx-forwarded-for: 127.0.0.1\r\nx-forwarded-port: 61337\r\nx-forwarded-proto: http\r\nx-forwarded-host: localhost:5601\r\ncontent-type: application/json\r\nhost: localhost\r\ntransfer-encoding: chunked\r\nConnection: close\r\n\r\n14d\r\n{\"create\ ": {\" _index \ ": \" article \ ", \ "_type \" : \ "_doc \", \ "_id \" : 3}} \ n {\ \ "id" : 3, \ "title \" : \ "li bai \", \ "content \" : \ "li baiAwesome!\ "and \" tags \ ": \" Java \ ", \ "oriented to like \ n \], \" create_time \ ": {1554015482530} \ n \"create\ ": {\" _index \ ": \" article \ ", \ "_type \" : \ "_doc \", \ "_id \" : 4}} \ n {\ \ "id" : 4, \ "title \" : \ "li bai \", \ \ "c" [truncated 89 bytes); line: 1, column: 3]" }, "status" :400
}

Copy the code

(2) Create index or replace index in full

POST _bulk 
{"index":{"_index":"article", "_type":"_doc", "_id":3}} 
{"id":3, "title" : "du fu (a)", "content" : "du fu 666", "tags" : [" Java ", "object oriented"], "create_time" :1554015482530} 
{"index":{"_index":"article", "_type":"_doc", "_id":4}}
{"id":4, "title" : "du fu (2)", "content" : "du fu NB", "tags" : [" Java ", "object oriented"], "create_time" :1554015482530} 

Copy the code

** If the original document does not exist, create ** ** if the original document exists, replace (fully modify the original document) **

Returns the result

#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{
  "took" : 651,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "article",
        "_type" : "_doc",
        "_id" : "3",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "article",
        "_type" : "_doc",
        "_id" : "4",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 1,
        "_primary_term" : 1,
        "status" : 201}}}]Copy the code

(3) Batch delete delete

POST _bulk 
{"delete":{"_index":"article", "_type":"_doc", "_id":3}} 
{"delete":{"_index":"article", "_type":"_doc", "_id":4}} 
Copy the code

Returns the result

#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{
  "took" : 32,
  "errors" : false,
  "items" : [
    {
      "delete" : {
        "_index" : "article",
        "_type" : "_doc",
        "_id" : "3",
        "_version" : 2,
        "result" : "deleted",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 2,
        "_primary_term" : 1,
        "status" : 200
      }
    },
    {
      "delete" : {
        "_index" : "article",
        "_type" : "_doc",
        "_id" : "4",
        "_version" : 2,
        "result" : "deleted",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 3,
        "_primary_term" : 1,
        "status" : 200}}}]Copy the code

(4) Batch update

POST _bulk 
{"update":{"_index":"article", "_type":"_doc", "_id":3}} {" doc ": {" title" : "ES solution required internal strength"}} {" update ": {" _index" : "article", "_type" : "_doc", "_id" :4}} 
{"doc":{"create_time":1554018421008}} 
Copy the code

Returns the result

#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{
  "took" : 3,
  "errors" : true,
  "items" : [
    {
      "update" : {
        "_index" : "article",
        "_type" : "_doc",
        "_id" : "3",
        "status" : 404,
        "error" : {
          "type" : "document_missing_exception",
          "reason" : "[_doc][3]: document missing",
          "index_uuid" : "uTqmplLRR3CpwQXWtAvJ7Q",
          "shard" : "0",
          "index" : "article"
        }
      }
    },
    {
      "update" : {
        "_index" : "article",
        "_type" : "_doc",
        "_id" : "4",
        "status" : 404,
        "error" : {
          "type" : "document_missing_exception",
          "reason" : "[_doc][4]: document missing",
          "index_uuid" : "uTqmplLRR3CpwQXWtAvJ7Q",
          "shard" : "0",
          "index" : "article"
        }
      }
    }
  ]
}

Copy the code

DSL language advanced query

1. Query DSL overview

**Domain Specific Language **

Domain-specific language

Elasticsearch provides a ful1 Query DSL based on JSON to define queries

Elasticsearch provides a JSON-based DSL to define queries.

A DSL consists of a leaf query clause and a compound query clause.

2. There is no query condition

The none condition is all. The default is all, or match_all is used for all

GET /es_db/_doc/_search 
{ 
"query":{ 
"match_all":{} 
} 
} 
Copy the code

Returns the result

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "es_db",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0, "_source" : {"name" : "zhang SAN ", "sex" :1,
          "age" : 25"Address", "guangzhou tianhe park", "remark" : "Java developer"}}, {" _index ":" es_db ", "_type" : "_doc", "_id" : "2", "_score" :1.0, "_source" : {"name" : "sex" :1,
          "age" : 28, "address", "guangzhou li wan building" and "remark" : "Java assistant"}}, {" _index ":" es_db ", "_type" : "_doc", "_id" : "3", "_score" :1.0,
        "_source" : {
          "name" : "rod",
          "sex" : 0,
          "age" : 26"Address", "guangzhou baiyun mountain park", "remark" : "PHP developer"}}, {" _index ":" es_db ", "_type" : "_doc", "_id" : "4", "_score" :1.0,
        "_source" : {
          "name" : "admin",
          "sex" : 0,
          "age" : 22J head ", "address", "changsha," remark ":" python assistant "}}, {" _index ":" es_db ", "_type" : "_doc", "_id" : "5", "_score" :1.0, "_source" : {"name" : "xiaoming ", "sex" :0,
          "age" : 19, "address" : "address", "remark" : "Java architect assistant"}}]}Copy the code

3. Search conditions exist

3.1 Leaf Query (Single Field Query)

3.1.1 Fuzzy matching

Fuzzy matching is mainly for fields of the text type, which will segment the content. In the query, it will also segment the search conditions, and then find matched data through the inverted index. Fuzzy matching is mainly realized by parameters such as match

  • Match: Use match keywords to blur matching conditions
  • Prefix: prefix matching regexp: data is matched by regular expression
  • Complex use of match
  • The match condition also supports the following parameters:
  • Query: Specifies the matched value
  • Operator: Indicates the matching condition type
  • And: match all conditional participles
  • Or: Conditional segmentation after a match (default)
  • Minmum_should_match: Specifies the minimum number of matches

3.1.2 Exact Matching

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

3.2 Combination Query condition (Multi-Condition Query)

Combined condition query is a complete query condition formed by combining leaf condition query statements

  • Bool: The relation between the conditions is AND, OR or not
  • Must: All conditions must be satisfied, i.e. all conditions must be and
  • Relation should: one of each condition can be satisfied, namely, each condition
  • Or
  • Must_not: All conditions are not met, that is, conditions are not
  • Relationship between
  • Filter: Does not count correlation scores. It does not count _score
  • Relevancy scores are more efficient
  • Constant_score: Relevance score is not calculated
  • Subconditions such as must/filter/shoud/must_not are pass
  • A term/terms/range/ids/exists/match leaves conditions as parameters

Note: The above parameters, when there is only one search condition, must and so on correspond to an object, when there are multiple conditions, correspond to an array

3.3 Join query (multi-document merge query)

  • Parent /child document query: parent/child
  • Nested document queries: nested

3.4 There are two TYPES of DSL query languages: Query DSL and Filter DSL

The difference between the two is shown below:





query DSL

In the query context, the query answers the question “Is this document relevant because it doesn’t match this query?” How do you verify a match that’s easy to understand, how do you compute the correlation? Index data in ES will store a _score value, the higher the score value represents the more matched. In addition, the calculation of the score for a particular search is very complex, so it takes a certain amount of time.

filter DSL

In the context of a filter, the query answers the question “Does this document match?” The answer is simple, yes or no. It doesn’t compute any points and doesn’t care about the sorting questions returned, so it’s a little more efficient. The filter context is the context in which the filter parameter is used, such as must_NOT or filter in bool queries. In addition, if the filter is used frequently, ES will automatically cache the contents of the filter, which improves the performance of the query. Some filtering cases:

3.5 Query: A case

Query the name term precisely by name. The term query does not perform a word segmentation query on the field

Adopt exact matching

Note: Term is used for precise query, and the query field mapping type belongs to keyword. For example:

POST /es_db/_doc/_search {" query ": {" term" : {" name ":" admin "}}} # equivalenceSQL: select * from student where name = 'admin' 
Copy the code

Fuzzy query match based on the remarks. Match performs word segmentation query based on the word segmentation of the field

For example:

POST /es_db/_doc/_search 
{"from": 0,"size": 2,"query": {"match": {"address": "guangzhou "}}} #SQL: SQL: select * from user where address like '% % in guangzhou' limit 0.2 
Copy the code

Multi-field fuzzy matching query and precise query multi_match

POST /es_db/_doc/_search {" query ": {" multi_match" : {" query ":" zhang ", "fields" : / "address", "name"}}} # equivalenceSQL: select * from student where name like 'Joe Smith % %' or address like Zhang '% thirty percent' 
Copy the code

Query query_string without specified field conditions, including AND AND OR conditions

POST /es_db/_doc/_search {" query ": {" query_string" : {" query ":" (guangzhou) OR changsha "}}}Copy the code

Query_string specifies field conditions, including AND AND OR conditions

POST /es_db/_doc/_search {" query ": {" query_string" : {" query ":" admin OR changsha ", "fields" : (" name ", "address")}}}Copy the code

Range Query Note: The meanings of some fields in the JSON request string

  • Range: indicates the keyword of the range
  • Gte is greater than or equal to
  • Lte less than or equal to
  • Gt is greater than the
  • Lt is less than
  • Now Current time
POST /es_db/_doc/_search 
{ 
"query" : { 
"range" : { 
"age" : { 
"gte":25, 
"lte":28}}}} # equivalentSQL: select * from user where age between 25 and 28 
Copy the code

Paging, output field, sort comprehensive query

POST /es_db/_doc/_search 
{ 
"query" : { 
"range" : { 
"age" : { 
"gte":25, 
"lte":28 
} 
} 
}, 
"from": 0, 
"size": 2, 
"_source": ["name", "age", "book"], 
"sort": {"age":"desc"} 
}
Copy the code

3.6 Filter The query method does not calculate the correlation score or sort the results. Therefore, the efficiency is higher and the query results can be cached.

Filter Context Filters data

POST /es_db/_doc/_search 
{ 
"query" : { 
"bool" : { 
"filter" : { 
"term":{ 
"age":25 
} 
} 
} 
} 
} 
Copy the code

Conclusion:

1. Match the match: Fuzzy match, you need to specify the field name, but the input will be split, for example, “Hello world” will be split into Hello and world, and then match, if the field contains Either Hello or world, or if the field contains both, the result will be queried, so match is a partially matched fuzzy query. The query conditions are relatively loose. 2. term term: This query is equivalent to match in some cases, like if we were to query for the single word hello, we would get the same result as match, but if we were to query for “hello world”, we would get a very different result, because the input is not segmented, which means that when we query, “Hello world” “Hello world” “Hello World” When saving “hello World “, elasticSearch will split the field into “Hello world” and” World “, there is no “Hello world”, so the query result will be empty. This is also the difference between term queries and match. 3. Match_phase Match_phase: the input will be segmented, but the result should also contain all the participles in the same order. In the case of “Hello world”, the result must contain” Hello “and” world”, and they must be connected in a fixed order. “Hello that word” is not satisfied, and “world Hello” is not satisfied. Query_string Query_string: Similar to match, but match requires a field name. Query_string searches for all fields in a wider range.

Document mapping

The mapping in ElasticSearch is similar to the schema information in MySQL.

1. Mapping in ES can be divided into dynamic mapping and static mapping

Dynamic mapping:

In a relational database, you need to create a database, then create a table under that database, and create table fields, types, lengths, primary keys, and so on, before you can insert data based on the table. For Elasticsearch, you do not need to define a Mapping (i.e. a table or field in a relational database). ** When a document is written to Elasticsearch, it automatically identifies the type of the field in the document. This mechanism is called dynamic Mapping. **

Dynamic mapping rules are as follows:



Static mapping:

In Elasticsearch, you can also define a map in advance, including the field type of the document, word segmentation, etc. This method is called static mapping.

2 Dynamic Mapping

2.1 Deleting an Index

DELETE /Es_db # returns {"acknowledged" :true
}

Copy the code

2.2 Creating An Index

PUT /Es_db # returns {"acknowledged" :true,
  "shards_acknowledged" : true,
  "index" : "es_db"
}

Copy the code

2.3 Creating a Document (ES automatically creates a mapping based on the data type)

PUT /es_db/_doc/1 
{ 
"name": "Jack", 
"sex": 1, 
"age": 25, "book", "introduction to Java to master", "address" : "the guangzhou baiyun airport" # returns {} "_index" : "es_db", "_type" : "_doc", "_id" : "1", "_version" :1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

Copy the code

2.4 Obtaining document Mappings

GET /es_db/_mappings # return {"es_db" : {"es_db" : {"properties" : {"address" : {"type" : "text", "fields" : {"keyword" : { "type" : "keyword", "ignore_above" :256
            }
          }
        },
        "age" : {
          "type" : "long"
        },
        "book" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "sex" : {
          "type" : "long"
        }
      }
    }
  }
}

Copy the code

3 Static Mapping

3.1 Deleting an Index

DELETE /es_db 
Copy the code

3.2 Creating An Index

PUT /es_db
Copy the code

3.3 Setting document Mapping

PUT /es_db 
{ 
"mappings":{ 
"properties":{ 
"name":{"type":"keyword","index":true,"store":true}, 
"sex":{"type":"integer","index":true,"store":true}, 
"age":{"type":"integer","index":true,"store":true}, 
"book":{"type":"text","index":true,"store":true}, 
"address":{"type":"text","index":true,"store":true}}}} # return {"error" : {"root_cause" : [{"type" : "resource_already_exists_exception", "reason" : "index [es_db/HvQwVSaGRdy0gHwI_e49DQ] already exists", "index_uuid" : "HvQwVSaGRdy0gHwI_e49DQ", "index" : "es_db" } ], "type" : "resource_already_exists_exception", "reason" : "index [es_db/HvQwVSaGRdy0gHwI_e49DQ] already exists", "index_uuid" : "HvQwVSaGRdy0gHwI_e49DQ", "index" : "es_db" }, "status" :400
}


Copy the code

3.4 Creating Documents Based on static Mappings

PUT /es_db/_doc/1 
{ 
"name": "Jack", 
"sex": 1, 
"age": 25, "book", "introduction to elasticSearch to master", "address" : "guangzhou car vicious" # returns {} "_index" : "es_db", "_type" : "_doc", "_id" : "1", "_version" :1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

Copy the code

3.5 Obtaining document Mappings

GET /es_db/_mappings # return {"es_db" : {"es_db" : {"properties" : {"address" : {"type" : "text", "fields" : {"keyword" : { "type" : "keyword", "ignore_above" :256
            }
          }
        },
        "age" : {
          "type" : "long"
        },
        "book" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "sex" : {
          "type" : "long"
        }
      }
    }
  }
}

Copy the code

4. Core datatype

  • String: string. The string type contains text and keyword.
  • Text: This type is used to index long text. Before creating an index, the text will be segmented into word combinations to create an index. Es is allowed to retrieve these words. Text cannot be used for sorting and aggregation.
  • Keyword: This type is non-word segmentation and can be used for search filtering, sorting, and aggregation. The keyword type cannot be used for word segmentation fuzzy retrieval with text.
  • Numeric types: long, INTEGER, short, byte, double, float
  • Date type: Date
  • Boolean: Boolean

The difference between keyword and text mapping types

Set the book field to keyword mapping (only accurate query, not word segmentation, can be aggregated and sorted)

POST /es_db/_doc/_search {"query": {"term": {"book": "elasticSearch "}}} # return data #! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

Copy the code

Set book field to text map can be fuzzy query, can be word segmentation query, can not aggregate, sort)

POST /es_db/_doc/_search {"query": {"match": {"book": "elasticSearch "}} # return data #! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.7260926,
    "hits" : [
      {
        "_index" : "es_db",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.7260926,
        "_source" : {
          "name" : "Jack",
          "sex" : 1,
          "age" : 25, "address" : "elasticSearch "}}]}Copy the code

Specify an IK toggle of type TEXT when creating a static map

1. Set the document mapping for the IK toggle

Delete the previous ES_DB and create a new ES_DB mapping to define ik_smart

PUT /es_db 
{ 
"mappings":{ 
"properties":{ 
"name":{"type":"keyword","index":true,"store":true}, 
"sex":{"type":"integer","index":true,"store":true}, 
"age":{"type":"integer","index":true,"store":true}, 
"book":{"type":"text","index":true,"store":true,"analyzer":"ik_smart","search_analyzer":"ik_smart"}, 
"address":{"type":"text","index":true,"store":true} 
} 
} 
}

# 返回
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "es_db"
}

Copy the code

2. Word segmentation query

POST /es_db/_doc/_search {" query ": {" match" : {" address ":" wide "}}} return to # #! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 81,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}


POST /es_db/_doc/_search {" query ": {" match" : {" address ":" guangzhou "}}} return to # #! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 81,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
Copy the code

7. Modify the existing mapping mapping

1) To push down an existing mapping, you need to create a new static index. 2) then import the data from the original index into the new index. 3) Delete the original index

PUT /db_index

POST _reindex 
{ 
"source": { 
"index": "db_index" 
}, 
"dest": { 
"index": "db_index_2" 
} 
} 

DELETE /db_index 

PUT /db_index_2/_alias/db_index
Copy the code

Note that these steps achieve a smooth transition of indexes with zero downtime

Optimistic concurrency control for Elasticsearch

In the database world, there are two ways to ensure concurrent updates without losing data:

Pessimistic concurrency control

This approach, widely used by relational databases, assumes that change conflicts are likely to occur and therefore blocks access to resources to prevent conflicts. A typical example is to lock a row of data before reading it, ensuring that only the thread that placed the lock can modify the row.

Optimistic concurrency control

The approach used in Elasticsearch assumes that conflicts are impossible and will not block the operation being attempted. However, if the source data is modified during reads and writes, the update will fail. The application then decides how to resolve the conflict. For example, you can retry updates, use new data, or report back to the user.

3. Take creating a document as an example

PUT /db_index/_doc/1 
{ 
"name": "Jack", 
"sex": 1, 
"age": 25, "book": "Spring Boot to master ", "remark": "hello world"}Copy the code

4. Implement _version optimistic lock to update documents

Note: ES 7.x does not support this keyword

PUT /db_index/_doc/1? version=1 
{ 
"name": "Jack", 
"sex": 1, 
"age": 25} # return {"error" : {"root_cause" : [{"type" : "action_request_validation_exception", "reason" : "Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;" } ], "type" : "action_request_validation_exception", "reason" : "Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;" }, "status" :400
}

Copy the code

ES new version does not use version for concurrent version control if_seq_NO = version value

  • &if_primary_term= Document location
  • _seq_no: indicates the document version. The function is the same as _version
  • _primary_term: document location
POST /my_doc/_search 

DELETE /my_doc 

POST /my_doc/_doc/1 
{ 
 "id": 1, "name", "guan yu", "desc" : "Trinidad performers", "create_date" : "24" 2021 ‐ ‐ 02} the POST/my_doc/_update/1{"doc": {"name": "guyu 1"}} # if_seq_no = version number, a version number can only be used once POST/my_doc/_update/1/? if_seq_no=1&if_primary_term=1{"doc": {"name": "guan Yu 1"}} POST/my_doc/_update/1/? if_seq_no=2&if_primary_term=1{"doc": {"name": "guanyu"}}Copy the code

Java API operation ES

Subsequent independent article explanation

X. Setting up ES cluster environment

1. Distribute the installation package to other servers

2. Modify the elasticsearch. Yml

Node1.hxl. cn The server uses the HXL user to modify the configuration file

The mkdir ‐ p/usr/local/es/Elasticsearch ‐7.101./The log mkdir ‐ p/usr/local/es/Elasticsearch ‐7.101./data 

cd /usr/local/es/Elasticsearch ‐7.101./Config rm ‐rf elasticSearch.yml vim elasticsearch.yml cluster.name: HXL ‐es node.name: node1.hxl.cn path.data:/usr/local/es/Elasticsearch ‐7.101./data 
path.logs: /usr/local/es/Elasticsearch ‐7.101./log 
network.host: node1.hxl.cn 
http.port: 9200discovery.seed_hosts: ["IP1", "IP2", "IP3"] cluster.initial_master_nodes: [" Node 1 name ", "node 2 name "," node 3 name "] bootstrap.system_call_filter:false 
bootstrap.memory_lock: false 
http.cors.enabled: trueHTTP. Cors. Allow ‐ origin: "*"Copy the code

3. Modify the JVM. Option

Node1.hxl. cn Run the following command as user HXL to resize the JVM heap based on the memory size of the server.

cd /usr/local/es/Elasticsearch ‐7.101./Options ‐Xms2g ‐Xmx2gCopy the code

4. Modify the ES configuration file on node2 and node3

Node2.hxl. cn and node3.hxl.cn also need to modify the ES configuration file node2.hxl.cn Run the following command as user HXL to modify the ES configuration file

The mkdir ‐ p/usr/local/es/Elasticsearch ‐7.101./The log mkdir ‐ p/usr/local/es/Elasticsearch ‐7.101./data 

cd /usr/local/es/Elasticsearch ‐7.101./Config vim elasticsearch.yml cluster.name: HXL ‐es node.name: node2.hxl.cn path.data:/usr/local/es/Elasticsearch ‐7.101./data 
path.logs: /usr/local/es/Elasticsearch ‐7.101./log 
network.host: node2.hxl.cn 
http.port: 9200discovery.seed_hosts: ["IP1", "IP2", "IP3"] cluster.initial_master_nodes: [" Node 1 name ", "node 2 name "," node 3 name "] bootstrap.system_call_filter:false 
bootstrap.memory_lock: false 
http.cors.enabled: trueHTTP. Cors. Allow ‐ origin: "*"Copy the code

Node3.hxl. cn Run the following command as user HXL to modify the configuration file

The mkdir ‐ p/usr/local/es/Elasticsearch ‐7.101./The log mkdir ‐ p/usr/local/es/Elasticsearch ‐7.101./data 

cd /usr/local/es/Elasticsearch ‐7.101./Config vim elasticsearch.yml cluster.name: HXL ‐es node.name: node3.hxl.cn10 path.data:/usr/local/es/Elasticsearch ‐7.101./data 
path.logs: /usr/local/es/Elasticsearch ‐7.101./log 
network.host: node3.hxl.cn 
http.port: 9200discovery.seed_hosts: ["IP1", "IP2", "IP3"] cluster.initial_master_nodes: [" Node 1 name ", "node 2 name "," node 3 name "] bootstrap.system_call_filter:false 
bootstrap.memory_lock: false 
http.cors.enabled: trueHTTP. Cors. Allow ‐ origin: "*"Copy the code

View cluster status:

GET _cat/nodes? vGET _cat/health? vCopy the code

Elasticsearch- Head plugin

You can install the elasticSearch-head plugin to access the index library. Elasticsearch -head 插件 图 片 es 插件 图 片 es 插件 图 片 You can install elasticsearch-head by downloading the source code package and compiling it. If the network is poor, it is impossible to install elasticsearch-head. To install elasticSearch-head, you need to install Node.js first

1 installation nodejs

Node.js is a JavaScript runtime environment based on Chrome V8 engine. Node.js is a Javascript runtime environment developed by Ryan Dahl and released in May 2009. It is essentially a wrapper around Chrome V8. Node.js is not a JavaScript framework, unlike CakePHP, Django, or Rails. Node.js is not a browser-side library, as jQuery and ExtJS are. Node.js is a development platform that lets JavaScript run on the server side, putting JavaScript on a par with server-side languages like PHP, Python, Perl, and Ruby.

1.1 Downloading the Installation Package

Node1.hxl. cn Run the following command to download the installation package and decompress it

CD/usr/local/es wget https://npm.taobao.org/mirrors/node/v8.1.0/node ‐ v8.1.0 ‐ Linux ‐ x64. Tar. Gz tar ‐ ZXVF The node ‐ v8.1.0 ‐ Linux ‐ x64. Tar. Gz ‐ / usr/local/es/CCopy the code

1.2 Creating a Soft Connection

Node1.hxl. cn Run the following command to create a soft connection

Sudo ln ‐ s/usr/local/es/The node ‐ v81.. 0Linux ‐ ‐ x64/lib/node_modules/npm/bin/Np m ‐ cli. Js/usr/local/bin/NPM sudo ln ‐ s/usr/local/es/The node ‐ v81.. 0Linux ‐ ‐ x64/bin/node /usr/local/bin/nod 
e 
Copy the code

1.3 Modifying Environment Variables

Node1.hxl. cn Adds environment variables to the server

vi /etc/profile 
export NODE_HOME=/usr/local/es/The node ‐ v81.. 0Linux ‐ ‐ x64 export PATH=:$PATH:$NODE_HOME/bin 
Copy the code

After modifying environment variables, run the source command to take effect source /etc/profile

1.4 Verifying the installation

Node1.hxl. cn Run the following command to verify the installation

Node v NPM ‐ ‐ vCopy the code

2 Local Installation

2.1 Uploading the compressed package to /usr/local/es

/usr/local/es /usr/local/es /usr/local/es /usr/local/es

2.2 Decompressing the installation package

On the server, run the following command to decompress the installation package

CD /usr/local/es/ tar ‐ ZXVF elasticSearch ‐head‐compile‐after.tar.gz ‐C /usr/local/es/Copy the code

2.3 node1.hxl.cn Machine modify gruntfile. js Modify gruntfile. js file

cd /usr/local/es/Elasticsearch ‐ head vim Gruntfile. JsCopy the code

Find line 93 in the code: hostname: ‘192.168.100.100’ and change it to: node1.hxl.cn

connect: { 
server: { 
options: { 
hostname: 'node1.hxl.cn', 
port: 9100, 
base: '. ', 
keepalive: true}}}Copy the code

2.4 node1 Machine changes app.js

Modify app.js on the first machine

cd /usr/local/es/Elasticsearch ‐ head/_site 
vim app.js 


#在Vim中输入「:4354", locate to the first4354Line # modify HTTP://localhost:9200For HTTP://node1.hxl.cn:9200 
Copy the code

2.5 Starting the HEAD service

Node1.hxl.cn starts elasticSearch-head plugin

cd /usr/local/es/Elasticsearch ‐ head/node_modules/grunt/bin/Process foreground start command./grunt server 
Copy the code

Background process start command

nohup ./grunt server >/dev/null 2>&1 & 
Running "connect:server" (connect) taskWaiting forever... Startedconnect web server on http://192.16852.100.:9100‐ the elasticsearch‐head plugin process is found by executing kill ‐9Kill the process netSTAT ‐ NLTP| grep 9100Kill ‐9 8328 
Copy the code

2.6 Accessing ElasticSearch-Head

Elasticsearch-head: select select * from elasticSearch-head; select select * from elasticSearch-head; select select * from elasticSearch-head; Go to/elasticSearch-7.6.1 /data/ and delete the nodes file. Restart the ES process on nodes 2 and 3