Elatsicsearch command set

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

The index

Create indexes

PUT /hello

View index

GET /_cat/indices

Carry parameters with header? v

GET /_cat/indices? v

Remove the index

DELETE /hello

Types and mappings

Create types and mappings

Type operation /index

PUT /hello
{
  "mappings": {
    "emp":{
      "properties":{
        "id":{
          "type":"keyword" 
        },
        "name":{
          "type":"text"
        },
        "age":{
          "type":"integer"
        },
        "bir":{
          "type":"date"
        }
      }
    }
  }
}
Copy the code

View the indexes created and the mappings in the indexes

View index

GET /hello

View the mapping in the index

GET /hello/_mapping

The document

Inserted into the document

Insert a document PUT/index/type /{id}

PUT/hello/emp / 2, {" id ": 2," name ":" bill ", "age" : 25, "bir" : "2000-03-23"}Copy the code

Generate “_id” randomly without specifying ID: “2jM6Y3gBsVEMiPvdjyzC”

POST/hello/emp / {" id ": 3," name ":" detective ", "age" : 42, "bir" : "2020-03-23"}Copy the code

Query the document

/hello/emp/{id}

GET /hello/emp/1 GET /hello/emp/2

Query all documents

GET /hello/emp/_search

Delete the document

DELETE /hello/emp/2

Update the document

1. Update This update does not retain the original data update -> delete before insert

POST/hello/emp / 2 jm6y3gbsvemipvdjyzc {" name ":" extrajudicial fanatics "}Copy the code

2. Retain the original data for update or add new fields

POST/hello/emp / 2 jm6y3gbsvemipvdjyzc / _update {" doc ": {" name" : "ruthless", "dept" : "goodfellas"}}Copy the code

The full text indexing

To prepare

PUT /ems
{
"mappings": {
 "emp":{
   "properties":{
     "name":{
       "type":"text"
     },
     "age":{
       "type":"integer"
     },
     "bir":{
       "type":"date"
     },
     "content":{
       "type":"text"
     },
     "address":{
       "type":"keyword"
     }
   }
 }
}
}
Copy the code

The batch

PUT/ems/emp / _bulk {" index ": {}} {" name" : "black", "age" : 23, "bir" : "2012-12-12", "content" : "for the development team to choose a good MVC framework is a difficult thing, In many feasible scheme decision requires a high level and experience ", "address", "Beijing"} {" index ": {}} {" name" : "wang black", "age" : 24, "bir" : "2012-12-12", "content" : "Spring A framework is a layered architecture consisting of seven well-defined modules. Spring modules are built on top of the core container, Core container defines the way to create, configure, and manage bean ", "address", "Shanghai"} {" index ": {}} {" name" : "five small", "age" : 8, "bir" : "2012-12-12", "content" : "Spring Cloud As a microservices framework for the Java language, it relies on Spring Boot and is characterized by rapid development, continuous delivery, and easy deployment. Spring Cloud has many components that cover all aspects of microservices, The well is getting better and better with the help of the open source community Spring and Netflix and Pivotal. {"name":"win7","age":9,"bir":"2012-12-12"," Content ":" The goal of Spring is to simplify Java development from all aspects. This certainly leads to more explanations of how Spring simplifies Java development. ", "address", "nanjing} {" index" : {}} {" name ":" plum ", "age" : 43, "bir" : "2012-12-12", "content" : "use ANSI Redis is an open source C language, network support, can be based on memory and persistent logging, key-value database, "Address ":" hangzhou "} {"index":{}} {"name":" ElasticSearch ","age":59,"bir":"2012-12-12"," Content ":"ElasticSearch is a lucene-based search server. It provides a distributed multi-user capable full-text search engine based on the RESTful Web interface ","address":" Beijing "}Copy the code

ES advanced query

The QueryString way

***QueryString sort :desc reverse paging from(offset) size size (limit) ***

GET /ems/emp/_search? q=*&sort=age:desc&size=5&from=4&_source=name,age,birCopy the code
GET /ems/emp/_search? q=springCopy the code

QueryDSL query

Query all
GET /ems/emp/_search
{
  "query":{
    "match_all": {}
  }
}
Copy the code
Query all and sort sort
GET /ems/emp/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      },
      "address": {
        "order": "desc"
      }
    }
  ]
}
Copy the code

paging

GET /ems/emp/_search
{
"query": {
 "match_all": {}
},
"size": 2,
"from": 4
}
Copy the code
Specified field lookup
GET /ems/emp/_search
{
  "query": {
    "match_all": {}
  },
  "size": 2,
  "from": 4
  , "_source": ["name","address"]
}
Copy the code
Keyword search

Name is text and it’s going to look up words that contain little

GET/ems/emp / _search {" query ": {" term" : {" name ": {" value" : "small"}}}}Copy the code

Bir is a date type and does not search for word segmentation, so it cannot find data according to the whole search

GET /ems/emp/_search/
{
"query": {
 "term": {
   "bir": {
     "value": "2012-12-01"
   }
 }
}
}
Copy the code
Find the range

Select * from age>=5,<=10

GET /ems/emp/_search
{
"query": {
 "range": {
   "age": {
     "gte": 5,
     "lte": 8
   }
 }
}
}
Copy the code
The prefix keyword

Retrieves related documents that contain the keyword with the specified prefix

GET/ems/emp / _search {" query ": {" prefix" : {" name ": {" value" : "no"}}}}Copy the code

Note: the specified prefix does not mean that the name attribute in the metadata document is prefixed with “zhang”, but the matched data in the index area after the word segmentation, where “Zhang Wuji” after the word segmentation is: “zhang”, “none”, “ji”, whichever match points to that document

