zoukankan      html  css  js  c++  java
  • elasticsearch的基本用法(转载)

    本文出自:http://blog.csdn.net/feelig/article/details/8499614

    最大的特点: 
    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/-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(20129,22200)))

        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=中华人民共和国'
  • 相关阅读:
    window 窗口对象http://www.diybl.com/course/1_web/javascript/jsjs/2007104/76401.html
    Web开发中的Drag&Drop完全手册http://www.cnblogs.com/birdshome/archive/2006/07/22/457369.aspx
    [转载]window 的 setTimeout() 與 clearTimeout() 方法
    javascript this详解(转) http://www.cnblogs.com/felix2007/archive/2007/03/21/682670.html
    关闭浏览器弹出对话框事件onbeforeunload http://www.blogjava.net/jennyli/articles/82351.html
    页面跳转http://www.9ba.cn/post/235.html
    POJ 3667 Hotel
    hdu 2511 汉诺塔 X
    UESTC Another LCIS
    Hdu 1576 A/B
  • 原文地址:https://www.cnblogs.com/zhanggl/p/3908749.html
Copyright © 2011-2022 走看看