ElasticSearch 入门(转)
最大的特点:
1. 数据库的 database, 就是 index
2. 数据库的 table, 就是 tag
3. 不要使用browser, 使用curl来进行客户端操作. 否则会出现 java heap ooxx...
curl: -X 后面跟 RESTful : GET, POST ...
-d 后面跟数据。 (d = data to send)
1. create:
指定 ID 来建立新记录。 (貌似PUT, POST都可以)
$ curl -XPOST localhost:9200/films/md/2 -d '
{ "name":"hei yi ren", "tag": "good"}'
使用自动生成的 ID 建立新纪录:
$ curl -XPOST localhost:9200/films/md -d '
{ "name":"ma da jia si jia3", "tag": "good"}'
2. 查询:
2.1 查询所有的 index, type:
$ curl localhost:9200/_search?pretty=true
2.2 查询某个index下所有的type:
$ curl localhost:9200/films/_search
2.3 查询某个index 下, 某个 type下所有的记录:
$ curl localhost:9200/films/md/_search?pretty=true
2.4 带有参数的查询:
$ curl localhost:9200/films/md/_search?q=tag:good
{"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"film","_type":"md","_id":"2","_score":1.0, "_source" :
{ "name":"hei yi ren", "tag": "good"}},{"_index":"film","_type":"md","_id":"1","_score":0.30685282, "_source" :
{ "name":"ma da jia si jia", "tag": "good"}}]}}
2.5 使用JSON参数的查询: (注意 query 和 term 关键字)
$ curl localhost:9200/film/_search -d '
{"query" : { "term": { "tag":"bad"}}}'
3. update
$ curl -XPUT localhost:9200/films/md/1 -d { ...(data)... }
4. 删除。 删除所有的:
$ curl -XDELETE localhost:9200/films
---------------------------------------------------------------------------------------------------------
角色关系对照
elasticsearch 跟 MySQL 中定义资料格式的角色关系对照表如下
MySQL elasticsearch
database index
table type
table schema mapping
row document
field field
3.索引映射
$ curl -XPUT http://localhost:9200/test-index
#创建Mapping
$ curl -XPUT http://localhost:9200/test-index/test-type/_mapping -d '{
"properties" : {
"name" : { "type" : "string" }
}
}'
def indexmapping():
"""索引映射"""
conn = ES('127.0.0.1:9200')
conn.debug_dump = True
try:
#删除索引
conn.delete_index("test-index")
except:
pass
#创建索引
conn.create_index("test-index")
mapping = {
u'id': {'store': 'yes',
'type': u'integer'},
u'author': {'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': u'string'},
u'published': {'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': u'datetime'},
u'url': {'store': 'yes',
'type': u'string'},
u'title': {'boost': 1.0,
'index': 'analyzed',
'store': 'yes',
'type': u'string'},
u'content': {'boost': 1.0,
'index': 'analyzed',
'store': 'yes',
'type': u'string',
"term_vector" : "with_positions_offsets"}
}
#索引映射
conn.put_mapping("test-type", {'properties':mapping}, ["test-index"])
return "索引映射"
4.索引
$ curl -XPUT http://localhost:9200/test-index/test-type/1 -d '{
"user": "kimchy",
"post_date": "2009-11-15T13:12:00",
"message": "Trying out elasticsearch, so far so good?"
}'
#获取
$ curl -XGET http://localhost:9200/test-index/test-type/1
#删除
$ curl -XDELETE 'http://localhost:9200/test-index/test-type/1'
def indexTest():
"""索引测试"""
conn = ES('127.0.0.1:9200')
for item in Data().getData():
#添加索引
conn.index(item,"test-index", "test-type",item['id'])
#索引优化
conn.optimize(["test-index"])
#删除索引内容
conn.delete("test-index", "test-type", 2668090)
#更新索引内容
model = conn.get("test-index", "test-type", 2667371)
model["title"]="标题修改测试"
conn.update(model,"test-index", "test-type",2667371)
#刷新索引
conn.refresh(["test-index"])
q = MatchAllQuery()
results = conn.search(query = q,indices="test-index",doc_types="test-type")
# for r in results:
# print r
return template('default.tpl', list=results,count=len(results))
5.搜索
$ curl -XGET http://localhost:9200/test-index/test-type/_search?q=user:kimchy
#query DSL方式查询
$ curl -XGET http://localhost:9200/test-index/test-type/_search -d '{
"query" : {
"term" : { "user": "kimchy" }
}
}'
#query DSL方式查询
$ curl -XGET http://localhost:9200/test-index/_search?pretty=true -d '{
"query" : {
"range" : {
"post_date" : {
"from" : "2009-11-15T13:00:00",
"to" : "2009-11-15T14:30:00"
}
}
}
}'
#查找全部索引内容
$ curl -XGET http://localhost:9200/test-index/test-type/_search?pretty=true
@route('/search/<searchkey>')
def search(searchkey=u"关键算法"):
"""索引搜索"""
conn = ES('127.0.0.1:9200')
#TextQuery会对searchkey进行分词
qtitle = TextQuery("title", searchkey)
qcontent = TextQuery("content", searchkey)
#发布时间大于"2012-9-2 22:00:00"
qpublished=RangeQuery(ESRangeOp("published", "gt", datetime(2012, 9,2, 22, 0, 0)))
h = HighLighter(['<b>'], ['</b>'], fragment_size=500)
#多字段搜索(must=>and,should=>or),高亮,结果截取(分页),排序
q = Search(BoolQuery(must=[qpublished],should=[qtitle,qcontent]),highlight=h, start=0, size=3, sort={'id': {'order': 'asc'}})
q.add_highlight("title")
q.add_highlight("content")
results = conn.search(query = q,indices="test-index",doc_types="test-type")
list=[]
for r in results:
if(r._meta.highlight.has_key("title")):
r['title']=r._meta.highlight[u"title"][0]
if(r._meta.highlight.has_key("content")):
r['content']=r._meta.highlight[u"content"][0]
list.append(r)
return template('search.tpl', list=list,count=results.total)
6.设置
$ curl -XPUT http://localhost:9200/elasticsearch/ -d '{
"settings" : {
"number_of_shards" : 2,
"number_of_replicas" : 3
}
}'
7.其他
curl -XGET 'http://localhost:9200/test-index/_analyze?text=中华人民共和国'
ElasticSearch 入门(转)的更多相关文章
- ElasticSearch入门-搜索如此简单
搜索引擎我也不是很熟悉,但是数据库还是比较了解.可以把搜索理解为数据库的like功能的替代品.因为like有以下几点不足: 第一.like的效率不行,在使用like时,一般都用不到索引,除非使用前缀匹 ...
- ElasticSearch入门知识扫盲
ElasticSearch 入门介绍 tags: 第三方 lucene [toc] 1. what Elastic Search(ES)是什么 全文检索和lucene 全文检索 优点:高效,准确,分词 ...
- 《读书报告 -- Elasticsearch入门 》--简单使用(2)
<读书报告 – Elasticsearch入门 > ' 第四章 分布式文件存储 这章的主要内容是理解数据如何在分布式系统中存储. 4.1 路由文档到分片 创建一个新文档时,它是如何确定应该 ...
- 《读书报告 -- Elasticsearch入门 》-- 安装以及简单使用(1)
<读书报告 – Elasticsearch入门 > 第一章 Elasticsearch入门 Elasticsearch是一个实时的分布式搜索和分析引擎,使得人们可以在一定规模上和一定速度上 ...
- ElasticSearch入门 附.Net Core例子
1.什么是ElasticSearch? Elasticsearch是基于Lucene的搜索引擎.它提供了一个分布式,支持多租户的全文搜索引擎,它具有HTTP Web界面和无模式JSON文档. Elas ...
- ElasticSearch入门点滴
这是Elasticsearch-6.2.4 版本系列的第一篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 ...
- 全文搜索引擎Elasticsearch入门实践
全文搜索引擎Elasticsearch入门实践 感谢阮一峰的网络日志全文搜索引擎 Elasticsearch 入门教程 安装 首先需要依赖Java环境.Elasticsearch官网https://w ...
- Elasticsearch Elasticsearch入门指导
Elasticsearch入门指导 By:授客 QQ:1033553122 1. 开启elasticsearch服务器 1 2. 基本概念 2 <1> 集群(Cluster) 2 < ...
- ElasticSearch 入门
http://www.oschina.net/translate/elasticsearch-getting-started?cmp ElasticSearch 简单入门 返回原文英文原文:Getti ...
- 全文搜索引擎 Elasticsearch 入门
1. 百科 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作 ...
随机推荐
- C#中的BackgroundWorker控件
C#中的BackgroundWorker控件 Keywords: C# .NET BackgroundWorkerSource: http://txw1958.cnblogs.com/ Backg ...
- MySQL 各种引擎
数据库中的存储引擎其实是对使用了InnoDB.HEAP(也称为MEMORY).CSV.BLACKHOLE(黑洞引擎).ARCHIVE.PERFORMANCE_SCHEMA. Berkeley.Merg ...
- Javascript-- jQuery Ajax应用
使用ajax()方法加载服务器数据 使用ajax()方法是最底层.功能最强大的请求服务器数据的方法,它不仅可以获取服务器返回的数据,还能向服务器发送请求并传递数值,它的调用格式如下: jQuery.a ...
- winform常用方法
1.对象的初始化器: Class a = new Class() { id = , name = "张三" } 2.窗体间传值 ①构造函数 ②单例函数 //单例模式:确 ...
- uva11292 Dragon of Loowater(排序后贪心)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- matrix-gui-2.0 将javascript文件夹改成js文件夹
/******************************************************************************** * matrix-gui-2.0 将 ...
- [转]NYOJ-511-移动小球
大学生程序代写 http://acm.nyist.net/JudgeOnline/problem.php?pid=511 这道题很容易想到要构建一个循环链表来确定每个球的相对位置,就是操作比较繁琐,考 ...
- mysql-jdbc创建connection理解
jdbc源码分析(http://blog.csdn.net/brilliancezhou/article/details/5499738) 创建JDBC连接代码 Class.forName(" ...
- CodeForces - 961D:Pair Of Lines (几何,问两条直线是否可以覆盖所有点)
You are given n points on Cartesian plane. Every point is a lattice point (i. e. both of its coordin ...
- Qt之事件处理机制
思维导读 一.事件简介 QT程序是事件驱动的, 程序的每个动作都是由内部某个事件所触发.QT事件的发生和处理成为程序运行的主线,存在于程序整个生命周期. 常见的QT事件类型如下: 键盘事件: 按键按下 ...