1.到http://www.elasticsearch.org/download/下载最新版的elasticsearch运行包
bin是运行的脚本,config是设置文件,lib是放依赖的包
2.到目录下启动 bin/elasticsearch
http://ip:9200/,查看页面信息,是否正常启动。status=200表示正常启动了,还有一些es的版本信息,name为配置文件中node.name的值。
3.在另外一台机器上,安装同样的步骤安装ES,因为至少2台服务才算集群嘛!注意,在配置时,将node.name的值设置为test-node2,总之必须和之前配置值不同。
两台es服务同时起来,因为配置文件中均默认cluster.name=elasticsearch,所以这两台机器自动构建成一个集群,集群名字为elasticsearch。
4.有关插件安装详细请见
http://my.oschina.net/xiaohui249/blog/228748
创建索引参考
http://www.oschina.net/translate/elasticsearch-getting-started
非常全
http://www.cnblogs.com/jefurry/p/3469548.html
python中:
http://www.qwolf.com/?p=1387#安装
python 导入数据
http://www.vpsee.com/2014/05/install-and-play-with-elasticsearch/
注:
No module named elasticsearch ——》sudo easy_install-2.7 elasticsearch
API
http://pyes.readthedocs.org/en/latest/guide/reference/query-dsl/boosting-query.html
创建:(参考http://834945712.iteye.com/blog/1915432)
import pyes
conn = pyes.ES('127.0.0.1:9200')
conn.create_index("human") #human 是一个新的索引库,相当于create database操作
mapping = {u'firstname': {'index': 'analyzed', #使用分词器
'type': u'string',
'analyzer':'ik'}, #分词器为ik
u'lastname': {'index': 'not_analyzed',
'type': u'string'},
u'age': {'index': 'not_analyzed', #不使用分词器
'type': u'long'}} #mapping 是字段,相当于数据库的表的列名
conn.put_mapping("man", {'properties':mapping}, ["human"]) #在human库中创建man,相当于create table操作
conn.put_mapping("woman", {'properties':mapping}, ["human"]) #woman同样相当于一张表
conn.index({'firstname':'David', 'lastname':'White', 'age':18}, 'human', 'man', 'David White', True) #向human的man中添加索引数据,相当于insert into操作
conn.index({'firstname':'Suzan', 'lastname':'Black', 'age':28}, 'human', 'woman', 'Suzan Black', True) #向human的woman中添加索引数据
conn.index({'firstname':'Uni', 'lastname':'Lavender', 'age':18}, 'human', 'man', 'Uni Lavender', True)
conn.index({'firstname':'Jann', 'lastname':'White', 'age':18}, 'human', 'woman', 'Jann White', True)
conn.index({'firstname':'Suzan', 'lastname':'White', 'age':18}, 'human', 'woman', 'Suzan White', True) #注意第四个参数是index的id,具有唯一性,因此更新数据,可以按照id使用index即可conn.index({'firstname':'Jann', 'lastname':'White', 'age':28}, 'human', 'woman', 'Jann White', True) #将年龄由18更新到28
3. 测试使用pyes官方文档的API的查询
使用res = conn.search(pyes.BoolQuery(must=must), 'human', 'woman', start=0, size=10, sort='age')查询,支持分页
a. 查找firstname为Suzan的女人的index数据
条件:must = pyes.StringQuery('Suzan', ['firstname',]) #must = pyes.StringQuery('Suzan', 'firstname')
相当于sql查询 select * from human.woman where firstname regexp '[^a-zA-Z]Suzan[^a-zA-Z]'
b. 查找lastname为white的女人的index数据
条件:must = pyes.StringQuery('White', ['lastname',]) #must = pyes.StringQuery('White', ['lastname',])或者must = pyes.TermQuery('lastname', 'White')
相当于sql查询 select * from human.woman where lastname = 'White'
c. 查找age为18,20,28的女人的index数据
条件:must = pyes.TermsQuery('age', [18,28])
相当于sql查询 select * from human.woman where age=18 or age = 28
d. 查找age为18,28并且firstname为Suzan的女人的index数据
条件:must = [pyes.TermsQuery('age', [18,28]), pyes.StringQuery('Suzan', 'firstname')]
相当于sql查询 select * from human.woman where (age=18 or age = 28) and firstname = 'Suzan'
e. 查找firstname或者lastname中出现Rich单词的女人的index数据
条件:must = pyes.StringQuery('Suzan', ['firstname',‘lastname’], default_operator='or')
相当于sql查询 select * from human.woman where firstname regexp '[^a-zA-Z]Suzan[^a-zA-Z]' or lastname regexp '[^a-zA-Z]Suzan[^a-zA-Z]'
f. 查找firstname并且lastname中出现Rich单词的女人的index数据
条件:must = pyes.StringQuery('Suzan', ['firstname',‘lastname’], default_operator='and')
相当于sql查询 select * from human.woman where firstname regexp '[^a-zA-Z]Suzan[^a-zA-Z]' and lastname regexp '[^a-zA-Z]Suzan[^a-zA-Z]'
g. 查找年龄在18到28之间的女人的index数据
条件:must = pyes.RangeQuery(pyes.ESRange('age', from_value=18, to_value=28))
相当于sql查询 select * from human.woman where age between 18 and 28]
h. 查找以Whi开头的lastname的女人的index数据
条件:must = pyes.PrefixQuery('lastname', 'Whi')
相当于sql查询 select * from human.woman where lastname like 'Whi%'
查询:
''' #coding=utf-8 import os import sys import pyes from pyes import * conn = pyes.ES(['127.0.0.1:9200'])#连接es conn.index({"aname":"test2", "apwd":"123456"}, "mydatabase", "account") s = MatchAllQuery() results=conn.search(query=Search(s), indexes=["mydatabase"], sort='aname:desc') for r in results: print "name:%s,pwd:%s"%(r['aname'],r['apwd']) ''' #coding=utf-8 import os import sys import pyes from pyes import * conn = pyes.ES(['127.0.0.1:9200'])#连接es #conn.index({"aname":"test2", "apwd":"123456"}, "mydatabase", "account") s = TermsQuery('aname', ["yin","test123"]) results=conn.search(query=Search(s), indexes=["mydatabase"], sort='aname:desc') for r in results: print "name:%s,pwd:%s"%(r['aname'])