Elasticsearch增刪改查

面向文檔

document數據格式node

  1. 應用系統的數據結構都是面向對象的,複雜的
  2. 對象數據存儲到數據庫中,只能拆解開來,變爲扁平的多張表,每次查詢的時候還得還原回對象格式,至關麻煩
  3. ES是面向文檔的,文檔中存儲的數據結構,與面向對象的數據結構是同樣的,基於這種文檔數據結構,es能夠提供複雜的索引,全文檢索,分析聚合等功能
  4. es的document用json數據格式來表達

Java數據

public class Employee {

  private String email;
  private String firstName;
  private String lastName;
  private EmployeeInfo info;
  private Date joinDate;

}

private class EmployeeInfo {
  
  private String bio; // 性格
  private Integer age;
  private String[] interests; // 興趣愛好

}
EmployeeInfo info = new EmployeeInfo();
info.setBio("curious and modest");
info.setAge(30);
info.setInterests(new String[]{"bike", "climb"});

Employee employee = new Employee();
employee.setEmail("zhangsan@sina.com");
employee.setFirstName("san");
employee.setLastName("zhang");
employee.setInfo(info);
employee.setJoinDate(new Date());

 數據庫數據

employee
id 	email 	first_name 	last_name 	join_date
001 	hangsan@sina.com 	san 	zhang 	2017/01/01
employee_info
employee_id 	bio 	age 	interests
001 	curious and modest 	30 	bike, climb

 Json數據

{
    "email":      "zhangsan@sina.com",
    "first_name": "san",
    "last_name": "zhang",
    "info": {
        "bio":         "curious and modest",
        "age":         30,
        "interests": [ "bike", "climb" ]
    },
    "join_date": "2017/01/01"
}

 集羣管理

GET /_cat/health?v

 

green:每一個索引的primary shard和replica shard都是active狀態的
yellow:每一個索引的primary shard都是active狀態的,可是部分replica shard不是active狀態,處於不可用的狀態
red:不是全部索引的primary shard都是active狀態的,部分索引有數據丟失了python

如今只啓動動了一個es進程,至關於就只有一個node。如今es中有一個index,就是kibana本身內置創建的index。因爲默認的配置是給每一個index分配5個primary shard和5個replica shard,並且primary shard和replica shard不能在同一臺機器上(爲了容錯)。如今kibana本身創建的index是1個primary shard和1個replica shard。當前就一個node,因此只有1個primary shard被分配了和啓動了,可是一個replica shard沒有第二臺機器去啓動。只要啓動第二個es進程,就會在es集羣中有2個node,而後那1個replica shard就會自動分配過去,而後cluster status就會變成green狀態。數據庫

新增

#語法
PUT /index/type/id
{
  "json數據"
}
# 添加商品1
PUT /ecommerce/product/1
{
    "name" : "gaolujie yagao",                        #商品名稱
    "desc" :  "gaoxiao meibai",                       #商品描述
    "price" :  30,								   #商品價格
    "producer" :      "gaolujie producer",            #生廠廠家
    "tags": [ "meibai", "fangzhu" ]                   #產品標籤
}
#添加商品2
PUT /ecommerce/product/2
{
    "name" : "jiajieshi yagao",
    "desc" :  "youxiao fangzhu",
    "price" :  25,
    "producer" :      "jiajieshi producer",
    "tags": [ "fangzhu" ]
}
#添加商品3
PUT /ecommerce/product/3
{
    "name" : "zhonghua yagao",
    "desc" :  "caoben zhiwu",
    "price" :  40,
    "producer" :      "zhonghua producer",
    "tags": [ "qingxin" ]
}

 es會自動創建index和type,不須要提早建立,並且es默認會對document每一個field都創建倒排索引,讓其能夠被搜索json

查詢

#語法
GET /index/type/id
GET /ecommerce/product/1

 

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "gaolujie yagao",
    "desc": "gaoxiao meibai",
    "price": 30,
    "producer": "gaolujie producer",
    "tags": [
      "meibai",
      "fangzhu"
    ]
  }
}

 修改

