>cd elasticsearch/bin
>elasticsearch.bat
若是沒有配置JAVA_HOME,又不想在環境變量中配置 可手動打開 elasticsearch-env.bat 在裏面設置 set java_home=***html
啓動起來後,可經過 http://localhost:9200 便可查看結果。java
{ "name" : "3hKw88o", "cluster_name" : "elasticsearch", "cluster_uuid" : "0elyGgO2Rsqzj7v3yKX24g", "version" : { "number" : "6.2.3", "build_hash" : "c59ff00", "build_date" : "2018-03-13T10:06:29.741383Z", "build_snapshot" : false, "lucene_version" : "7.2.1", "minimum_wire_compatibility_version" : "5.6.0", "minimum_index_compatibility_version" : "5.0.0" }, "tagline" : "You Know, for Search" }
默認狀況下,elasticsearch只容許本機訪問,若想改變ip則須要修改文件git
config\elasticsearch.ymlweb
可修改port和 network地址:ajax
network.host: 192.168.201.105
elastic使用 curl 命令來訪問,但這種操做太不方便了,所以elasticsearch有個專用chrome插件 sense(sense下載和安裝參見 瀏覽器chrome插件)。chrome
或者使用 ElasticSearch-head插件可作集羣的傻瓜式操做。數據庫
默認打開後,直接訪問報錯:json
elasticsearch6.x {"error":"Content-Type header [application/x-www-form-urlencoded] is not supported"api
緣由爲:sense0.9不支持elasticsearch6.x,6.x加了content_type聲明。所以有人直接修改sense插件的ajax請求,sense.crx可被解壓爲文件夾,修改後將文件夾直接拖放到chrome插件發佈中。瀏覽器
而後再訪問就OK了。
因沒有加入任何數據,所以檢索出的結果爲0。
elasticSearch 採用lucene做爲索詞工具,所以也會有索引和文檔管理的概念。
文檔管理crud介紹: create, retrieve, update, delete
create結構:[PUT|POST] http://ip:9200/<index>/<type>/[<id>]
elastic中的索引 index 如同數據庫庫名同樣。
注意點:
1. 其中 id可選填,不填系統默認會給分配id且不填時必須用post提交
2. index爲索引必須小寫,一種索引只能歸屬於一種類型。一個索引下能夠存在多筆數據,不過一個索引下的數據越多搜索消耗時間就越長。
3.type爲類型,一種類型可對應多個索引。類型是一種空間區分。不過type在elastic6.0後已經不推薦使用deprecated.
以例子中的創建movie(小王子)爲例:
create:
POST http://192.168.201.105:9200/thelittleprince/movie/ { "title":"The Little Prince", "year" : "2012-10", "description": " a little pure prince" }
執行結果 result created:
{ "_index": "thelittleprince", "_type": "movie", "_id": "jDzZE2UB4ZisejttH3B6", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
update:
update和create方式同樣,不一樣點在於update要加id。內容json部分可隨意修改。
POST http://192.168.201.105:9200/thelittleprince/movie/jDzZE2UB4ZisejttH3B6 { "title":"The Little Prince", "year" : "2012-10", "description": " a little pure prince", "country" : "franch" }
執行結果 result:updated:
{ "_index": "thelittleprince", "_type": "movie", "_id": "jDzZE2UB4ZisejttH3B6", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
retrieve獲取數據:
將post變爲get請求便可。必須包含ID
GET http://192.168.201.105:9200/thelittleprince/movie/jDzZE2UB4ZisejttH3B6
執行結果 found:true:則找到數據,內容爲_source中的json對象。
{ "_index": "thelittleprince", "_type": "movie", "_id": "jDzZE2UB4ZisejttH3B6", "_version": 2, "found": true, "_source": { "title": "The Little Prince", "year": "2012-10", "description": " a little pure prince", "country": "franch" } }
delete 刪除文檔:
DELETE http://192.168.201.105:9200/thelittleprince/movie/jDzZE2UB4ZisejttH3B6
執行結果 result: deleted:
{ "_index": "thelittleprince", "_type": "movie", "_id": "jDzZE2UB4ZisejttH3B6", "_version": 3, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 1 }
刪除後再次查詢就會發現found爲false.
若是隻是以上這些,那elasticSearch就沒有什麼用,咱們要的是搜索引擎式的海量模糊檢索。
搜索,搜索前咱們先插入幾筆與電影有關的數據。此數據來自於易佰教程。
POST http://192.168.201.105:9200/movie/crime { "title": "The Godfather", "director": "Francis Ford Coppola", "year": 1972, "genres": ["Crime", "Drama"] } POST http://192.168.201.105:9200/movie/crime { "title": "Lawrence of Arabia", "director": "David Lean", "year": 1962, "genres": ["Adventure", "Biography", "Drama"] } POST http://192.168.201.105:9200/movie/crime { "title": "To Kill a Mockingbird", "director": "Robert Mulligan", "year": 1962, "genres": ["Crime", "Drama", "Mystery"] } POST http://192.168.201.105:9200/movies/drama { "title": "Apocalypse Now", "director": "Francis Ford Coppola", "year": 1979, "genres": ["Drama", "War"] } POST http://192.168.201.105:9200/movies/drama { "title": "Kill Bill: Vol. 1", "director": "Quentin Tarantino", "year": 2003, "genres": ["Action", "Crime", "Thriller"] } POST http://192.168.201.105:9200/movies/drama { "title": "The Assassination of Jesse James by the Coward Robert Ford", "director": "Andrew Dominik", "year": 2007, "genres": ["Biography", "Crime", "Drama"] }
_search 第一種經過url檢索:
http://localhost:9200/<index>/<type>/_search
post http://192.168.201.105:9200/_search post http://192.168.201.105:9200/movie/_search post http://192.168.201.105:9200/movie/crime/_search
注意分類必定要添加索引才能夠查詢,若是索引不正確會致使直接報錯error,而非查無結果。
正文檢索 -DSL query:
簡單字符串查詢 query_string
POST http://192.168.201.105:9200/_search {"query": {"query_string": { "query": "Kill" }}}
執行結果:會查到與Kill有關的全部的信息。
增長fields能夠縮小查詢的範圍,約束查詢指定的屬性。
POST http://192.168.201.105:9200/_search {"query": {"query_string": { "query": "Bill", "fields": [ "title" ] }}}
此時也就約束僅僅查詢屬性爲title 的內容。
增長過濾條件 filter:
在elasticsearch5.0廢棄:
"filtered": { "query": {}, "filter": {} } no [query] registered for [filtered]
改成:
POST http://192.168.201.105:9200/_search {"query": { "multi_match": { "query": "kill", "fields": ["title"] } }}
具體查詢詳見ElasticSearch的DSL說明。
_bulk 批量導入:
咱們若是但願批量向elasticsearch中導入數據:
post _bulk {"index":{"_index":"myindex","_type":"fulltext","_id":"123456"}} {"content":"你好China6","title1":"你好World6"} {"index":{"_index":"myindex","_type":"fulltext","_id":"123457"}} {"content":"你好China7","title1":"你好World7"}
多索引查詢:
http://ip:port/_index1,_index2,.../_search
POST myindex,indextest/_search { "query": { "term": { "content": { "value": "中國" } } } }
自動建立索引關閉 elasticsearch.yml:
action.auto_create_index:false index.mapper.dynamic:false
限制只容許(+)和不容許(-)以什麼開頭建立的索引:
action.auto_create_index:+acc*,-bank*
建立索引帶有設置5個分片3個複製品:
PUT myindex2 { "settings" : { "index" : { "number_of_shards" : 5, "number_of_replicas" : 3 } } }
----------------
>get myindex3 //測試索引是否存在
>POST myindex2/_close //關閉索引
>POST myindex2/_open //開啓索引
>GET myindex2/_settings //查看索引設置
>POST myindex2/_analyze // 字詞分解查看
POST myindex2/_analyze { "analyzer" : "standard", "text" : "I am a worker" }
myindex2/_aliases?pretty=true>
建立別名,可將多個索引使用一個別名綁定
PUT myindex2/_aliases?pretty=true { "actions": [ { "add": {"index": "myindex2", "alias": "indx"} } ] }
>get myindex2/_stats //查看索引下的一些狀態信息
>POST _template/[temp_id] //建立索引模板,頗有用,這樣相似的索引就不須要單獨設置mapping和setting了。
建立索引模板:
index_patterns 選擇index匹配規則,此處以ik開頭或以ik結尾的index都使用此template。
type1 爲設置默認類型,_source 中enabled表示source是否可見。
這樣在建立index後會默認使用template的設置。
POST _template/temp_ik { "index_patterns": ["ik_*", "*_ik"], "settings": { "number_of_shards": 2 }, "mappings": { "type1": { "_source": { "enabled": false }, "properties": { "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "name":{ "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "content":{ "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "create_date": { "type": "date", "format": "EEE MMM dd HH:mm:ss Z YYYY" } } } } }
>delete _template/[temp_id]
>delete myindex2 //刪除索引
>get [index]/_flush //刷新清除數據,將緩存內存中的index數據存入到存儲中
>get [index]/_refresh //刷新
elasticsearch.yml
主要模塊說明:
參照 https://www.yiibai.com/elasticsearch/elasticsearch_modules.html