一.官网提供的Elasticsearch的Python接口包

  1.github地址:https://github.com/elastic/elasticsearch-dsl-py

  2.安装:pip install elasticsearch-dsl

  3.有很多api,使用可参考github中的文档

二.定义写入es的Pipeline:

  1.生成索引,type及映射:

    有可能会报IllegalOperation异常,访问本地9200端口查看es版本,然后将python中的elasticsearch和elasticsearch-dsl改成相近版本即可

  1. # _*_ encoding:utf-8 _*_
  2. __author__ = 'LYQ'
  3. __date__ = '2018/10/29 11:02'
  4. #新版本把DocType改为Docment
  5. from datetime import datetime
  6. from elasticsearch_dsl import DocType,Date, Nested, Boolean, \
  7. analyzer, Completion, Keyword, Text, Integer
  8. from elasticsearch_dsl.connections import connections
  9.  
  10. # es连接到本地,可以连接到多台服务器
  11. connections.create_connection(hosts=["localhost"])
  12.  
  13. class ArticleType(DocType):
  14. "定义es映射"
  15. # 以ik解析
  16. title = Text(analyzer="ik_max_word")
  17. create_date = Date()
  18. # 不分析
  19. url = Keyword()
  20. url_object_id = Keyword()
  21. front_image_url = Keyword()
  22. front_image_path = Keyword()
  23. praise_nums = Integer()
  24. fav_nums = Integer()
  25. comment_nums = Integer()
  26. tags = Text(analyzer="ik_max_word")
  27. content = Text(analyzer="ik_max_word")
  28.  
  29. class Meta:
  30. #定义索引和type
  31. index = "jobbole"
  32. doc_type = "artitle"
  33.  
  34. if __name__ == "__main__":
  35. #调用init()方法便能生成相应所应和映射
  36. ArticleType.init()

  2.创建相应item:

  1. #导入定义的es映射
  2. from models.es import ArticleType
  3. from w3lib.html import remove_tags
  4.  
  5. class ElasticsearchPipeline(object):
  6. """
  7. 数据写入elasticsearch,定义pipeline,记得配置进setting
  8. """
  9. class ElasticsearchPipeline(object):
  10. """
  11. 数据写入elasticsearch
  12. """
  13. class ElasticsearchPipeline(object):
  14. """
  15. 数据写入elasticsearch
  16. """
  17.  
  18. def process_item(self, item, spider):
  19. #将定义的elasticsearch映射实列化
  20. articletype=ArticleType()
  21. articletype.title= item["title"]
  22. articletype.create_date = item["create_date"]
  23. articletype.url = item["url"]
  24. articletype.front_image_url = item["front_image_url"]
  25. if "front_image_path" in item:
  26. articletype.front_image_path = item["front_image_path"]
  27. articletype.praise_nums = item["praise_nums"]
  28. articletype.fav_nums = item["fav_nums"]
  29. articletype.comment_nums = item["comment_nums"]
  30. articletype.tags = item["tags"]
  31. articletype.content = remove_tags(item["content"])
  32. articletype.meta.id = item["url_object_id"]
  33.  
  34. articletype.save()
  35. return item

查看9100端口,数据插入成功

  

  1. class JobboleArticleSpider(scrapy.Item):
  2. ......
  3.  
  4. def save_to_es(self):
  5. "在item中分别定义存入es,方便不同的字段的保存"
  6. articletype = ArticleType()
  7. articletype.title = self["title"]
  8. articletype.create_date = self["create_date"]
  9. articletype.url = self["url"]
  10. articletype.front_image_url = self["front_image_url"]
  11. if "front_image_path" in self:
  12. articletype.front_image_path = self["front_image_path"]
  13. articletype.praise_nums = self["praise_nums"]
  14. articletype.fav_nums = self["fav_nums"]
  15. articletype.comment_nums = self["comment_nums"]
  16. articletype.tags = self["tags"]
  17. articletype.content = remove_tags(self["content"])
  18. articletype.meta.id = self["url_object_id"]
  19.  
  20. articletype.save()
  1. class ElasticsearchPipeline(object):
  2. """
  3. 数据写入elasticsearch
  4. """
  5.  
  6. def process_item(self, item, spider):
  7. # 将定义的elasticsearch映射实列化
  8. #调用item中的方法
  9. item.save_to_es()
  10. return item

三.搜索建议:

  实质调用anylyer接口如下:

  1. GET _analyze
  2. {
  3. "analyzer": "ik_max_word",
  4. "text" : "Python网络基础学习"
  5. }

  es文件中:

  1. from elasticsearch_dsl.analysis import CustomAnalyzer as _CustomAnalyzer
  2.  
  3. esc=connections.create_connection(ArticleType._doc_type.using)
  4.  
  5. class Customanalyzer(_CustomAnalyzer):
  6. """自定义analyser"""
  7.  
  8. def get_analysis_definition(self):
  9. # 重写该函数返回空字典
  10. return {}
  11.  
  12. ik_analyser = Customanalyzer("ik_max_word", filter=["lowercase"])
  13.  
  14. class ArticleType(DocType):
  15. "定义es映射"
  16. suggest = Completion(analyzer=ik_analyser)
  17. ......