PUT /ecommerce/product/1
{
    "name" : "jiaqiangban gaolujie yagao",
    "desc" :  "gaoxiao meibai",
    "price" :  30,
    "producer" :      "gaolujie producer",
    "tags": [ "meibai", "fangzhu" ]
}

 刪除

DELETE /ecommerce/product/1

 查詢

query string search的由來:由於search參數都是以http請求的query string來附帶的數據結構

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
    "hits": {
    "total": 3,
    "max_score": 1,
    "hits": 
  ......
  {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "zhonghua yagao",
          "desc": "caoben zhiwu",
          "price": 40,
          "producer": "zhonghua producer",
          "tags": [
            "qingxin"
          ]
       ......
}

 

took:耗費了幾毫秒
timed_out:是否超時,這裏是沒有
_shards:數據拆成了5個分片,因此對於搜索請求,會打到全部的primary shard(或者是它的某個replica shard)
hits.total:查詢結果的數量,3個document
hits.max_score:score的含義,就是document對於一個search的相關度的匹配分數,越相關,就越匹配,分數也高
hits.hits:包含了匹配搜索的document的詳細數據curl

按售價降序排列工具

GET /ecommerce/product/_search?q=name:yagao&sort=price:desc

適用場景url

適用於臨時的在命令行使用一些工具,好比curl,快速的發出請求,來檢索想要的信息;若是查詢請求很複雜,是很難去構建的在生產環境中,幾乎不多使用query string search命令行

query DSL

DSL:Domain Specified Language,特定領域的語言
http request body:請求體,能夠用json的格式來構建查詢語法,比較方便,能夠構建各類複雜的語法,比query string search確定強大多了rest

查詢全部

GET /ecommerce/product/_search
{
  "query": { "match_all": {} }
}

條件查詢

查詢名稱包含yagao的商品,同時按照價格降序排序

GET /ecommerce/product/_search
{
    "query" : {
        "match" : {
            "name" : "yagao"
        }
    },
    "sort": [
        { "price": "desc" }
    ]
}

 分頁查詢

GET /ecommerce/product/_search
{
  "query": { "match_all": {} },
  "from": 1,
  "size": 1
}

 指定查詢

更加適合生產環境的使用,能夠構建複雜的查詢

GET /ecommerce/product/_search
{
  "query": { "match_all": {} },
  "_source": ["name", "price"]
  }

 query filter

過濾查詢

搜索商品名稱包含yagao,並且售價大於25元的商品

GET /ecommerce/product/_search
{
    "query" : {
        "bool" : {
            "must" : {
                "match" : {
                    "name" : "yagao" 
                }
            },
            "filter" : {
                "range" : {
                    "price" : { "gt" : 25 } 
                }
            }
        }
    }
}

 full-text search(全文檢索)

GET /ecommerce/product/_search
{
    "query" : {
        "match" : {
            "producer" : "yagao producer"
        }
    }
}

 

producer這個字段,會先被拆解,創建倒排索引

special   4
yagao   4
producer 1,2,3,4  
gaolujie 1  
zhognhua 3  
jiajieshi 2

yagao producer 會被拆解爲 yagao和producer

phrase search(短語搜索)

跟全文檢索相對應,相反,全文檢索會將輸入的搜索串拆解開來,去倒排索引裏面去一一匹配,只要能匹配上任意一個拆解後的單詞,就能夠做爲結果返回
phrase search,要求輸入的搜索串,必須在指定的字段文本中,徹底包含如出一轍的,才能夠算匹配,才能做爲結果返回

GET /ecommerce/product/_search
{
    "query" : {
        "match_phrase" : {
            "producer" : "yagao producer"
        }
    }
}

 highlight search(高亮搜索結果)

GET /ecommerce/product/_search
{
    "query" : {
        "match" : {
            "producer" : "producer"
        }
    },
    "highlight": {
        "fields" : {
            "producer" : {}
        }
    }
}
相關文章
相關標籤/搜索