elasticsearch初探

來源:互聯網
上載者:User

標籤:http   ar   io   os   使用   sp   java   on   資料   

https://github.com/richardwilly98/elasticsearch-river-mongodb

https://github.com/mallocator/Elasticsearch-MySQL-River

 https://github.com/BioMedCentralLtd/spring-data-elasticsearch-sample-application/blob/master/src/test/resources/springContext-book-test.xml 執行個體

http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/search.html

http://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#repositories.create-instances.spring

為了替換現在使用體驗比較差的SegmentFault搜尋,我開始了前期搜尋引擎的探索。目前首選是elasticsearch

elasticsearch需要java環境
安裝java

sudo aptitude install openjdk-7-jre

下載elasticsearch
http://www.elasticsearch.org/overview/elkdownloads/
https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.1.0.deb

安裝
因為我的環境是ubuntu,所以直接用它的deb包。

sudo dpkg -i elasticsearch-1.1.0.deb

啟動

sudo /etc/init.d/elasticsearch start

jdbc river
用於定期或者即時匯入需要搜尋的資料
我們資料庫是mysql,所以用的官方elasticsearch-river-jdbc
https://github.com/jprante/elasticsearch-river-jdbc

river jdbc quickstart
https://github.com/jprante/elasticsearch-river-jdbc/wiki/Quickstart

安裝

cd /usr/share/elasticsearchsudo /bin/plugin --install river-jdbc --url http://bit.ly/1jyXrR9

如果安裝失敗,可以手動下載後再安裝。

sudo /bin/plugin --install river-jdbc --url file:///tmp/elasticsearch-river-jdbc-1.0.0.1.zip

建立一個JDBC river

curl -XPUT ‘localhost:9200/_river/my_jdbc_river/_meta‘ -d ‘{    "type" : "jdbc",    "jdbc" : {        "url" : "jdbc:mysql://localhost:3306/test",        "user" : "root",        "password" : "",        "sql" : "select * from question",        "index" : "question", "type" : "question" }}‘

測試匯入效果:

curl -XGET ‘localhost:9200/question/_search?pretty&q=*‘

or

localhost:9200/question/_search?pretty&q=*

官方有中文分詞支援,但是不是非常準確,這裡使用medcl的ik分詞
安裝elasticsearch-analysis-ik

cd /tmpwget https://github.com/medcl/elasticsearch-analysis-ik/archive/master.zipunzip master.zipcd elasticsearch-analysis-ik/

這裡需要用mvn package命令打包成elasticsearch-analysis-ik-1.2.6.jar

mvn package

沒有maven的可以安裝一下

sudo aptitude install maven

複製elasticsearch-analysis-ik-1.2.6.jar到ES_HOME/plugins/analysis-ik下

sudo cp elasticsearch-analysis-ik-1.2.6.jar /usr/share/elasticsearch/plugins/analysis-ik

將ik的配置和字典都複製到ES_HOME/config下

sudo cp -R ik /etc/elasticsearch

elasticsearch配置啟用ik

sudo vim /etc/elasticsearch

底部增加一行

index.analysis.analyzer.ik.type : ‘ik‘

重啟服務載入配置

sudo service elasticsearch restart

測試分詞效果

localhost:9200/question/_analyze?analyzer=ik&pretty=true&text=杭州堆棧科技有限公司

返回

{  "tokens" : [ {    "token" : "杭州", "start_offset" : 0, "end_offset" : 2, "type" : "CN_WORD", "position" : 1 }, { "token" : "堆棧", "start_offset" : 2, "end_offset" : 4, "type" : "CN_WORD", "position" : 2 }, { "token" : "科技", "start_offset" : 4, "end_offset" : 6, "type" : "CN_WORD", "position" : 3 }, { "token" : "有限公司", "start_offset" : 6, "end_offset" : 10, "type" : "CN_WORD", "position" : 4 } ]}

測試官方的php用戶端
官方的php用戶端通過composer安裝
先安裝composer

curl -s http://getcomposer.org/installer | phpsudo mv composer.phar /usr/bin/composer

產生一個composer.json,寫入

{    "require": {        "elasticsearch/elasticsearch": "~1.0" }}

開始安裝

composer install --no-dev

項目中require之後就可以使用了

<?phprequire ‘vendor/autoload.php‘;$client = new Elasticsearch\Client();

使用中文分詞ik的mapping

$params[‘index‘] = ‘question‘;$params[‘type‘] = ‘question‘; $myTypeMapping = array( ‘_source‘ => array( ‘enabled‘ => true ), ‘_all‘ => array( ‘indexAnalyzer‘ => ‘ik‘, ‘searchAnalyzer‘ => ‘ik‘, ‘term_vector‘ => ‘no‘, ‘store‘ => ‘false‘ ), ‘properties‘ => array( ‘text‘ => array( ‘type‘ => ‘string‘, ‘term_vector‘ => ‘with_positions_offsets‘, ‘indexAnalyzer‘ => ‘ik‘, ‘searchAnalyzer‘ => ‘ik‘, ‘include_in_all‘ => ‘true‘, ‘boost‘ => 8 ), ‘title‘ => array( ‘type‘ => ‘string‘, ‘term_vector‘ => ‘with_positions_offsets‘, ‘indexAnalyzer‘ => ‘ik‘, ‘searchAnalyzer‘ => ‘ik‘, ‘include_in_all‘ => ‘true‘, ‘boost‘ => 8 ) ) );$params[‘body‘][‘question‘] = $myTypeMapping;$response = $client->indices()->putMapping($params);

測試一下效果,搜尋問題內容‘php架構’

$searchParams = array();$searchParams[‘index‘] = ‘question‘;$searchParams[‘type‘] = ‘question‘;$searchParams[‘body‘][‘query‘][‘match‘][‘text‘] = ‘php架構‘;

返回,取了幾條

  1. segmentfault是用什麼php架構寫的啊? http://segmentfault.com/q/1010000000095152
  2. 剛學PHP,求介紹THINKPHP架構的優劣 http://segmentfault.com/q/1010000000129535
  3. symfony是不是比其他的php架構功能強大很多?
    http://segmentfault.com/q/1010000000095952
  4. python 架構繁多,如何整體把握? http://segmentfault.com/q/1010000000186319
  5. 什麼是ORM,以及在php上的使用? http://segmentfault.com/q/1010000000318125
  6. codeigniter架構php依賴安裝問題 http://segmentfault.com/q/1010000000253966

總結
elasticsearch安裝和使用還是非常簡單的,從沒有最佳化的返回結果來看也比現有的搜尋要理想。
不過唯一的缺點就是文檔相比solr還是太少,很多都只給了最基本的例子。
最佳化 組合搜尋等等,都要自己琢磨和尋找。

elasticsearch初探

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.