ElasticSearch 入門筆記1


1. 起步

1. 建demo工程,看文檔,做典型demo

2. 資源列表:

http://es.xiaoleilu.com/010_Intro/10_Installing_ES.html

3. 啟動:

elasticsearch.bat

啟動完成后

用ARC工具訪問 http://localhost:9200/

2016-09-24 15-12-52_Advanced REST client

這說明你的ELasticsearch集群已經啟動並且正常運行,接下來我們可以開始各種實驗了。

 

https://imququ.com/post/elasticsearch.html 用ElasticSearch進行全文搜索示例

2. 安裝分詞器

  編譯IK分詞器

E:\002.tools\apache-maven-3.0.4\bin\mvn.bat package

編譯過程中遇到 早先配置的oschina鏡像停止了

用 <url>http://repo2.maven.org/maven2/</url>

修改 C:\Users\chen.simon\.m2 settings.xml配置

 

如果一切順利,在 target/releases/ 目錄下可以找到編好的文件。將其解壓並拷到 ~/es_root 對應目錄:

mkdir -p ~/es_root/plugins/ik/ unzip target/releases/elasticsearch-analysis-ik-1.9.0.zip -d ~/es_root/plugins/ik/ 

elasticsearch-analysis-ik 的配置文件在 ~/es_root/plugins/ik/config/ik/ 目錄,很多都是詞表,直接用文本編輯器打開就可以修改,改完記得保存為 utf-8 格式。

現在再啟動 Elasticsearch 服務,如果看到類似下面這樣的信息,說明 IK Analysis 插件已經裝好了:

 

 

java.lang.IllegalArgumentException: Plugin [analysis-ik] is incompatible with Elasticsearch [2.4.0]. Was designed for version [2.3.0]

暈  這個版本不兼容elasticsearch2.4.0

去github重新找

https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v1.10.0/elasticsearch-analysis-ik-1.10.0.zip

這個是匹配的  並且這個是編譯好的 無需mvn package  直接解開用

將他source包拿下來 以便以后使用

 

增加java啟動參數 以便調試

在elasticsearch.bat中 "%JAVA_HOME%\bin\java開頭的這一行 %JAVA_OPTS%后面 -Xdebug -Xrunjdwp:transport=dt_socket,address=8500,server=y,suspend=y

 

elasticsearch所在路徑 不能有含有空格 否則會出現詭異的權限問題(空格轉成了%20了)

2016-09-24 17-31-51_解決:java.security.AccessControlException_ Access Denied Error - changyanmanman的專欄

 

配置同義詞

Elasticsearch 自帶一個名為 synonym 的同義詞 filter。為了能讓 IK 和 synonym 同時工作,我們需要定義新的 analyzer,用 IK 做 tokenizer,synonym 做 filter。聽上去很復雜,實際上要做的只是加一段配置。

打開 ~/es_root/config/elasticsearch.yml 文件,加入以下配置:

index:
  analysis:
    analyzer:
      ik_syno:
          type: custom
          tokenizer: ik_max_word
          filter: [my_synonym_filter]
      ik_syno_smart:
          type: custom
          tokenizer: ik_smart
          filter: [my_synonym_filter]
    filter:
      my_synonym_filter:
          type: synonym
          synonyms_path: analysis/synonym.txt

以上配置定義了 ik_syno 和 ik_syno_smart 這兩個新的 analyzer,分別對應 IK 的 ik_max_word 和 ik_smart 兩種分詞策略。根據 IK 的文檔,二者區別如下:

ik_max_word:會將文本做最細粒度的拆分,例如「中華人民共和國國歌」會被拆分為「中華人民共和國、中華人民、中華、華人、人民共和國、人民、人、民、共和國、共和、和、國國、國歌」,會窮盡各種可能的組合;
ik_smart:會將文本做最粗粒度的拆分,例如「中華人民共和國國歌」會被拆分為「中華人民共和國、國歌」;

ik_syno 和 ik_syno_smart 都會使用 synonym filter 實現同義詞轉換。為了方便后續測試,建議創建~/es_root/config/analysis/synonym.txt 文件,輸入一些同義詞並存為 utf-8 格式。例如:

ua,user-agent,userAgent
js,javascript
谷歌=>google
 
----
准備測試
 

3. 基本概念

 

2016-09-24 18-22-38_使用 Elasticsearch 實現博客站內搜索 _ JerryQu 的小站

 

4. 創建index

2016-09-24 18-26-05_Advanced REST client

響應:

2016-09-24 18-26-13_Advanced REST client

這就創建了一個simon的index  相當於mysql的database    再次發送這個請求 服務器就回400 bad request

注意此時http method 為put

如果使用get的http method 便能查詢到此index的信息:

2016-09-24 18-28-00_

查詢index

http://localhost:9200/_cat/indices?v

GET

相關文檔:

https://www.elastic.co/guide/en/elasticsearch/reference/2.4/indices-create-index.html

5. 創建mapping

Type 不用單獨創建,在創建 Mapping 時指定就可以。Mapping 用來定義 Document 中每個字段的類型、所使用的 analyzer、是否索引等屬性,非常關鍵。創建 Mapping 的代碼示例如下:

url http://localhost:9200/simon/_mapping/article

{
    "properties": {
        "title": {
            "type": "string",
            "term_vector": "with_positions_offsets",
            "analyzer": "ik_syno",
            "search_analyzer": "ik_syno"
        },
        "content": {
            "type": "string",
            "term_vector": "with_positions_offsets",
            "analyzer": "ik_syno",
            "search_analyzer": "ik_syno"
        },
        "slug": {
            "type": "string"
        },
        "tags": {
            "type": "string",
            "index" : "not_analyzed"
        },
        "update_date": {
            "type" : "date",
            "index" : "not_analyzed"
        }
            
    }
}

注意: 如果報 elasticseatch analyzer [ik_syno]  not found for field  則需要檢查elasticsearch.yml文件中是否配置了此分析器

2016-09-24 19-09-46_Advanced REST client

響應:

2016-09-24 19-10-14_Advanced REST client

6. 加入數據

url http://localhost:9200/simon/article/1 方法:put

1表示這條數據的id

報文:

{
    "title" : "什么是 JS?",
    "slug" :"what-is-js",
    "tags" : ["JS", "JavaScript", "TEST"],
    "content" : "JS 是 JavaScript 的縮寫!",
    "update_date" : "2015-12-15T13:05:55Z"
}

 

2016-09-24 19-27-13_Advanced REST client

2016-09-24 19-27-21_Advanced REST client

如何不指定id ,自動生成

http://localhost:9200/simon/article/_search 這個可以查到加入的數據

簡單的查詢也可以  http://localhost:9200/simon/article/_search?q=js

更復雜的查詢就需要 query dsl了

 

7. 其他 

查詢所有index

http://localhost:9200/_cat/indices?v


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM