elasticsearch学习笔记——二.querystring查询document方式与dsl查询document方式
// 1.創建test_serach索引,指定主要分片數量,副本分片數量,文檔中字段的數據類型,分詞器,是否作為搜索條件
PUT test_search
{
“settings”: {
“number_of_shards”: 2,
“number_of_replicas”: 1
},
“mappings”: {
“test_type”:{
“properties”:{
“dname”:{
“type”:“text”,
“analyzer”:“standard”
},
“ename”:{
“type”:“text”,
“analyzer”:“standard”
},
“eage”:{
“type”:“long”
},
“hiredate”:{
“type”:“date”
},
“gender”:{
“type”:“keyword”
}
}
}
}
}
// 2.批量向test_search索引中新增文檔數據12條
POST test_search/test_type/_bulk
{“index”:{}}
{“dname”:“Sales Department”,“ename”:“張三”,“eage”:20,“hiredate”:“2019-01-01”,“gender”:“男性”}
{“index”:{}}
{“dname”:“Sales Department”,“ename”:“李四”,“eage”:21,“hiredate”:“2019-01-01”,“gender”:“男性”}
{“index”:{}}
{“dname”:“Development Department”,“ename”:“王五”,“eage”:23,“hiredate”:“2019-01-03”,“gender”:“男性”}
{“index”:{}}
{“dname”:“Development Department”,“ename”:“趙柳”,“eage”:20,“hiredate”:“2018-01-01”,“gender”:“男性”}
{“index”:{}}
{“dname”:“Development Department”,“ename”:“韓梅梅”,“eage”:24,“hiredate”:“2019-03-01”,“gender”:“女性”}
{“index”:{}}
{“dname”:“Development Department”,“ename”:“錢哄”,“eage”:29,“hiredate”:“2018-03-01”,“gender”:“女性”}
{“index”:{}}
{“dname”:“Sales Department”,“ename”:“張三2”,“eage”:20,“hiredate”:“2019-01-01”,“gender”:“男性”}
{“index”:{}}
{“dname”:“Sales Department”,“ename”:“李四2”,“eage”:21,“hiredate”:“2019-01-01”,“gender”:“男性”}
{“index”:{}}
{“dname”:“Development Department”,“ename”:“王五2”,“eage”:23,“hiredate”:“2019-01-03”,“gender”:“男性”}
{“index”:{}}
{“dname”:“Development Department”,“ename”:“趙柳2”,“eage”:20,“hiredate”:“2018-01-01”,“gender”:“男性”}
{“index”:{}}
{“dname”:“Development Department”,“ename”:“韓梅梅2”,“eage”:24,“hiredate”:“2019-03-01”,“gender”:“女性”}
{“index”:{}}
{“dname”:“Development Department”,“ename”:“錢哄2”,“eage”:29,“hiredate”:“2018-03-01”,“gender”:“女性”}
// (參考執行)3.批量刪除文檔
POST test_search/test_type/_bulk
{“delete”:{"_id":“l-WMEnkBwUn15EIMlpdn”}}
{“delete”:{"_id":“mOWMEnkBwUn15EIMlpdn”}}
{“delete”:{"_id":“k-WKEnkBwUn15EIMtJcX”}}
{“delete”:{"_id":“lOWKEnkBwUn15EIMtJcX”}}
{“delete”:{"_id":“luWKEnkBwUn15EIMtJcX”}}
{“delete”:{"_id":“m-WMEnkBwUn15EIMlpdn”}}
// 4.querystringz方式查詢
// 4.1 魔鬼查詢
GET test_search/test_type/_search
// 解決:指定時間,超過時間就返回數據,took是多線程每一個縣城執行時間的和,至少是 分片數+1 的線程
GET test_search/test_type/_search?timeout=1ms
// 4.1 查詢所有索引下數據
GET _search
// 4.1 查詢指定索引下數據
GET wmr_index,test_search,test_index1/_search
// 4.1 查詢指定索引下數據
GET test*/_search
// 4.1 查詢所有索引下數據
GET _all/_search
// 4.2分頁查詢
GET test_search/test_type/_search?size=18
// 4.2 分頁查詢,0是第一條數據
GET test_search/_search?from=0&size=1
// 4.3 條件查詢,q去查詢所有字段
GET test_search/_search?q=24
// 4.3 條件查詢,不支持utf-8。失敗
GET test_search/_search?q=女性
// 4.3條件查詢,指定字段查詢:分割
GET test_search/_search?q=eage:21
// 4.4±查詢,-除了年齡是21的文檔都返回
GET test_search/_search?q=-eage:21
// 4.5排序
GET test_search/_search?sort=eage
// 5.dsl查詢方式 支持utf-8中文不會亂碼
GET test_search/_search
{
“query”:{
“match_all”:{}
}
}
GET test_search/_search
{
“query”:{
“match”:{
“ename”:“李四”// 可查出來文檔
}
}
}
GET test_search/_search
{
“query”:{
“match_phrase”:{
“ename”:“李四2”
}
}
}
GET test_search/_search
{
“query”: {
“range”: {
“eage”: {
“gte”: 20,
“lte”: 23
}
}
}
}
GET test_search/_search
{
“query”: {
“range”: {
“hiredate”: {
“gte”: “2018-01-01”,
“lte”: “2019-01-01”
}
}
}
}
GET test_search/_search
{
“query”: {
“term”: {
“dname”:“Sales Department”
}
}
}
GET test_search/_search
{
“query”: {
“bool”: {
“must”: [
{“range”:{
“eage”:{
“lte”: 26,
“gte”: 20
}
}},{
“match”: {
“ename”: “張三”
}
},{
“match”: {
“dname”: “Sales Department”
}
}
]
}
}
}
GET test_search/_search
{
“query”: {
“match_all”: {
}
},
“sort”:[
{
“hiredate”: {
“order”: “desc”
}
},
{
“eage”: {
“order”: “asc”
}
}
],
“from”:1,
“size”:2
}
GET test_search/_search
{
“query”: {
“match”: {
“ename”: “韓梅梅”
}
}
, “highlight”: {
“fields”: {
“ename”:{
“fragment_size”:3,
“number_of_fragments”:1
}
},
“pre_tags”: “”,
“post_tags”: “”
}
}
總結
以上是生活随笔為你收集整理的elasticsearch学习笔记——二.querystring查询document方式与dsl查询document方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第一课 让人拍案叫绝的创意都是如何诞生的
- 下一篇: 基于物联网的室内环境监测系统设计的背景