es syntax operation command

Inquire


#查询索引
GET person

#查询全部索引的状态数据
GET /_cat/indices?v

#查询
GET http://ip:端口/索引名称  # 查询单个索引信息
GET http://ip:端口/索引名称1,索引名称2...  # 查询多个索引信息
GET http://ip:端口/_all  # 查询所有索引信息

#查询索引user映射
GET user/_mapping

#查询user索引文档,指定id
GET /user/_doc/1

#查询user索引所有文档
GET /user/_search

Add to

#添加
PUT http://ip:端口/索引名称

#添加索引
PUT user

#创建索引并添加映射(推荐)
PUT /person1
{
    
    
  "mappings": {
    
    
    "properties": {
    
    
      "name": {
    
    
        "type": "text"
      },
      "age": {
    
    
        "type": "integer"
      }
    }
  }
}

#添加字段(属性)
PUT /person1/_mapping
{
    
    
  "properties": {
    
    
    "name": {
    
    
      "type": "text"
    },
    "age": {
    
    
      "type": "integer"
    },
    "createTime": {
    
    
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      }
  }
}
##如果需要用时间范围查询,必须要设置"type": "date""format"
#请注意,在设置日期映射时,建议使用Elasticsearch支持的日期格式,并根据需要选择正确的日期格式。此外,如果您已经创建了索引并且需要更新日期映射,可以使用Update Mapping API来更新。

#添加文档,指定id
POST /person/_doc/1
{
    
    
  "name":"张三",
  "age":18,
  "address":"北京"
}

#添加文档,不指定id
POST /person1/_doc/
{
    
    
  "name":"张三",
  "age":18,
  "address":"北京"
}

#字段"tel"不可以进行查询,不然会报错只因创建映射时"tel""index"false
{
    
    
    "user": {
    
    
        "mappings": {
    
    
            "properties": {
    
    
                "name": {
    
    
                    "type": "text"
                },
                "sex": {
    
    
                    "type": "keyword"
                },
                "tel": {
    
    
                    "type": "keyword",
                    "index": false
                }
            }
        }
    }
}



#实现模糊检索,类似于SQL中的like条件,这个需求我们使用wildcard类型字段来实现. 
对同一个字段即可实现模糊检索,也能实现分词检索(我们原来使用text类型字段+ik分词器实现),这两种检索应用于同一字段
定义字段 name:【使用示例,就是在已经固定的mapping映射上加属性,因为已经添加的属性不能修改,只能添加】
"name": {
    
    
	"type": "text",
	"fields": {
    
    
  		"wildcard": {
    
    
  		"type": "wildcard"
  		},
  		"ik": {
    
    
  		"type": "text",
  		"analyzer": "ik_max_word"
  		}
	}
}
#然后进行检索如下,可以同时对 name.wildcardname.ik 字段进行检索
{
    
    
	"query": {
    
    
		"multi_match": {
    
    
		"query": "查询关键词",
		"fields": ["name.wildcard", "name.ik"]
		}
	}
}
 




Revise

#局部更新
POST /user/_update/4
{
    
    
  "doc": {
    
    
    "email":"88886"
  }
}

#全量更新,存在就更新,不存在就创建
POST /user/_doc/4
{
    
    
   "name" : "王九",
   "sex" : "男性",
   "tel" : "99"
}


delete

#删除索引
DELETE http://ip:端口/索引名称

#删除指定id文档
DELETE /person/_doc/1

#删除某个文档中的属性
POST user/_doc/1/_update
{
    
    
    "script" : "ctx._source.remove('new_field')"
}

# 按照查询条件进行删除
POST user/_delete_by_query
{
    
    
  "query":{
    
    
    "match_phrase":{
    
    
      "catagoryId": "1643191714195750913"
    }
  }
}

Advanced ur path query

#查询name端包含王或者九的
GET /user/_search?q=name:王九

#URL带参数形式查询,这很容易让不善者心怀恶意,或者参数值出现中文会出现乱码情况。为了避免这些情况,我们可用使用带JSON请求体请求进行查询

Advanced query term

#term是代表完全匹配,也就是精确查询,搜索前不会再对搜索词进行分词拆解
GET user/_search
{
    
    
  "query": {
    
    
    "term": {
    
    
      "FIELD": {
    
    
        "value": "VALUE"
      }
    }
  }
}


#terms里的[ ] 多个是或者的关系,只要满足其中一个词就可以
GET user/_search
{
    
    
  "query": {
    
    
    "terms": {
    
    
      "FIELD": [
        "VALUE1",
        "VALUE2"
      ]
    }
  }
}

#terms里的[ ] 多个是或者的关系,只要满足其中一个词就可以。想要通知满足两个词的话,就得使用boolmust来做
GET user/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": [
        {
    
    "term": {
    
    
          "FIELD": {
    
    
            "value": "VALUE"
          }
        }},
        {
    
    
          "term": {
    
    
            "FIELD": {
    
    
              "value": "VALUE"
            }
          }
        }
      ]
    }
  }
}

Advanced query match

#查找所有文档内容
GET user/_search
{
    
    
	"query":{
    
    
		"match_all":{
    
    }
	}
}


# 查询全部文档指定字段
GET user/_search
{
    
    
	"query":{
    
    
		"match_all":{
    
    }
	},
	"_source":["FIELD"]
}





#match进行搜索的时候,会先进行分词拆分,拆完后,再来匹配
GET user/_search
{
    
    
  "query": {
    
    
    "match": {
    
    
      "FIELD": "TEXT"
    }
  }
}

#match_phrase 称为短语搜索,要求所有的分词必须同时出现在文档中,同时位置必须紧邻一致
GET user/_search
{
    
    
"query": {
    
    
  "match_phrase": {
    
    
    "FIELD": "PHRASE"
  }
}


# 上面的 MatchQuery 有一个短板,假如用户输入了某关键字,我们在查找的时候并不知道他输入的是 name 还是 description,这时我们用什么都不合适,而 MultiQuery 的出现解决了这个问题,他可以通过 fields 属性来设置多个域联合查找
GET user/_search
{
    
    
    "query": {
    
    
        "multi_match": {
    
    
            "query": "Spring开发",
            "minimum_should_match": "70%",
            "fields": ["name", "description"]
        }
    }
}
}


# 在多域联合查询的时候,可以通过 boost 来设置某个域在计算得分时候的比重,比重越高的域当他符合条件时计算的得分越高,相应的该记录也更靠前。通过在 fields 中给相应的字段用 ^权重倍数来实现
GET user/_search
{
    
    
    "query": {
    
    
        "multi_match": {
    
    
            "query": "Spring开发",
            "minimum_should_match": "70%",
            "fields": ["name^10", "description"]
        }
    }
}
}


Multi-condition query, must is equivalent to && in the database, should is equivalent to || in the database

#must相当于数据库的&&
GET user/_search
{
    
    
	"query":{
    
    
		"bool":{
    
    
			"must":[{
    
    
				"match":{
    
    
					"name":"小王"
				}
			},{
    
    
				"match":{
    
    
					"age":18
				}
			}]
		}
	}
}


#should相当于数据库的||
GET user/_search
{
    
    
	"query":{
    
    
		"bool":{
    
    
			"should":[{
    
    
				"match":{
    
    
					"name":"小王"
				}
			},{
    
    
				"match":{
    
    
					"age":18
				}
			}]
		}
	}
}

If we want to perform word segmentation query on some fields and precise query on other fields at the same time, we need to use Boolean query to achieve it. Boolean query corresponds to Lucene's BooleanQuery query. It implements the combination of multiple queries. There are three optional parameters: must: the document must match the query conditions included in must, which is equivalent to "AND" should: the document should match what should include One or more of the query conditions, equivalent to "OR" must_not: the document cannot match the query conditions included in must_not, equivalent to "NOT"

GET user/_search
{
    
    
    "query": {
    
    
        "bool": {
    
     // 布尔查询
            "must": [ // 查询条件 must 表示数组中的查询方式所规定的条件都必须满足
                {
    
    
                    "multi_match": {
    
    
                        "query": "王小妹",
                        "minimum_should_match": "50%",
                        "fields": [
                            "name^10",
                            "title"
                        ]
                    }
                },
                {
    
    
                    "term": {
    
    
                        "address": "广州"
                    }
                }
            ]
        }
    }
}

Paging query

GET user/_search
{
    
    
	"query":{
    
    
		"match_all":{
    
    }
	},
	"from":0,
	"size":2
}

Query sorting, support adding sorting to keyword, date, float and other types. Fields of text type are not allowed to be sorted. Text cannot be sorted because it will be divided into words

{
    
    
	"query":{
    
    
		"match_all":{
    
    }
	},
	"sort": [
    {
    
    
      "age": {
    
    
        "order": "desc"
      }
    }
  ]
}

range query

GET user/_search
{
    
    
	"query":{
    
    
		"bool":{
    
    
			"should":[{
    
    
				"match":{
    
    
					"name":"小王"
				}
			},{
    
    
				"match":{
    
    
					"age":18
				}
			}],
			"filter": [
			  {
    
    "range": {
    
    
			    "age": {
    
    
			      "gte": 10,
			      "lte": 20
			    }
			  }}
			]
		}
	}
}

Full Text Search

{
    
    
	"query":{
    
    
		"match":{
    
    
			"name" : "王妹"
		}
	}
}

Highlight query, the highlighted data is itself a field in the document, and the field will be returned to you in the form of highlight alone.

ES provides a highlight attribute, which is at the same level as query

GET user/_search
{
    
    
  "query": {
    
    
    "match_phrase": {
    
    
      "name": "张三"
    }
  },
  "highlight": {
    
    
    "fields": {
    
    
      "name": {
    
    }
    },
    "pre_tags": "<font color='red'>",
    "post_tags": "</font>",
    "fragment_size": 10
  }
    }
}


