用ElasticSearch和mongodb River搭建一个简单地search工程。

来源:互联网 发布:java电商架构 编辑:程序博客网 时间:2024/05/01 03:08

近期的项目需要用到分布式elasticSearch来做search.自己研究了一下。在这里做个备注.

背景:

为什么要索引。

   举个例子,给你一本字典要查出来“你”这个字,大家一般都会去目录页的n部分去找到ni这个拼音在哪一页,然后去打开那一页。

以上的目录就是一个索引,通过索引可以很快速的找到你想要找的内容。


在软件开发的过程中,我们也会需要很快速的查找某些内容的场景。刚入软件行业的童鞋肯定会觉得,用sql的 like 也可以完成,但是如果数据量过多,你整个数据库会被这个like给“like坏了”。

于是就开始说这个搜索引擎的问题啦。因为我直接从es开始的,就直接说es啦。

Getting Start with ElasticSearch

ElasticSearch是一个开源的分布式搜索引擎,具备高可靠性,支持非常多的企业级搜索用例。像Solr4一样,是基于Lucene构建的。支持时间时间索引和全文检索。官网:http://www.elasticsearch.org
它对外提供一系列基于javahttpapi用于索引、检索、修改大多数配置。
运行命令行,进入  elasticsearch-1.0.0\bin目录。
这时运行 elasticsearch.bat
浏览器里测试一下:http://localhost:9200


Plugin for ElasticSearch
(需要的插件)

•分布式搜索elasticsearch集群管理工具head

插件安装方法:
1.elasticsearch/binplugin-installmobz/elasticsearch-head
2.运行es
3.打开http://localhost:9200/_plugin/head/

•MongoDB River Plugin (作者 Richard Louapre)
https://github.com/richardwilly98/elasticsearch-river-mongodb 
mongodb同步插件,mongodb必须搭成副本集的模式,因为这个插件的原理是通过定期读取mongodb中的oplog来同步数据


How to work

create index

首先要创建个索引。

$curl -XPOST 'http://localhost:9200/test_index/' -d '{
   

  "settings": {

    "analysis": {

      "analyzer": {

        "str_index_analyzer": {

          "tokenizer":"keyword",

          "filter": [

            "lowercase",

            "ngram"

          ]

        }

      },

      "filter": {

        "ngram": {

          "type": "ngram",

          "min_gram": 2,

          "max_gram": 1000

        }

      }

    }

  }

}’


put:http://localhost:9200/test_index

Mapping

然后是自己的mapping.这里只mapping了name,在创建river之后如果要索引的数据里面有除了name其他字段也会自动的刷出mapping来

 $curl -XPOST 'http://localhost:9200/test_index/test_mapping/_mapping' -d '{


        "test_mapping": {        

            "properties": {

                 "name": {

                    "type":"string",

                    "analyzer":"str_index_analyzer"

                }

        }

    }

}'





River
put: http://localhost:9200/_river/test_index/_meta
{
    "type":"mongodb",
    "mongodb":{
    "host":"127.0.0.1",
        "port":"27017",
        "db":"test",
        "collection":"test"
},
    "index":{
    "name":"test_index",
        "type":"test_mapping"
}
}
Search
•QueryDSL:

        Term:          {    "query" :{ "term" : { "user" : "kimchy"}  } }

BoolQuery
{"query":{
    "bool": {
        "must": {"term": {"user": "kimchy"}},
        "must_not": {"range":{"age": { "from": 10, "to": 20 }}},
        "should": [
            {"term": {"tag": "wow" }},
            {"term": {"tag": "elasticsearch"}}
        ]}}}
•FilterQuery:
{
    "query": {
        "filtered": {
            "filter": {
                "range":{"age": { "from": 10, "to": 20 }}
            }}}}
•Geosearch
http://www.elasticsearch.org/blog/geo-location-and-search/
 addresses" :{"location" : {  "lat" : 40.12,  "lon": -71.34  }
(mapping)"addresses":{"properties": {"location": {"type": "geo_point"}}}
"filter": {
            "
geo_distance": {
                "distance" :"12km",
                "
pin.location": {
                    "
lat": 40,
                    "
lon": -70
                }
            }
        }
Suggest
completionsuggester     is a so-called prefixsuggester
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
mapping"suggest": { "type": "completion", "index_analyzer":"simple",                               "search_analyzer":"simple", "payloads": false}
"shortNames_suggest":{
                               "text": name,
                               "completion": {
                                   "field": "shortNames.suggest"
                                }
                            }




0 0