1瞻想、新增索引
#請(qǐng)求
put 192.168.1.139:9200/test11
{
"mappings": {
"properties": {
"title": {
"type": "text"
},
"year": {
"type": "date",
"format": "yyyy"
},
"type": {
"type": "integer"
}
}
},
"settings": {
"index": {
"number_of_shards": 5,
"number_of_replicas": 1
}
}
}
# 返回
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "test11"
}
2、新增文檔
#請(qǐng)求
post 192.168.1.139:9200/test11/_doc/
{
"title": "標(biāo)題",
"year": "2024-01-18 12:00:34",
"type": 1,
"star": 5.2,
"director": "啦啦啦"
}
#返回
{
"_index": "test11",
"_id": "QlS7Go0BnqotedfyWh-N",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
3徘六、 查詢文檔
########### 查詢所有
get 192.168.1.139:9200/test11/_search/
{
"query":{
"match_all":{
}
}
}
#返回
{
"took": 163,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "test11",
"_id": "QlS7Go0BnqotedfyWh-N",
"_score": 1.0,
"_source": {
"title": "標(biāo)題",
"year": "2024-01-18 12:00:34",
"type": 1,
"star": 5.2,
"director": "啦啦啦"
}
}
]
}
}
########### 匹配查詢(match)
# 查看title 字段有"標(biāo)題"的
GET /test11/_search
{
"query":{
"match": {
"title": "標(biāo)題"
}
}
}
# 查看所有字段有"標(biāo)題"
body = {
"query": {
"multi_match": {
"query": "標(biāo)題",
}
}
}
# 查看title字段有"標(biāo)"or"題"的
GET /test11/_search
{
"query": {
"match": {
"title": {
"query": "標(biāo)題",
"analyzer": "standard"
}
}
}
}
3. 多字段查詢
# 查詢 title or director 含有標(biāo)的
GET /test11/_search/
{
"query": {
"multi_match": {
"query": "標(biāo)",
"fields": [
"title",
"director"
]
}
}
}
4. 詞條匹配(term)
# term 查詢被用于精確值 匹配内边,
# 這些精確值可能是數(shù)字、時(shí)間待锈、布爾或者那些未分詞的字符串(keyword)
GET /test11/_search/
{
"query":{
"term":{
"director":"標(biāo)題"
}
}
}
# term只能完整值匹配漠其,這樣就查詢不出來
GET /test11/_search/
{
"query":{
"term":{
"director":"標(biāo)"
}
}
}
對(duì)于未分詞的字符串查找
GET /test11/_search/
{
"query":{
"term":{
"director.keyword":"標(biāo)"
}
}
}
5. 范圍
GET test11/_search
{
"query": {
"range": {
"type": {
"gte": 0,
"lte": 1
}
}
}
}
6. 復(fù)雜查詢
# bool有3類查詢關(guān)系,must(都滿足),should(其中一個(gè)滿足),must_not(都不滿足)
GET test11/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"type": 1
}
},
{
"match": {
"title": "標(biāo)"
}
},
{
"range": {
"type": {
"gte": 1,
"lte": 2
}
}
}
]
}
}
}
7.通配符
# 諸如我的喪失女友竿音,逆我者亡等等
GET test11/_search
{
"query":{
"wildcard":{
"title":"*標(biāo)*"
}
}
}
8.多字段搜索
GET /test11/_search
{
"query": {
"match": {
"title": {
"query": "騰訊 收入",
"operator": "and"
}
}
}
}
9.類似數(shù)據(jù)表一樣和屎,條件、范圍春瞬、排序
GET test11/_search
{
"sort":{// 排序
"type":{"order":"asc"}// 類型排序柴信,asc正序,desc倒敘
}
#“sort”: { “update_time”: { “order”: “asc”,”mode”:”min” }}//對(duì)數(shù)字或者日期可以查詢多個(gè)值當(dāng)中min宽气、max随常、avg、sum排序
"from":0,//條目開始位置
"size":10,// 每頁數(shù)量
"query": {// 查詢條件
"bool": {
"must": [
{
"match": {
"type": 1
}
},
{
"match": {
"title": "標(biāo)"
}
},
{
"range": {
"type": {
"gte": 1,
"lte": 2
}
}
}
]
}
}
}
10萄涯、精準(zhǔn)匹配match_phrase
GET test11/_search
{
"query":{
"match_phrase":{
"title":"標(biāo)題1"
}
}
}
4绪氛、指定id插入文檔
PUT /test11/1
{
"title": "標(biāo)題11",
"year": "2024-01-18 12:00:34",
"type": 1,
"star": 5.2,
"director": "啦啦啦"
}
# 返回
{
"_index" : "test11",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 26,
"_primary_term" : 4
}
5、指定id獲取文檔
GET /test11/_doc/1
# 返回
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 26,
"_primary_term" : 4,
"found" : true,
"_source" : {
"title": "標(biāo)題11",
"year": "2024-01-18 12:00:34",
"type": 1,
"star": 5.2,
"director": "啦啦啦"
}
}
6涝影、修改文檔
# 單個(gè)文檔單個(gè)字段修改
post test11/_update/1
{
"doc": {
"title":"標(biāo)題2222"
}
}
#單個(gè)文檔全部字段修改
put test11/_doc/1
{
"title": "標(biāo)題333",
"year": "2024-01-19 12:00:34",
"type": 2,
"star": 5.3,
"director": "啦e啦"
}
# 全部文檔統(tǒng)一修改
POST /test11/_doc/_update_by_query
{
"query": {// 根據(jù)查詢條件修改枣察,若不需要,在這里不需要query,只要script
"match": {
"director": "啦e啦"
}
},
"script": {
"inline": "ctx._source['title'] = '都是這個(gè)標(biāo)題'"
}
}
7燃逻、刪除文檔
delete test11/_doc/1
8序目、獲取索引信息
# 查詢所有索引
get /_cat/indices
# 查詢單個(gè)索引
get /test11/
查詢對(duì)應(yīng)索引的數(shù)據(jù)結(jié)構(gòu)
`GET /test11/_mapping`
查詢對(duì)應(yīng)索引的數(shù)據(jù)條數(shù)
GET /test11/_count
9、刪除索引
delete /test11
10伯襟、修改索引
# 修改名字
## 方法1
為原索引增加別名猿涨,則就可以用別名作為新名字
## 方法2
新建一個(gè)索引,然后把就數(shù)據(jù)copy到新索引
# 遷移數(shù)據(jù)
POST _reindex
{
"source": {
"index": "test1"http:// 舊索引
},
"dest": {
"index": "test2"http:// 新索引
}
}
# 修改字段類型
由于es無法修改mapping姆怪,則需要曲線救國嘿辟,可通過新建索引的方式來修改字段類型
先新增新的索引
然后吧舊數(shù)據(jù)倒過來
POST _reindex
{
"source": {
"index": "test1"http:// 舊索引
},
"dest": {
"index": "test2"http:// 新索引
}
}
## 此外還可以進(jìn)行字段名修改舆瘪,新創(chuàng)建的索引字段名為create_time
PUT test2
{
"mappings": {
"properties": {
"create_time": {
"type": "text"
}
}
}
}
POST _reindex
{
"source": {
"index": "test1
},
"dest": {
"index": "test2"
},
"script": {
"source": "ctx._source.create_time= ctx._source.remove(\"create_date\")"
}
}