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

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

背景:

为什么要索引?

举个例子,给你一本字典要查出来“你”这个字,大家一般都会去目录页的n部分去找到ni这个拼音在哪一页,然后去打开那一页。以上的目录就是一个索引,通过索引可以很快速的找到你想要找的内容。

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

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

Getting Start with ElasticSearch

  1. ElasticSearch是一个开源的分布式搜索引擎,具备高可靠性,支持非常多的企业级搜索用例。像Solr4一样,是基于Lucene构建的。支持时间时间索引和全文检索。官网:http://www.elasticsearch.org
  2. 它对外提供一系列基于javahttpapi用于索引、检索、修改大多数配置。
  3. 运行命令行,进入  elasticsearch-1.0.0\bin目录。
  4. 这时运行 elasticsearch.bat
  5. 浏览器里测试一下:http://localhost:9200
Plugins for ElasticSearch
(需要的插件)
 
分布式搜索elasticsearch集群管理工具head
插件 安装方法:
  1. elasticsearch/binplugin-installmobz/elasticsearch-head
  2. 运行es
  3. 打开http://localhost:9200/_plugin/head/
MongoDB River Plugin (作者 Richard Louapre)
  1. https://github.com/richardwilly98/elasticsearch-river-mongodb 
  2. mongodb同步插件,mongodb必须搭成副本集的模式,因为这个插件的原理是通过定期读取mongodb中的oplog来同步数据


How to work
1. 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

2.Mapping
 
然后是自己的mapping.这里只mapping了name,在创建river之后如果要索引的数据里面有除了name其他字段也会自动的刷出mapping来
{
    "test_mapping": {
        "properties": {
            "name": {
                "type": "string",
                "analyzer": "str_index_analyzer"
            }
        }
    }
}
 

'


3.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"
    }
}

 
4.Search
QueryDSL:

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

Bool Query
{
    "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
" 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
completion suggester      is a so-called prefix suggester
mapping "suggest": { "type": "completion", " index_analyzer ":"simple",                               " search_analyzer ":"simple", "payloads": false}
" shortNames_suggest ":{
                               "text": name,
                              "completion": {
                                 "field": " shortNames.suggest "
                               }
                           }

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值