生成该字段的信息

   2.item文件:

  1. ......
  2. from models.es import esc
  3. def get_suggest(index, info_tuple):
  4. """根据字符串和权重生成搜索建议数组"""
  5. used_words = set()
  6. suggests = []
  7. for text, weight in info_tuple:
  8. if text:
  9. # 调用es得analyer接口分析字符串
  10. # 返回解析后得分词数据
  11. words = esc.indices.analyze(index=index, analyer="ik_max_word", params={"filter": ["lowercase"]}, body=text)
  12. # 生成式过滤掉长度为1的
  13. anylyzed_words = set([r["token"] for r in words if len(r) > 1])
  14. # 去重
  15. new_words = anylyzed_words - used_words
  16. else:
  17. new_words = set()
  18. if new_words:
  19. suggests.append({"input": list(new_words), "weight": weight})
      return suggests
  20. class JobboleArticleSpider(scrapy.Item):
  21. ......
  22. def save_to_es(self):
  23. articletype = ArticleType()
  24. .......# 生成搜索建议字段,以及字符串和权重
  25. articletype.suggest = get_suggest(ArticleType._doc_type.index,((articletype.title,1),(articletype.tags,7)) )
  26.  
  27. articletype.save()

  

Python对elasticsearch的CRUD的更多相关文章

  1. python实现elasticsearch操作-CRUD API

    python操作elasticsearch常用API 目录 目录 python操作elasticsearch常用API1.基础2.常见增删改操作创建更新删除3.查询操作查询拓展类实现es的CRUD操作 ...

  2. ElasticSearch第二步-CRUD之Sense

    ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearch第三步-中文分词 ElasticSea ...

  3. Python 操作 ElasticSearch

    Python 操作 ElasticSearch 学习了:https://www.cnblogs.com/shaosks/p/7592229.html 官网:https://elasticsearch- ...

  4. Python 和 Elasticsearch 构建简易搜索

    Python 和 Elasticsearch 构建简易搜索 作者:白宁超 2019年5月24日17:22:41 导读:件开发最大的麻烦事之一就是环境配置,操作系统设置,各种库和组件的安装.只有它们都正 ...

  5. Python操作ElasticSearch

    Python批量向ElasticSearch插入数据 Python 2的多进程不能序列化类方法, 所以改为函数的形式. 直接上代码: #!/usr/bin/python # -*- coding:ut ...

  6. 笔记13:Python 和 Elasticsearch 构建简易搜索

    Python 和 Elasticsearch 构建简易搜索 1 ES基本介绍 概念介绍 Elasticsearch是一个基于Lucene库的搜索引擎.它提供了一个分布式.支持多租户的全文搜索引擎,它可 ...

  7. Python中elasticsearch插入和更新数据的实现方法

    Python中elasticsearch插入和更新数据的实现方法 这篇文章主要介绍了Python中elasticsearch插入和更新数据的实现方法,需要的朋友可以参考下 首先,我的索引结构是酱紫的. ...

  8. python操作Elasticsearch (一、例子)

    E lasticsearch是一款分布式搜索引擎,支持在大数据环境中进行实时数据分析.它基于Apache Lucene文本搜索引擎,内部功能通过ReST API暴露给外部.除了通过HTTP直接访问El ...

  9. Elasticsearch的CRUD:REST与Java API

    CRUD(Create, Retrieve, Update, Delete)是数据库系统的四种基本操作,分别表示创建.查询.更改.删除,俗称"增删改查".Elasticsearch ...

随机推荐

  1. 极光推送(C#)

    推荐使用appSetting 加载这两个参数 webConfig: <appSettings> <add key="AppKey" value="ccc ...

  2. 「JOI 2016 Final」断层

    嘟嘟嘟 今天我们模拟考这题,出的是T3.实在是没想出来,就搞了个20分暴力(还WA了几发). 这题关键在于逆向思维,就是考虑最后的\(n\)的个点刚开始在哪儿,这样就减少了很多需要维护的东西. 这就让 ...

  3. PHP操作Redis常用技巧总结

    一.Redis连接与认证 //连接参数:ip.端口.连接超时时间,连接成功返回true,否则返回false $ret = $redis->connect('127.0.0.1', 6379, 3 ...

  4. 【vue】vue +element 搭建项目,在使用InputNumber 计数器时遇到的问题

    自己遇到的坑: InputNumber 计数器的change事件定义时如果不传入参数value,会产生this.num不同步的问题 <template> <el-input-numb ...

  5. Luogu2398 GCD SUM

    Luogu2398 GCD SUM 求 \(\displaystyle\sum_{i=1}^n\sum_{j=1}^n\gcd(i,j)\) \(n\leq10^5\) 数论 先常规化式子(大雾 \[ ...

  6. 请根据英文单词的第一个字母判断星期几,如果第一个字母是一样的,则继续判断第二个字母。例如如果第一个字母是S,则继续判断第二个字母,如果第二个字母是a,则输出“星期六”

    请根据英文单词的第一个字母判断星期几,如果第一个字母是一样的,则继续判断第二个字母.例如如果第一个字母是S,则继续判断第二个字母,如果第二个字母是a,则输出“星期六”.星期的英文单词如下表所示. 星期 ...

  7. visualbox 安装

    1.下载地址:官网 2.安装步骤 3.新建虚拟机

  8. Generative Adversarial Nets[pix2pix]

    本文来自<Image-to-Image Translation with Conditional Adversarial Networks>,是Phillip Isola与朱俊彦等人的作品 ...

  9. [Luogu4916]魔力环[Burnside引理、组合计数、容斥]

    题意 题目链接 分析 sπo yyb 代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; #defi ...

  10. A2D Framework - 看如何精简业务逻辑 - 缓存子系统

    A2D中一项功能是关于Cache的,能够将判断.获取.删除cache的代码缩减到最少量,如下是Order业务逻辑的demo示范: interface IOrder { [Cachable()] str ...