近期的项目需要用到分布式elasticSearch来做search.自己研究了一下。在这里做个备注.
背景:
为什么要索引?
举个例子,给你一本字典要查出来“你”这个字,大家一般都会去目录页的n部分去找到ni这个拼音在哪一页,然后去打开那一页。以上的目录就是一个索引,通过索引可以很快速的找到你想要找的内容。
在软件开发的过程中,我们也会需要很快速的查找某些内容的场景。刚入软件行业的童鞋肯定会觉得,用sql的 like 也可以完成,但是如果数据量过多,你整个数据库会被这个like给“like慢了”。
于是就开始说这个搜索引擎的问题啦。因为我直接从es开始的,就直接说es啦。
Getting Start with ElasticSearch
- ElasticSearch是一个开源的分布式搜索引擎,具备高可靠性,支持非常多的企业级搜索用例。像Solr4一样,是基于Lucene构建的。支持时间时间索引和全文检索。官网:http://www.elasticsearch.org
- 它对外提供一系列基于java和http的api,用于索引、检索、修改大多数配置。
- 运行命令行,进入 elasticsearch-1.0.0\bin目录。
- 这时运行 elasticsearch.bat
- 浏览器里测试一下:http://localhost:9200
- elasticsearch/bin/ plugin-installmobz/elasticsearch-head
- 运行es
- 打开http://localhost:9200/_plugin/head/
- https://github.com/richardwilly98/elasticsearch-river-mongodb
- mongodb同步插件,mongodb必须搭成副本集的模式,因为这个插件的原理是通过定期读取mongodb中的oplog来同步数据。
How to work
– $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来$curl -XPOST 'http://localhost:9200/test_index/test_mapping/_mapping' -d '
{ "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" } }
Term: { "query" :{ "term" : { "user" : "kimchy"} } }
{
"query": {
"bool": {
"must": {
"term": {
"user": "kimchy"
}
},
"must_not": {
"range": {
"age": {
"from": 10,
"to": 20
}
}
},
"should": [
{
"term": {
"tag": "wow"
}
},
{
"term": {
"tag": "elasticsearch"
}
}
]
}
}
}
{
"query": {
"filtered": {
"filter": {
"range": {
"age": {
"from": 10,
"to": 20
}
}
}
}
}
}
" 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
}
}
}
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 "}}