es的增刪改查

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\")"
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市红伦,隨后出現(xiàn)的幾起案子英古,更是在濱河造成了極大的恐慌,老刑警劉巖昙读,帶你破解...
    沈念sama閱讀 212,599評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件召调,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡蛮浑,警方通過查閱死者的電腦和手機(jī)唠叛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,629評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來沮稚,“玉大人艺沼,你說我怎么就攤上這事≡烫停” “怎么了障般?”我有些...
    開封第一講書人閱讀 158,084評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長盛杰。 經(jīng)常有香客問我挽荡,道長,這世上最難降的妖魔是什么即供? 我笑而不...
    開封第一講書人閱讀 56,708評(píng)論 1 284
  • 正文 為了忘掉前任定拟,我火速辦了婚禮,結(jié)果婚禮上逗嫡,老公的妹妹穿的比我還像新娘青自。我一直安慰自己,他們只是感情好驱证,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,813評(píng)論 6 386
  • 文/花漫 我一把揭開白布延窜。 她就那樣靜靜地躺著,像睡著了一般雷滚。 火紅的嫁衣襯著肌膚如雪需曾。 梳的紋絲不亂的頭發(fā)上吗坚,一...
    開封第一講書人閱讀 50,021評(píng)論 1 291
  • 那天祈远,我揣著相機(jī)與錄音,去河邊找鬼商源。 笑死车份,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的牡彻。 我是一名探鬼主播扫沼,決...
    沈念sama閱讀 39,120評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼出爹,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了缎除?” 一聲冷哼從身側(cè)響起严就,我...
    開封第一講書人閱讀 37,866評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎器罐,沒想到半個(gè)月后梢为,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,308評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡轰坊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,633評(píng)論 2 327
  • 正文 我和宋清朗相戀三年铸董,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片肴沫。...
    茶點(diǎn)故事閱讀 38,768評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡粟害,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出颤芬,到底是詐尸還是另有隱情悲幅,我是刑警寧澤,帶...
    沈念sama閱讀 34,461評(píng)論 4 333
  • 正文 年R本政府宣布驻襟,位于F島的核電站夺艰,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏沉衣。R本人自食惡果不足惜郁副,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,094評(píng)論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望豌习。 院中可真熱鬧存谎,春花似錦狮惜、人聲如沸险毁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,850評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽攒霹。三九已至抓谴,卻和暖如春魂挂,著一層夾襖步出監(jiān)牢的瞬間链患,已是汗流浹背吸占。 一陣腳步聲響...
    開封第一講書人閱讀 32,082評(píng)論 1 267
  • 我被黑心中介騙來泰國打工晴叨, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人矾屯。 一個(gè)月前我還...
    沈念sama閱讀 46,571評(píng)論 2 362
  • 正文 我出身青樓兼蕊,卻偏偏與公主長得像,于是被迫代替她去往敵國和親件蚕。 傳聞我的和親對(duì)象是個(gè)殘疾皇子孙技,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,666評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容