#全文高亮检索
GET person1/_search
{
    
    
  "query": {
    
    
    "multi_match": {
    
    
      "query": "广州",
      "fields": []
    }
  },
  "highlight": {
    
    
    "fields": {
    
    
      "*": {
    
    }
    }
  }
}

fragment_size: Specify how many characters to display in the highlighted data;
pre_tag: Specify the prefix tag, such as <font color="red">
post_tags: Specify the suffix tag, such as </font>
field: Specify which field is the highlighted field

The definition of filter query is to filter the data based on the original query results, so the step of recalculating the score is omitted, which is more efficient. And easy to cache. It is recommended to use filters to implement queries as much as possible or to use filters and queries together. Filters are used in Boolean queries, and filter queries can have the same effect as retrieval queries. The difference is that filtering queries are not scored, and the results can be cached, while retrieval queries are scored, and the results are not cached. Generally, filter queries are not used directly, but are used after retrieving certain data. The following is to filter based on search results

GET user/_search
{
    
    
    "query": {
    
    
        "bool": {
    
    
            "must": [
                {
    
    
                    "multi_match": {
    
    
                        "query": "王小妹",
                        "minimum_should_match": "50%",
                        "fields": [
                            "name^10",
                            "title"
                        ]
                    }
                }
            ],
            "filter": [
                {
    
    
                    // 过滤条件:studymodel 必须是 201001
                    "match": {
    
    "address": "广州"}
                },
                {
    
    
                    // 过滤条件:年龄 >=10 <=100
                    "range": {
    
    "age": {
    
    "gte": 10,"lte": 100}}
                }
            ]
        }
    }
}

Group query

GET user/_search
{
    
    
  "aggs": {
    
    
    "gg_gl": {
    
    
      "terms": {
    
    
        "field": "age"
      }
    }
  },
  "size": 0
}

Advanced mapping nesting** [Field type is set to keyword and will not be word-segmented]

#嵌套节点下面新增字段
PUT /user/_mapping

{
    
    
    "properties": {
    
    
        "school": {
    
    
            "type": "keyword",
            "norms": false,
            "doc_values": false
        },
        "teacher": {
    
    
            "type": "nested",
            "properties": {
    
    
                "rightStatus": {
    
    
                    "type": "keyword",
                    "norms": false,
                    "doc_values": false
                },
                "rightCurrTime": {
    
    
                    "type": "keyword",
                    "norms": false,
                    "doc_values": false
                }
            }
        }
    }
}

#Close and open the index (closing the index is to release the memory, persist the data to the hard disk, and cannot read and write the closed index)

POST http://ip:端口/索引名称/_close  

POST http://ip:端口/索引名称/_open ```

The IK tokenizer has two segmentation modes: ik_max_word and ik_smart mode

Linux system installs word breaker, first docker installs es

enter into es

docker exec -it elasticsearch /bin/bash

Execute the installation of the ik word breaker (note that the installed word breaker needs to be consistent with the es version, if it is inconsistent, change the version number of the ik word breaker)

./bin/elasticsearch-plugin  install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.0/elasticsearch-analysis-ik-7.12.0.zip

After the installation is successful, you can see the ik file under the plugin of the host es volume, and then you must restart the es container

docker restart elasticsearch 

Operate on kibana and view the result of the tokenizer**【Note that if the English letter is capitalized, it will become lowercase after using the default tokenizer】

GET /_analyze
{
    
    
  "analyzer": "ik_smart",
  "text": "东哥是前端大佬"
}


#结果
{
    
    
  "tokens" : [
    {
    
    
      "token" : "东",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "CN_CHAR",
      "position" : 0
    },
    {
    
    
      "token" : "哥",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "CN_CHAR",
      "position" : 1
    },
    {
    
    
      "token" : "是",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "CN_CHAR",
      "position" : 2
    },
    {
    
    
      "token" : "前端",
      "start_offset" : 3,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
    
    
      "token" : "大佬",
      "start_offset" : 5,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 4
    }
  ]
}

ES data type

Aggregation: equivalent to sum (summation) in mysql
text: word segmentation, no aggregation
keyword: no word segmentation, all content as an entry, support aggregation
integer: value type
boolean: Boolean
binary: binary
integer_range, float_range, long_range, double_range, date_range: range type
date: date
[] Nested: nested (for arrays of JSON objects JSON objects of array type): array
{ } Object: object (for single JSON objects single JSON object): object

GET: used to obtain resources
POST: used to create new resources (can also be used to update resources)
PUT: used to update resources
DELETE: used to delete resources

  • An index in ES 5.x can have multiple types.
  • An index in ES 6.x can only have one type.
  • After ES 7.x, the concept of type will be gradually removed. The current operation is no longer used. The default _doc
    #View the field mapping under the index. If the es version is 7, you can use the following command, and add format after mapping? =json&include_type_name=true
    GET /user/_doc/_mapping?format=json&include_type_name=true

Guess you like

Origin blog.csdn.net/qq_19891197/article/details/129505835