elasticsearch學習入門

(由於ES更新很快,本文這類快餐式的記錄僅供參考)

這幾年,搜索的開發門檻越來越低,每個語言都有開源的檢索工具包,而且功能越來越全,完整的解決方案也越來越多、越來越好用,比如lucene上就有solr, elasticsearch, sensei等。它們對於絕大部分的需求應該說都覆蓋了,解放了直接在檢索工具包上的開發工作量,讓人可以更多關注業務上的開發。個人比較看好elasticsearch(簡稱ES),ES的使用非常簡單,讓人感覺更多地在使用一個Nosql,而且允許很多插件功能可以自己開發。我們可以很容易通過rest客戶端去測試ES,因此學習起來很容易。

ES的官網有比較全面的API,但我看過以後感覺API的層次還是有點亂,至少沒有mongodb的文檔那麼簡單易讀。從簡單的應用開始慢慢認識ES的。比如要搭建箇中文新聞信息的搜索引擎,新聞有"標題"、"內容"、"作者"、"類型"、"發佈時間"這五個字段;我們要提供"標題和內容的檢索"、"排序"、"高亮"、"統計"、"過濾"等一些基本功能。ES提供了smartcn的中文分詞插件,測試的話建議使用IK分詞插件,也可以看看插件作者給的例子。

下載安裝好插件、啓動ES,之後就可以開始ES的體驗了。

1. 創建一個名叫test的索引

PUT http://localhost:9200/test

2. 創建mapping

POST http://localhost:9200/test/news/_mapping

內容爲:

{
    "news": {
       
        "properties": {
            "content": {
                "type": "string",
                "store": "no",
                "term_vector": "with_positions_offsets",
                "index_analyzer": "ik",
                "search_analyzer": "ik"
            }
,
            "title": {
                "type": "string",
                "store": "no",
                "term_vector": "with_positions_offsets",
                "index_analyzer": "ik",
                "search_analyzer": "ik",
                "boost": 5
            }
,
            "author": {
                "type": "string","index":"not_analyzed"
            }
,
            "publish_date": {
                "type": "date", "format":"yyyy/MM/dd","index":"not_analyzed"
            }
,
   "category": {
                "type": "string","index":"not_analyzed"
            }
        }
    }
}

URL的test、news在ES中是索引/類型,感覺是對應數據庫像庫名/表名的關係,POST內容中properties對應mapping裏的內容,裏面5個字段。type指出字段類型、內容、標題字段要進行分詞和高亮因此要設置分詞器和開啓term_vector。具體類型的API可以看這裏

3. 製造並提交一些數據:

POST http://localhost:9200/test/news/

內容隨便造若干條:

{
"content":"中國國務院總理訪問歐洲",
"title":"美國今年經濟形勢良好",
"publish_date":"2010/07/01",
"author":"張三",
"category":"財經"
}

4. 檢索 

POST http://localhost:9200/test/news/_search

內容包括幾個部分:

分頁:from/size、字段:fields、排序sort、查詢:query、過濾:filter、高亮:highlight、統計:facet

{
"from":0,
"size":10,
"fields":["title","content","publish_date","category", "author"],
"sort":[
        { "publish_date" : {"order" : "asc"} },
        "_score"
],
        "query":{
"bool":{
"should" : [
            {
                "term" : { "title" : "中國" }
            },
            {
                "term" : { "content" : "中國" }
            }
        ]}
},
"filter":{
  "range" : {
                "publish_date" : { 
                    "from" : "2010/07/01", 
                  "to" : "2010/07/21", 
                  "include_lower" : true, 
                  "include_upper" : false
                }
            }
},
"highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "title" : {}, 
"content" : {}
}
},
"facets" : {
     "cate" : { "terms" : {"field" : "category"} }
    }
}


這裏query我選擇構造了個標題或內容中含”中國“, 

結果如下:

