全文檢索實踐【PHP篇】

本文寫的較淺顯,僅供你們交流,轉載須註明地址,我的博客php

一套全文檢索解決方案,涉及到的技術有elasticsearch、mongodb、php、monolog等。java

  1. PHP程序添加文章寫入Mongodb中。python

  2. 經過mongodb-connector同步Mongodb數據到elasticsearch中。linux

  3. PHP程序(elasticsearch-php)全文檢索elasticsearch。git

Elasticsearch準備

1. 安裝新版 java環境
2. 下載Elasticsearch
3. 解壓壓縮包
$ unzip elasticsearch-{version}.zip
4. 運行elasticsearch
$ ./elasticsearch-{version}/bin/elasticsearch
5. 測試運行狀況 守護進程模式,加-d
$ curl 127.0.0.1:9200/?pretty

Notice - 正常能夠看到如下返回信息:github

{
        "name" : "Martinex",
        "cluster_name" : "elasticsearch",
        "version" : {
                "number" : "2.2.0",
                "build_hash" : "8ff36d139e16f8720f2947ef62c8167a888992fe",
                "build_timestamp" : "2016-01-27T13:32:39Z",
                "build_snapshot" : false,
                "lucene_version" : "5.4.1"
            },
          "tagline" : "You Know, for Search"
    }

MongoDB準備

1. 下載MongoDB,也能夠採用如下方式進行安裝(linux下)
$ brew install mongodb
2. 解壓壓縮包到某個自定義目錄下
3. 進入解壓目錄,新建data目錄,而後在data目錄下新建db目錄,用來存放mongodb數據
4. 進入bin目錄下,啓動mongod,切記命令爲
$ ./mongod

首次需配置MongoDB數據存放位置mongodb

$ sudo ./mongod --dbpath /Users/wlei24/es/mongodb-osx-x86_64-3.0.0/data/db/

後面運行時可能出現segmentfault

ERROR:dbpath (/data/db) does not exist.

這是因爲mongod啓動時沒有找到mongodb.conf致使的,所以咱們的啓動mongodb的時候手動添加 --dbpath便可app

5. 測試MongoDB運行狀況

進入bin目錄,運行 ./mongo 進入mongodb控制檯,輸入curl

$ show dbs

顯示結果:

article  0.078GB
    local    0.328GB

一樣你能夠經過 127.0.0.1:27017 訪問,頁面顯示:

It looks like you are trying to access MongoDB over HTTP on the native driver port.

Mongo-Connector準備

1. 下載mongo-connector

首先須要確保你已經安裝pip,不然執行如下命令

$ easy_install pip

若已安裝,執行如下命令

pip install mongo-connector

一樣你也能夠這樣安裝 - 下載完成後執行sudo python setup.py install

git clone https://github.com/10gen-labs/mongo-connector.git
    cd mongo-connector
    python setup.py install
2. 確保開啓MongoDB複製集
mongod --replSet myDevReplSet

接着在mongodb控制檯執行 rs.initiate()

3. 運行 mongodb-connector
mongo-connector -m 127.0.0.1:27017 -t 127.0.0.1:9200 -d elastic_doc_manager

你會驚奇發現報了一大堆的錯誤

No handlers could be found for logger "mongo_connector.util"
    Traceback (most recent call last):
      File "/usr/local/bin/mongo-connector", line 9, in <module>
        load_entry_point('mongo-connector==2.3', 'console_scripts', 'mongo-connector')()
      File "/Library/Python/2.7/site-packages/mongo_connector-2.3-py2.7.egg/mongo_connector/util.py", line 85, in wrapped
        func(*args, **kwargs)
      File "/Library/Python/2.7/site-packages/mongo_connector-2.3-py2.7.egg/mongo_connector/connector.py", line 1041, in main
        conf.parse_args()
      File "/Library/Python/2.7/site-packages/mongo_connector-2.3-py2.7.egg/mongo_connector/config.py", line 118, in parse_args
        option, dict((k, values.get(k)) for k in option.cli_names))
      File "/Library/Python/2.7/site-packages/mongo_connector-2.3-py2.7.egg/mongo_connector/connector.py", line 824, in apply_doc_managers
        module = import_dm_by_name(dm['docManager'])
      File "/Library/Python/2.7/site-packages/mongo_connector-2.3-py2.7.egg/mongo_connector/connector.py", line 814, in import_dm_by_name
        "vailable doc managers." % full_name)
    mongo_connector.errors.InvalidConfiguration: Could not import mongo_connector.doc_managers.elastic_doc_manager. It could be that this doc manager has been moved out of this project and is maintained 
        else where. Make sure that you have the doc manager installed alongside mongo-connector. Check the README for a list of available doc managers.

花了大半天沒有解決問題,怪本身沒仔細看錯誤輸出,偌大的錯誤提示-沒有找到elastic_doc_manager
不過感受mongodb-connector也有點坑,默認doc_managers裏面只有solr_doc_manageir

這時就須要你去elastic2-doc-manager

將elastic2-doc-manager.py拷貝到本地doc_manaers目錄

執行以前命令,發現繼續報錯~

IOError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/mongo_connector-2.3-py2.7.egg/mongo_connector/doc_managers/mongo-connector.log'

這個錯誤只須要根據報錯信息,新建此文件,並賦予讀寫權限便可。

繼續執行以前命令,驚奇發現已經顯示正常跡象,不過隨即退出。

解決此問題只需採用在命令前面加上sudo便可(意思你懂得~)

Logging to mongo-connector.log.

實際效果

添加文章效果圖
Mongodb存儲文章形式
Elasticsearch存儲文章形式
搜索文章標題效果
搜索文章內容效果

參考資料:Elasticsearch權威指南mongo-connectorMongoDB數據自動同步到Elasticsearch

Github地址,請手動start,感謝~

相關文章
相關標籤/搜索