背景:

  1,系统简介:通过人工解读研报然后获取并录入研报分类及摘要等信息,系统通过摘要等信息来获得该研报的URI

  2,现有实现:老系统使用MSSQL存储摘要等信息,并将不同的关键字分解为不同字段来提供搜索查询

  3,存在问题:

    -查询操作繁琐,死板:例如要查某个机构,标题含有周报的研报,现有系统需要勾选相应字段再输入条件

    -查询速度缓慢,近千万级别数据响应时间4-5s

  4,改进:使用es优化,添加多个关键字模糊查询(非长文本数据,因此未使用_socre进行评分查询)

    -例如:输入“国泰君安 周报”就可查询到所有相关的国泰君安的周报

1,新建Index

curl -X PUT 'localhost:9200/src_test_1' -H 'Content-Type: application/json' -d '
{
"settings": {
"number_of_shards": 1,
"number_of_replicas":
},
"mappings": {
"doc_test": {
"properties": {
"title": {#研报综合标题
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"author": {#作者
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"institution": {#机构
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"industry": {#行业
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"grade": {#评级
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"doc_type": {#研报分类
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"time": {#发布时间
"type": "date" ,
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"doc_uri": {#地址
"type": "text",
"index":false
},
"doc_size": {#文件大小
"type": "integer",
"index":false
},
"market": {#市场
"type": "byte"
}
}
}
}
}'

※特别提示对于需要模糊查询的中文文本字段最好都设置text属性(keyword无法被分词:用于精确查找),并使用ik_max_word分词器。

※使用ik_max_word原因:针对该场景,例如我想使用“国泰”关键词进行匹配,如果使用默认ik会将“国”,“泰”分开进行查询,而不是需求的“国泰”这个词

2,数据导入(CSV分批)

import pandas as pd
import numpy as np
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
es = Elasticsearch() data_will_insert = []
x = 1 # #使用pandas读取csv数据;如果出现乱码加:encoding = "ISO-8859-1"
src_data = pd.read_csv('ResearchReportEx.csv') for index,i in src_data.iterrows():
x+=1
#每次插入100000条
if x%100000 == 99999:
#es批量插入
success, _ = bulk(es, data_will_insert, index='src_test_1', raise_on_error=True)
print('Performed %d actions' % success)
data_will_insert = [] #判断市场
if i['ExchangeType'] == 'CN':
market = 0
elif i['ExchangeType'] == 'HK':
market = 1
elif i['ExchangeType'] == 'World':
market = 2
else:
market = 99 data_will_insert.append({"_index":'src_test_1',"_type": 'doc_test','_source':
{
'title':i['Title'],
'author':i['AuthorName'],
'time':i['CreateTime']+':00',
'institution':i['InstituteNameCN'],
'doc_type':i['KindName'] if i['Kind2Name'] is np.NaN else i['KindName']+'|%s' % i['Kind2Name'],
'industry':'' if i['IndustryName'] is np.NaN else i['IndustryName'],
'grade':'' if i['GradeName'] is np.NaN else i['GradeName'],
'doc_uri':i['FileURL'],
'doc_size':i['Size'],
'market':market
}
}) #将最后剩余在list中的数据插入
if len(data_will_insert)>0:
success, _ = bulk(es, data_will_insert, index='src_test_1', raise_on_error=True)
print('Performed %d actions' % success)

3,查询

import time
from elasticsearch import Elasticsearch
from elasticsearch.helpers import scan # es连接
es = Elasticsearch() # 计算运行时间装饰器
def cal_run_time(func):
def wrapper(*args, **kwargs):
start_time = time.time()
res = func(*args, **kwargs)
end_time = time.time()
print(str(func) + '---run time--- %s' % str(end_time - start_time))
return res return wrapper @cal_run_time
def query_in_es():
body = {
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "国泰 报告",
"type": "cross_fields",#跨字段匹配
"fields": ["title", "institution","grade"
"doc_type","author","industry"],#在这6个字段中进行查找
"operator": "and"
}#此查询条件等于:query中的关键词都在fields中所有字段拼接成的字符中
},
{
"range": {
"time": {
"gte": '2018-02-01'#默认查询限制时间
}
}
}
],
}
}
} # 根据body条件查询
scanResp = scan(es, body, scroll="10m", index="src_test_1", doc_type="doc_test", timeout="10m")
row_num = 0 for resp in scanResp:
print(resp['_source'])
row_num += 1 print(row_num) query_in_es()

※测试结果速度相当快:多关键字查询只需零点几秒

ElasticSearch改造研报查询实践的更多相关文章

  1. PB级数据实时查询,滴滴Elasticsearch多集群架构实践

    PB级数据实时查询,滴滴Elasticsearch多集群架构实践  mp.weixin.qq.com 点击上方"IT牧场",选择"设为星标"技术干货每日送达 点 ...

  2. 让Elasticsearch飞起来!——性能优化实践干货

    原文:让Elasticsearch飞起来!--性能优化实践干货 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog ...

  3. elasticsearch要点及常用查询

    目录 elasticsearch要点及常用查询 查询与过滤 明确查询和过滤各自的优缺点,以及适用场景. 性能上的差异 适用场景 1.kibana 中操作es-查询 Mapping映射基础 mappin ...

  4. ElasticSearch第四步-查询详解

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

  5. Elasticsearch Span Query跨度查询

    ES基于Lucene开发,因此也继承了Lucene的一些多样化的查询,比如本篇说的Span Query跨度查询,就是基于Lucene中的SpanTermQuery以及其他的Query封装出的DSL,接 ...

  6. i美股投资研报--Michael Kors(IPO版) _Michael Kors(KORS) _i美股

    i美股投资研报--Michael Kors(IPO版) _Michael Kors(KORS) _i美股 i美股投资研报--Michael Kors(IPO版)

  7. ElasticSearch(6)-结构化查询

    引用:ElasticSearch权威指南 一.请求体查询 请求体查询 简单查询语句(lite)是一种有效的命令行_adhoc_查询.但是,如果你想要善用搜索,你必须使用请求体查询(request bo ...

  8. Elasticsearch java api 常用查询方法QueryBuilder构造举例

    转载:http://m.blog.csdn.net/u012546526/article/details/74184769 Elasticsearch java api 常用查询方法QueryBuil ...

  9. 【spring boot】【elasticsearch】spring boot整合elasticsearch,启动报错Caused by: java.lang.IllegalStateException: availableProcessors is already set to [8], rejecting [8

    spring boot整合elasticsearch, 启动报错: Caused by: java.lang.IllegalStateException: availableProcessors ], ...

随机推荐

  1. Leetcode: Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  2. #WEB安全基础 : HTTP协议 | 0x13 不安全的HTTP

    HTTP作为一个大规模使用的网络协议就真的安全了吗? 我们知道互联网为什么叫互联网,你可以在任何地方都可以与之相连,所以在这些可以连接的点上都可以获取互联网的部分信息. 那么HTTP通信时有什么缺点吗 ...

  3. 关于MapReduce二次排序的一点解答

    上一篇博客说明了怎么自定义Key,而且用了二次排序的例子来做测试,但没有详细的说明二次排序,这一篇说详细的说明二次排序,为了说明曾经一个思想的误区,特地做了一个3个字段的二次排序来说明.后面称其为“三 ...

  4. H.264学习--1

    1.宏块(Macro Block):一个编码图像首先要划分成多个块(4x4 像素)才能进行处理,显然宏块应该是整数个块组成,通常宏块大小为                               ...

  5. 再谈HTTP2性能提升之背后原理—HTTP2历史解剖

    即使千辛万苦,还是把网站升级到http2了,遇坑如<phpcms v9站http升级到https加http2遇到到坑>. 因为理论相比于 HTTP 1.x ,在同时兼容 HTTP/1.1 ...

  6. day14 python各种推导式详解

    推导式的套路 之前我们已经学习了最简单的列表推导式和生成器表达式.但是除此之外,其实还有字典推导式.集合推导式等等. 下面是一个以列表推导式为例的推导式详细格式,同样适用于其他推导式. variabl ...

  7. 00004-20180324-20180517-fahrenheit_converter--华氏温度到摄氏温度转换计算器

    00004-20180324-20180517-fahrenheit_converter--华氏温度到摄氏温度转换计算器 def fahrenheit_converter(C): fahrenheit ...

  8. webpack4 坑收集:html-webpack-plugin在多页面时,无法将optimization.splitChunks提取的公共块,打包到页面中

    问题描述:  有2个页面index.html和product.html,用html-webpack-plugin和optimization.splitChunks的基本配置如下 { template: ...

  9. PHP百度AI的OCR图片文字识别

    第一步可定要获取百度的三个东西 要到百度AI网站(http://ai.baidu.com/)去注册 然后获得 -const APP_ID = '请填写你的appid'; -const API_KEY ...

  10. winform Combobox出现System.Data.DataRowView的解决的方法

    个人总结: 1.触发了SelectedIndexChanged事件时:comboBox1.DataSource = dt;要放在comboBox1.SelectedIndex = 0;的上面 comb ...