Implement Chinese search with MongoDB + elasticsearch

Source: Internet
Author: User

While Elasticsearch can support full-text retrieval in a variety of languages, we don't want to switch to Elasticsearch as the backend database for the time being.

Of course, when you can store data in a Web application, write a copy of it to Elasticsearch, but it certainly pollutes the original business logic.

In the IT industry, as long as there is demand, there must already be a lot of good or bad use of open source wheels.

Fortunately, there are some conversion scenarios that can automatically import data from MongoDB into Elasticsearch, allowing Elasticsearch to provide intelligent Chinese retrieval.

The conversion scheme mainly includes river and Mongo-connector. The former is no longer maintained, while the latter is supported by the official MONGO, so it is chosen as a conversion scheme.

Mongo-connector

Written in Python, it's easy to install and use.

Pip install mongo-connectormongo-connector--auto-commit-interval=0 -M mongo:27017-t Elasticsearch:9200-d Elastic_doc_manager

Auto-commit-interval is a commit delay, 0 means instant write, and-m and-T specify MONGO and Elasticsearch addresses respectively.

It is important to note that the MONGO needs to be configured as cluster mode.

Configuring the Environment

Users can configure their own MONGO and elasticsearch environments and connect them via Mongo-connector.

The Docker-compose template is already available here, and you can use the Docker container to create an environment that includes MONGO, Mongo-connector, Elasticserach, and so on. Docker and docker-compose environments need to be installed in advance.

git clone https://github.com/yeasy/docker-compose-files.gitmongo-elasticsearch; Docker-compose up

docker inspect container_id |grep AddrYou can view the IP address of the container by.

Test

Writing data in the MONGO container

> UseTest> Db.col.insert({Name"Wang Xiaoming", Birth:"1980-01-01"})> Db.col.insert({Name"Wang Dongdong", Birth:"1981-01-01"})> Db.col.insert({Name"Zhang Limin", Birth:"1982-01-01"})

Retrieving by Elasticsearch Container

curl-xget-h ' charset=utf-8 ' elasticsearch:9200/test/col/_search-d ' {" query ": {" Match ": {" name ":" King "}} ' {" Took ":" Timed_out ": false," _shards ": {" Total ": 5," successful ": 5," failed ": 0}," hits ": {" Total ": 2," "Max_score": 0.5, "hits": [{"_index": "Test", "_type": "Col", "_id": "55d6d4640e247e587cfc73ca", "_score": 0.5, "_source": {"name": "\u738b\u4e1c\u4e1c", "Birth": "1981-01-01"}},{"_index": "Test", "_type": "Col", "_id": " 55d6d45d0e247e587cfc73c9 "," _score ": 0.15342641," _source ": {" name ":" \u738b\u6653\u660e "," Birth ":" 1980-01-01 "}}]}} 

Reprint Please specify: http://blog.csdn.net/yeasy/article/details/47842437

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Implement Chinese search with MongoDB + elasticsearch

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.