最大的特点: 
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. Red hat linux 下配置Java环境(jdk)

      1.把jdk-6u25-linux-i586-rpm.bin 复制到redhat linux中,放到/usr/java 目录下,该目录是mkdir 的,并chmod 755 jdk-6u25-li ...

  2. Java_脚本引擎_03_nashorn支持es6

    一.前言 jdk1.8的nashorn 支持部分es6语法. 二.支持es6 jdk默认是关闭了对es6的支持的,想要开启对es6的支持,需要设置一下jvm参数: -Dnashorn.args=--l ...

  3. Spring_总结_02_依赖注入

    一.前言 本文承接上一节:Spring_总结_01_Spring概述 在上一节中,我们了解了Spring的最根本使命.四大原则.六大模块以及Spring的生态. 这一节我们开始了解Spring的第二大 ...

  4. xsl教程学习笔记

    一 . Hello world 尝试: Hello.xml: <?xml version="1.0" encoding="UTF-8"?> < ...

  5. 2017.12.15 python资料,转存一下。

    最近GD项目三个型号都是用Python做批量烧录和测试的.marking一下,,虽然自己不会写. 1.入门阶段 The Python Tutorial(https://docs.python.org/ ...

  6. Codeforces Round #271 (Div. 2)D(递推,前缀和)

    很简单的递推题.d[n]=d[n-1]+d[n-k] 注意每次输入a和b时,如果每次都累加,就做了很多重复性工作,会超时. 所以用预处理前缀和来解决重复累加问题. 最后一个细节坑了我多次: print ...

  7. C++ 静态常量

    #include<iostream> #include<stdexcept> #include <map> using namespace std; class n ...

  8. 2017-2018-1 20179215《Linux内核原理与分析》第十周作业

    第17章 设备与模块 一.设备类型  除了以上3种典型的设备之外,其实Linux中还有一些其他的设备类型,其中见的较多的应该算是"伪设备".所谓"伪设备",其实 ...

  9. 使用Jsonp实现跨域请求

    来自百度百科的一段话: JSONP(JSON with Padding)是JSON的一种“使用模式”,可用于解决主流浏览器的跨域数据访问的问题.由于同源策略,一般来说位于 server1.exampl ...

  10. Logstash详解之——input模块

    原文地址 Logstash由三个组件构造成,分别是input.filter以及output.我们可以吧Logstash三个组件的工作流理解为:input收集数据,filter处理数据,output输出 ...