Wildcard query

? , which matches any single character

*, which can match zero or more characters, including an empty one

GET /ems/emp/_search { "query": { "wildcard": { "name": { "value": "? Small *"}}}}Copy the code
Id to find more
GET /ems/emp/_search
{
  "query": {
    "ids": {
      "values": ["2zN4Y3gBsVEMiPvdQCym","3TN4Y3gBsVEMiPvdQCyn"]
    }
  }
}
Copy the code
Fuzzy query
GET /ems/emp/_search
{
"query": {
"fuzzy": {
"content": {
  "value": "clou"
}
}
}
}
Copy the code

Fuzzy query rules:

The maximum ambiguity error must be between 0 and 2

The search term length is 2, and no fuzzy 0 is allowed

Search keywords are 3-5 in length allowing one blur of 0, 1

Search keyword length greater than 5 allows a maximum of 2 obfuscation

Boolean query

Must, must, must

Should: equivalent to | | formed a line

Must_not: equivalent to! You can’t satisfy either of them

GET /ems/emp/_search { "query": { "bool": { "must": [ { "range": { "age": { "gte":8, "lte": 9 } } } ], "must_not": [{" fuzzy ": {" address" : {" value ":" * * "south}}}]}}}Copy the code
Highlight query
GET/ems/emp / _search {" query ": {" term" : {" name ": {" value" : "5"}}}, "highlight" : {" fields ": {" name" : {}}}}Copy the code

“Query” is used to highlight the result of the query, and is not used to operate on the original data. Custom HTML tags can be added to highlight using pre_tags and post_tags

GET/ems/emp / _search {" query ": {" term" : {" name ": {" value" : "5"}}}, "highlight" : {" pre_tags ": ["<span style='color:red'>"], "post_tags": ["</span>"], "fields": { "name":{} } } }Copy the code
Multi-field query
GET/ems/emp / _search {" query ": {" multi_match" : {" query ":" Chinese ", "fields" : (" name ", "content")}}}Copy the code

Note: Whether the keyword needs to be broken down for retrieval also depends on whether the specified field has a participle

Multiple fields increase word segmentation

GET /ems/emp/_search {"query": {"multi_match": {"query": "level decision "," Analyzer ": "standard", "fields": ["content","name"] } } }Copy the code

GET _cat/plugins

GET /_analyze {“text”: [” National anthem of the People’s Republic of China “], “Analyzer “: “ik_smart”}

Ik participle

To prepare

Delete the original EMS index data

DELETE /ems

Create a new index/type/constraint

Also added the use of IK word dividers for attributes

PUT /ems
{
"mappings":{
 "emp":{
   "properties":{
     "name":{
       "type":"text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word"
     },
     "age":{
       "type":"integer"
     },
     "bir":{
       "type":"date"
     },
     "content":{
       "type":"text",
       "analyzer": "ik_max_word",
       "search_analyzer": "ik_max_word"
     },
     "address":{
       "type":"keyword"
     }
   }
 }
}
}
Copy the code

Add data

PUT/ems/emp / _bulk {" index ": {}} {" name" : "black", "age" : 23, "bir" : "2012-12-12", "content" : "for the development team to choose a good MVC framework is a difficult thing, In many feasible scheme decision requires a high level and experience ", "address", "Beijing"} {" index ": {}} {" name" : "wang black", "age" : 24, "bir" : "2012-12-12", "content" : "Spring A framework is a layered architecture consisting of seven well-defined modules. Spring modules are built on top of the core container, Core container defines the way to create, configure, and manage bean ", "address", "Shanghai"} {" index ": {}} {" name" : "five small", "age" : 8, "bir" : "2012-12-12", "content" : "Spring Cloud As a microservices framework for the Java language, it relies on Spring Boot and is characterized by rapid development, continuous delivery, and easy deployment. Spring Cloud has many components that cover all aspects of microservices, The well is getting better and better with the help of the open source community Spring and Netflix and Pivotal. {"name":"win7","age":9,"bir":"2012-12-12"," Content ":" The goal of Spring is to simplify Java development from all aspects. This certainly leads to more explanations of how Spring simplifies Java development. ", "address", "nanjing} {" index" : {}} {" name ":" plum ", "age" : 43, "bir" : "2012-12-12", "content" : "use ANSI Redis is an open source C language, network support, can be based on memory and persistent logging, key-value database, "Address ":" hangzhou "} {"index":{}} {"name":" ElasticSearch ","age":59,"bir":"2012-12-12"," Content ":"ElasticSearch is a lucene-based search server. It provides a distributed multi-user capable full-text search engine based on the RESTful Web interface ","address":" Beijing "}Copy the code

test

GET/ems/emp / _search {" query ": {" term" : {" content ":" a "}}, "highlight" : {" pre_tags ": ["<span style='color:red'>"], "post_tags": ["</span>"], "fields": { "*":{} } } }Copy the code

type

IK segmentation provides two mapping types for document segmentation: IK_max_word and IK_smart

What is the difference between IK_max_word and IK_smart?

ik_max_word:

The text will be split into the most fine-grained, for example, “The National anthem of the People’s Republic of China” will be split into “the People’s Republic of China, the People’s Republic of China, the People’s Republic of China, the People’s Republic of China, the People’s Republic of China, the People’s Republic of China, the People’s Republic of China, the People’s Republic of China, the People’s Republic of China, the Republic of China, and the guo Guo, the national anthem of the People’s Republic of China”, will exhaust all possible combinations;

ik_smart:

Will do the coarsest split, such as “People’s Republic of China national anthem” will be split into “People’s Republic of China national anthem”.