Elasticsearch

介紹

  • Elasticsearch是一個實時的分佈式搜索分析引擎, 它被用做全文檢索、結構化搜索、分析以及這三個功能的組合;
  • Index 對應 DB,Type 對應表,Document 對應記錄

其餘

知識點

  • 集羣: 多臺物理機,經過配置一個相同的cluster name,組織成一個集羣
  • 節點: 同一個集羣中的一個 Elasticearch主機
  • 主分片: 索引的一個物理子集。同一個索引在物理上能夠切多個分片,分佈到不一樣的節點上。分片的實現是Lucene 中的索引, 個數是創建索引時就要指定的,創建後不可再改變
  • 副本分片: 每一個主分片能夠有一個或者多個副本,個數是用戶本身配置的
  • 索引:(database) 索引數據的存儲位置, 數據會被索引成一個index
  • 類型: (table)索引中的分類
  • 因爲索引幾乎有固定的開銷,因此建議使用較少的索引和較多的類型
  • 文檔: (row)ES中一個能夠被檢索的基本單位
  • 字段: (column)
  • 映射: (schema)

查詢

  • 以文檔ID查詢
get /_index/_type/_id

//例子
http://localhost:9200/library/article/57508457556e30622882ba58

//返回
//未找到
{
    _index: "library",    //索引
    _type: "article",     //類型
    _id: "57508457556e3043434",    //文檔ID
    found: false
}

//查找到
{
    _index: "library",
    _type: "article",
    _id: "57508457556e30622882ba58",
    _version: 1,        //版本號,每次改動會+1
    found: true,
    _source: {          //文檔所有內容
        ........
    }
}
  • 以屬性參數查詢
get /_index/_type/_search?q=
//例子
http://localhost:9200/library/article/_search?q=title:pariatur

//未找到
{
    took: 1,                  //查詢花費的時間,單位毫秒
    timed_out: false,     //是否超時
    _shards: {                  //描述分片的信息
        total: 5,                  //查詢了多少個分片
        successful: 5,           //成功的分片數量
        skipped: 0,                //跳過的分片數量
        failed: 0                 //失敗的分片數量
    },
    hits: {                       //搜索的結果
        total: 0,                  //所有的知足條件的文檔數目
        max_score: null,      //文檔最大得分
        hits: [ ]
    }
}

//正則, 以空格分開等於OR, 注意括號區分優先級
name.raw:/.*jinks.*/ OR username.raw:(/.*jinks.*/ OR "jinks")
  • 使用DSL查詢,提交JSON格式參數
get http://localhost:9200/library/article/_search
參數:
{
  "query": {
    "match": {
      "title": "pariatur"
    }
  },
  "from": 0, //表示從第幾行開始(默認0) 
  "size": 10 //表示查詢多少條文檔(默認10)
} 
//注意:若是搜索size大於10000,須要設置index.max_result_window參數,size的大小不能超過index.max_result_window這個參數的設置,默認爲10,000。


//至關於
{
  "query": {
            "bool": {
                "must": [{
                        "match": {
                       "title": "pariatur"
                }  
                }]
            }
  },
  "size": 10
}
  • 多條件查詢 bool:must、filter、should
{
  "query": {
    "bool" : {  //成爲query的過濾器,還有其它的,如:and,or,not,limit
      "must" : {  //must,filter,should爲過濾條件,若是有多個子條件,使用[]
        "term" : { "user" : "kimchy" }
      },
      "filter": {
        "term" : { "tag" : "tech" }
      },
      "must_not" : {
        "range" : {
          "age" : { "gte" : 10, "lte" : 20 }
        }
      },
      "should" : [
        { "term" : { "tag" : "wow" } },
        { "term" : { "tag" : "elasticsearch" } }
      ],
      "minimum_should_match" : 1,  //用於如今should的子條件匹配個數。
      "boost" : 1.0 //表示此過濾器的權重,默認1.0
    }
  }
}


//filter 對於數字: gt, lt, ge, le
  • 短語查詢: match_phrase
//6.x
{
    "query" : {
        "match_phrase" : {
             "title": "xxxx1 xxxx2"    //非短語搜索爲或
         }
    }
}

//5.x
{
    "query": {
        "match" : {
            "title": {
                 "query": "xxxx1 xxxx2",
                 "type": "phrase"
             }
         }
    }
}
  • 高亮搜索
{
    "query": {...},
     "highlight": {
         "fields" : {
             "name": {}
         } 
     }
}

//返回
{
    items: [{
        ....,
        hightlight: [{name: ...<em>..<em>..}]
    }],
    counts: {...}
}

開發思路

相關文章
相關標籤/搜索