Python编程:elasticsearch库操作Elasticsearch

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: Python编程:elasticsearch库操作Elasticsearch

使用Python 的接口库elasticsearch 对ES数据库进行操作

安装

pip install elasticsearch
• 1

ES 文档:https://elasticsearch-py.readthedocs.io/en/master/


1、创建新的索引

中文搜索需要制定ik分词器,类似结巴jieba

IK分词器文档: https://github.com/medcl/elasticsearch-analysis-ik

PUT http://localhost:9200/blog
{
    "settings" : {
        "index" : {
            "analysis.analyzer.default.type": "ik_max_word"
        } 
    } 
}

2、检查分词效果

如果没有使用中文分词器,默认单个字符分隔,出现词组说明分词器设置成功

POST http://localhost:9200/blog/_analyze
{"field":"title", "text":"拼多多确认警方成立专案组 实际资损大概率低于千万"}
{
    "tokens": [
        {
            "token": "拼",
            "start_offset": 0,
            "end_offset": 1,
            "type": "CN_CHAR",
            "position": 0
        },
        {
            "token": "多多",
            "start_offset": 1,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 1
        },
        {
            "token": "确认",
            "start_offset": 3,
            "end_offset": 5,
            "type": "CN_WORD",
            "position": 2
        },
        {
            "token": "警方",
            "start_offset": 5,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 3
        }
        ...
    ]
}

3、添加数据

from elasticsearch import Elasticsearch
# 实例化
es = Elasticsearch()
# 批量提交数据, 注意格式,一行指令一行数据
bulk_doc = """
{"index":{ "_index": "blog", "_type": "post", "_id": "001" }}
{"title": "比亚迪:今年将推出多款新车型","post_time": "2019-01-21 14:22:58","source": "36氪"}
{"index":{ "_index": "blog", "_type": "post", "_id": "002" }}
{"title": "亚马逊:2018年近20万第三方卖家年销售额超10万美元,同比增长40%","post_time": "2019-01-21 14:21:01","source": "雨果网"}
{"index":{ "_index": "blog", "_type": "post", "_id": "003" }}
{"title": "拼多多确认警方成立专案组 实际资损大概率低于千万","post_time": "2019-01-21 14:15:52","source": "新浪财经"}
"""
result = es.bulk(bulk_doc)
print(result)
"""
{
    "took":30,
    "errors":false,
    "items":[
        {
            "index":{
                "_index":"blog",
                "_type":"post",
                "_id":"001",
                "_version":1,
                "result":"created",
                "_shards":{
                    "total":2,
                    "successful":1,
                    "failed":0
                },
                "_seq_no":0,
                "_primary_term":1,
                "status":201
            }
        }
        ...
    ]
}
"""

4、搜索查询

query_body = {
    "query": {
        "term": {
            "title": "多多"
        }
    }
}
ret = es.search("blog", "post", query_body)
print(ret)
"""
{
    "took":2,
    "timed_out":false,
    "_shards":{
        "total":5,
        "successful":5,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":1,
        "max_score":0.2876821,
        "hits":[
            {
                "_index":"blog",
                "_type":"post",
                "_id":"003",
                "_score":0.2876821,
                "_source":{
                    "title":"拼多多确认警方成立专案组 实际资损大概率低于千万",
                    "post_time":"2019-01-21 14:15:52",
                    "source":"新浪财经"
                }
            }
        ]
    }
}
"""

如果不使用分词器,也可以使用短语查询

query_body = {
    "query": {
        "match_phrase": {
            "title": "拼多多"
        }
    }
}
相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
17小时前
|
Python
【Python操作基础】——帮助文档
【Python操作基础】——帮助文档
|
17小时前
|
Python
【Python操作基础】——包
【Python操作基础】——包
|
17小时前
|
Python
【Python操作基础】——函数
【Python操作基础】——函数
|
17小时前
|
Python
【Python操作基础】——字典,迭代器和生成器
【Python操作基础】——字典,迭代器和生成器
|
17小时前
|
索引 Python
【Python操作基础】——序列
【Python操作基础】——序列
|
17小时前
|
Python
【Python操作基础】——字符串
【Python操作基础】——字符串
|
17小时前
|
Python
【Python操作基础】——元组
【Python操作基础】——元组
|
17小时前
|
Python
【Python操作基础】——列表操作
【Python操作基础】——列表操作
|
17小时前
|
Python
【Python操作基础】——while语句用法和pass语句
【Python操作基础】——while语句用法和pass语句
|
17小时前
|
Python
【Python操作基础】——for语句用法
【Python操作基础】——for语句用法