最大的特点: 
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" }
    }
}'

@route('/indexsetting/')
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'

@route('/indextest/')
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.搜索

#lucene语法方式的查询
$ 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/')
@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 入门(转)的更多相关文章

  1. ElasticSearch入门-搜索如此简单

    搜索引擎我也不是很熟悉,但是数据库还是比较了解.可以把搜索理解为数据库的like功能的替代品.因为like有以下几点不足: 第一.like的效率不行,在使用like时,一般都用不到索引,除非使用前缀匹 ...

  2. ElasticSearch入门知识扫盲

    ElasticSearch 入门介绍 tags: 第三方 lucene [toc] 1. what Elastic Search(ES)是什么 全文检索和lucene 全文检索 优点:高效,准确,分词 ...

  3. 《读书报告 -- Elasticsearch入门 》--简单使用(2)

    <读书报告 – Elasticsearch入门 > ' 第四章 分布式文件存储 这章的主要内容是理解数据如何在分布式系统中存储. 4.1 路由文档到分片 创建一个新文档时,它是如何确定应该 ...

  4. 《读书报告 -- Elasticsearch入门 》-- 安装以及简单使用(1)

    <读书报告 – Elasticsearch入门 > 第一章 Elasticsearch入门 Elasticsearch是一个实时的分布式搜索和分析引擎,使得人们可以在一定规模上和一定速度上 ...

  5. ElasticSearch入门 附.Net Core例子

    1.什么是ElasticSearch? Elasticsearch是基于Lucene的搜索引擎.它提供了一个分布式,支持多租户的全文搜索引擎,它具有HTTP Web界面和无模式JSON文档. Elas ...

  6. ElasticSearch入门点滴

    这是Elasticsearch-6.2.4 版本系列的第一篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 ...

  7. 全文搜索引擎Elasticsearch入门实践

    全文搜索引擎Elasticsearch入门实践 感谢阮一峰的网络日志全文搜索引擎 Elasticsearch 入门教程 安装 首先需要依赖Java环境.Elasticsearch官网https://w ...

  8. Elasticsearch Elasticsearch入门指导

    Elasticsearch入门指导 By:授客 QQ:1033553122 1. 开启elasticsearch服务器 1 2. 基本概念 2 <1> 集群(Cluster) 2 < ...

  9. ElasticSearch 入门

    http://www.oschina.net/translate/elasticsearch-getting-started?cmp ElasticSearch 简单入门 返回原文英文原文:Getti ...

  10. 全文搜索引擎 Elasticsearch 入门

    1. 百科 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作 ...

随机推荐

  1. 《Effective C++》——条款08:别让异常逃离析构函数

    考虑如下代码: class Widget{ public: ... ~Widget(){...}//假设这个可能吐出一个异常 }; void doSomething() { std::vector&l ...

  2. SQL-主键与外键

    1.PRIMARY KEY 主键,唯一标识一行或多行,不允许重复值,也不允许未NULL. 语法:[CONSTRAINT <约束名>] PRIMARY KEY [(列名1,列名2...)] ...

  3. hdu1863(最小生成树)

    很裸的最小生成树,但要注意判断输出问号的情况.其实就是当给的图不是连通图时输出问号.判断方法是:看形成的最小生成树的边数是不是等于节点数减一. #include<iostream> #in ...

  4. 利用HTML5开发Android笔记(中篇)

    资源来自于www.mhtml5.com 杨丰盛老师成都场的PPT分享 一个很简明的demo 可以作为入门基础 学习的过程中做了点笔记 整理如下 虽然内容比较简单 但是数量还是比较多的 所以分了3篇 ( ...

  5. 真机环境spotlight光源丢失

    maya做好的模型生成的fbx,导入到unity之后,pc运行正常,到了ios真机上发现光线丢失,场景内物体都是暗暗的,查出来原因是spot光源丢失了,选中spot光源,在其Render Mode里, ...

  6. 如何隐藏掉Nginx的版本号

    最近新学习了一个命令curl,里面有一个参数-I可以查看到网站使用的是哪种服务器,比如: zhangxiaoliudeMacBook-Pro-2:~ zhangxiaoliu$ curl -I htt ...

  7. JQ选择器大全

    jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个 ...

  8. 机器学习:SVM(基础理解)

    一.基础理解 1)简介 SVM(Support Vector Machine):支撑向量机,既可以解决分类问题,又可以解决回归问题: SVM 算法可分为:Hard Margin SVM.Soft Ma ...

  9. 解决django不能以本机ip地址访问

    在使用django框架来架设网站时,我们测试一般是通过django的开发服务器来完成,但是我们可以看到生成的地址是127.0.0.1:8000这样的话,我们在外网就无法访问了. 解决办法是通过传入第三 ...

  10. Ubuntu 开启telnet、ftp服务

    Telnet 这里我们就来对Ubuntu Linux telnet的安装设置进行一下讲解. 1. sudo apt-get install xinetd telnetd 2. Ubuntu Linux ...