{
  "took" : 18,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : null,
    "hits" : [ {
      "_index" : "test",
      "_type" : "news",
      "_id" : "_FM13zCCSnWaTPOuzIok_A",
      "_score" : 0.024621923,
      "fields" : {
        "content" : "路透社報道,美國昨天在伊拉克如何如何,中國將如何面對形勢。",
        "author" : "張三",
        "title" : "美國留給伊拉克的是個爛攤子",
        "category" : "政治",
        "publish_date" : "2010/07/10"
      },
      "highlight" : {
        "content" : [ "路透社報道,美國昨天在伊拉克如何如何,<tag2>中國</tag2>將如何面對形勢。" ]
      },
      "sort" : [ 1278720000000, 0.024621923 ]
    }, {
      "_index" : "test",
      "_type" : "news",
      "_id" : "4FEY1T6-RMOmOJYts4FoAQ",
      "_score" : 0.024621923,
      "fields" : {
        "content" : "美聯社報道,中國將於今天訪問俄羅斯的北部地區。",
        "author" : "張三",
        "title" : "美國訪問俄羅斯",
        "category" : "政治",
        "publish_date" : "2010/07/11"
      },
      "highlight" : {
        "content" : [ "美聯社報道,<tag2>中國</tag2>將於今天訪問俄羅斯的北部地區。" ]
      },
      "sort" : [ 1278806400000, 0.024621923 ]
    }, {
      "_index" : "test",
      "_type" : "news",
      "_id" : "lL-8BzCNTT2yEkqs1Owc_A",
      "_score" : 0.61871845,
      "fields" : {
        "content" : "美國國務卿希拉里對記者說,韓國首爾江南區的大部分富豪都有移居朝鮮的意願。",
        "author" : "李四",
        "title" : "中國經濟將面臨下滑危險",
        "category" : "經濟",
        "publish_date" : "2010/07/12"
      },
      "highlight" : {
        "title" : [ "<tag2>中國</tag2>經濟將面臨下滑危險" ]
      },
      "sort" : [ 1278892800000, 0.61871845 ]
    }, {
      "_index" : "test",
      "_type" : "news",
      "_id" : "DNB6gTpSRaOexC1axCpZ0Q",
      "_score" : 0.048311904,
      "fields" : {
        "content" : "中國國務院總理談到國內體育,金牌無疑是最重要的,美國",
        "author" : "張三",
        "title" : "美國體育的舉國體制",
        "category" : "體育",
        "publish_date" : "2010/07/14"
      },
      "highlight" : {
        "content" : [ "<tag2>中國</tag2>國務院總理談到國內體育,金牌無疑是最重要的,美國" ]
      },
      "sort" : [ 1279065600000, 0.048311904 ]
    }, {
      "_index" : "test",
      "_type" : "news",
      "_id" : "4lH55yoAQVe7cARIYVfYnQ",
      "_score" : 0.048311904,
      "fields" : {
        "content" : "中國對東南亞海域的軍事威脅將持續,俄羅斯嚴密關注事態發展",
        "author" : "張三",
        "title" : "各國大力發展教育事業",
        "category" : "政治",
        "publish_date" : "2010/07/15"
      },
      "highlight" : {
        "content" : [ "<tag2>中國</tag2>對東南亞海域的軍事威脅將持續,俄羅斯嚴密關注事態發展" ]
      },
      "sort" : [ 1279152000000, 0.048311904 ]
    }, {
      "_index" : "test",
      "_type" : "news",
      "_id" : "hFovoBdCTI-2ErYAQV-AKg",
      "_score" : 0.12422675,
      "fields" : {
        "content" : "中國經濟反彈乏力,美國經濟持續低迷,其他經濟體也靠不住",
        "author" : "李四",
        "title" : "歐洲債務危機席捲全球",
        "category" : "經濟",
        "publish_date" : "2010/07/19"
      },
      "highlight" : {
        "content" : [ "<tag2>中國</tag2>經濟反彈乏力,美國經濟持續低迷,其他經濟體也靠不住" ]
      },
      "sort" : [ 1279497600000, 0.12422675 ]
    } ]
  },
  "facets" : {
    "cate" : {
      "_type" : "terms",
      "missing" : 0,
      "total" : 10,
      "other" : 0,
      "terms" : [ {
        "term" : "政治",
        "count" : 4
      }, {
        "term" : "經濟",
        "count" : 3
      }, {
        "term" : "體育",
        "count" : 3
      } ]
    }
  }
}

結果包含了需要的幾個部分。值得注意的是,facet的統計是命中的結果進行統計,filter是對結果進行過濾,filter不會影響facet,如果要統計filter掉的的就要使用filter facet。

ES提供了非常多的功能,JSON的組合比一般的SQL這樣的語法要凌亂,但是通過組合能夠滿足各種複雜的應用,很多影響性能的設置也值得好好去看。對開發者而已,利用好內置API接口進行二次開發也非常必要,比如開發自己的分詞,和其他庫的同步等。ES現在還在0.19.9 但是已經出現了很多官方的培訓,所以前途應該不可限量,未來可能會是檢索系統解決方案的首選。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章