搜索记录

通过关键词搜索记录

示例1:

# encoding:utf-8
#!/usr/bin/python

from elasticsearch import Elasticsearch

class ElasticObj:
    def __init__(self, index_name, index_type, ip ="127.0.0.1"):

        '''
        :param index_name: 索引名称
        :param index_type: 索引类型
        '''
        self.index_name =index_name
        self.index_type = index_type

        # 无用户名密码状态
        self.es = Elasticsearch([ip])

        #用户名密码状态
        # self.es = Elasticsearch([ip],http_auth=('elastic', 'password'),port=9200)

    '''
    搜索数据
    '''
    def Search(self, query):
        print self.es.search(index=self.index_name, doc_type=self.index_type, body=query)


if __name__ == "__main__":

    obj =ElasticObj("test", "test_type", ip ="127.0.0.1")
    query = {'query': {'match': {'title': 'Eloquent'}}}
    obj.Search(query)

执行结果:

{
  u'hits': {
    u'hits': [
      {
        u'_score': 0.2876821,
        u'_type': u'test_type',
        u'_id': u'2',
        u'_source': {
          u'url': u'https://www.codercto.com/courses/d/62.html',
          u'id': 2,
          u'title': u'Eloquent: 模型关联'
        },
        u'_index': u'test'
      },
      {
        u'_score': 0.2876821,
        u'_type': u'test_type',
        u'_id': u'1',
        u'_source': {
          u'url': u'https://www.codercto.com/courses/d/61.html',
          u'id': 1,
          u'title': u'Eloquent: 入门'
        },
        u'_index': u'test'
      }
    ],
    u'total': 2,
    u'max_score': 0.2876821
  },
  u'_shards': {
    u'successful': 5,
    u'failed': 0,
    u'skipped': 0,
    u'total': 5
  },
  u'took': 23,
  u'timed_out': False
}

可以看到 2 条包含 Eloquent 关键词的数据被检索出来了。

示例2:

obj =ElasticObj("test", "test_type", ip ="127.0.0.1")
query = {'query': {'match': {'title': '入门'}}}
obj.Search(query)

执行结果:

{
  u'hits': {
    u'hits': [
      {
        u'_score': 0.5753642,
        u'_type': u'test_type',
        u'_id': u'1',
        u'_source': {
          u'url': u'https://www.codercto.com/courses/d/61.html',
          u'id': 1,
          u'title': u'Eloquent:入门'
        },
        u'_index': u'test'
      }
    ],
    u'total': 1,
    u'max_score': 0.5753642
  },
  u'_shards': {
    u'successful': 5,
    u'failed': 0,
    u'skipped': 0,
    u'total': 5
  },
  u'took': 10,
  u'timed_out': False
}

可以看到只有条件包含 入门 的数据被检索出来了。

点击查看所有 Python 操作 ElasticSearch 简明教程 文章: https://codercto.com/courses/l/6.html

查看所有标签

美工神话

美工神话

聂小燕、赵秋轶 / 人民邮电出版社 / 2006 / 52.00元

本书是一本专门针对网页美工设计的图书,全面、细致地介绍利用 Dreamweaver 8 和 Photoshop CS2 进行创意和设计的具体方法和步骤。 全书共分为4篇12章,在阐述网页色彩设计和风格创意的基础上,以生动的实例引导读者全面地掌握 Photoshop 网页元素设计与 Dreamweaver 网页布局美化。书中涉及了诸多网页美化所必须掌握的知识点,比如色彩知识,风格创意,网页规划......一起来看看 《美工神话》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

MD5 加密
MD5 加密

MD5 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具