使用elasticsearch的关键技术点
前言
最近有一个项目用到了搜索引擎,这里记录下使用过程中遇到的一些问题和解决方案。
0.准备工作
1)安装elasticsearch
2)安装Marvel
3)安装head
tips:在es的配置文件(/config/elasticsearch.yml)中可以看到es设置的对外服务的http端口,默认为9200
http.port: 9200
但是我们的服务器没有开放9200端口,因此需要改配置文件,这里改为:
http.port:8080
这时head和sense就都可以访问了,如下地址:
http://IP_ADDRESS:8080/_plugin/head/
http://IP_ADDRESS:8080/_plugin/marvel/sense/
1. 一些API,如存储,删除,查看设置等
存储
PUT /index_name/type_name/id
{
"field1":"value1",
"field2":"value2"
}
删除index:
DELETE /index_name
查看index的setting:
GET /user/_settings
返回了如下的一个结果:
{
"user": {
"settings": {
"index": {
"creation_date": "1437553188027",
"uuid": "Ui-wLKGSS2y_bJwb71gLtA",
"number_of_replicas": "1",
"number_of_shards": "5",
"version": {
"created": "1060099"
}
}
}
}
}
更改settings
如果想要定义新的analyzers或者filters,那么首先_close the index, 在更新完成后再_open
POST /user/_close
PUT /user/_settings
{
"analysis":{
"analyzer":{
"my_analyzer":{
"type":"whitespace",
"filter":["standard", "lowercase"],
"tokenizer":"standard"
}
}
}
}
POST /user/_open
查找API
查找一项
GET /user/youku/_search?explain
{
"query": {
"term": {
"req_app_content_keywords": {
"value": "睫毛膏 裸妆"
}
}
}
}
查找多项
GET /user/_search?explain
{
"query": {
"terms": {
"req_app_content_keywords": [
"睫毛膏",
"裸妆"
]
}
}
}
2. 解决一个问题:要对关键字中的中文词语进行查找
问题描述如下:
需要查找的关键字:英雄联盟
req_app_content_keywords:英雄联盟 起小点 TOP10 S3集锦
elasticserach里默认的分词器是standard,对英文的分词支持的比较好,但是对于中文,比如调用下面的语句:
GET /user/_analyze?analyzer=standard
{
"text":"明天会更好"
}
返回的结果就是:
{
"tokens": [
{
"token": "text",
"start_offset": 5,
"end_offset": 9,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "明",
"start_offset": 12,
"end_offset": 13,
"type": "<IDEOGRAPHIC>",
"position": 2
},
{
"token": "天",
"start_offset": 13,
"end_offset": 14,
"type": "<IDEOGRAPHIC>",
"position": 3
},
{
"token": "会",
"start_offset": 14,
"end_offset": 15,
"type": "<IDEOGRAPHIC>",
"position": 4
},
{
"token": "更",
"start_offset": 15,
"end_offset": 16,
"type": "<IDEOGRAPHIC>",
"position": 5
},
{
"token": "好",
"start_offset": 16,
"end_offset": 17,
"type": "<IDEOGRAPHIC>",
"position": 6
}
]
}
那么假设想要查找中文词语,是查找不到的,只能用单个的单词,才会查找到结果:
GET /local/hello/_search
{
"query": {
"term": {
"tags": {
"value": "角"
}
}
}
}
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.19178301,
"hits": [
{
"_index": "local",
"_type": "hello",
"_id": "1",
"_score": 0.19178301,
"_source": {
"tags": "角色",
"name": "1234"
}
}
]
}
}
解决方案:
设置Mapping,那么不同的field可以有不同的index:
analyzed
First analyze the string and then index it.
not_analyzed
Index this field, so it is searchable, but index the value exactly as specified. Do not analyze it.
no
Don't index this field at all. This field will not be searchable.
因为这里只要进行最简单的分词,就是使用空格来分割,那么可以使用elasticsearch自带的analyzer:whitespace,
whitespace以空格为分隔符拆分
对各个mapping设定不同的分词器的API如下:
分词器的设定要在创建的时候就设定,后面新数据的加入会转化成最开始设定的格式,如果转化不成功,就会报错。
PUT /user
{
"mappings": {
"youku": {
"properties": {
"tags":{
"type": "string",
"analyzer": "whitespace"
},
"req_app_content_keywords":{
"type": "string",
"analyzer": "whitespace"
},
"req_app_content_title":{
"type": "string",
"analyzer": "whitespace"
}
}
}
}
}
3. 探索es的相似性的计算方式
检索词频率:(TF)
How often does the term appear in the field? The more often, the more relevant. A field containing five mentions of the same term is more likely to be relevant than a field containing just one mention.
tf(t in d) = √frequency
The term frequency (tf) for term t in document d is the square root of the number of times the term appears in the document.
按照这个定义,我们的field中的term由于都是唯一的,所以其TF都等于1.
IDF:
每个检索词在索引中出现的频率?频率越高,相关性越低。 检索词出现在多数文档中会比出现在少数文档中的权重更低, 即检验一个检索词在文档中的普遍重要性。
How often does each term appear in the index? The more often, the less relevant. Terms that appear in many documents have a lower weight than more-uncommon terms.
idf(t) = 1 + ln ( numDocs / (docFreq + 1))
Field-lenth norm
norm(d) = 1 / √numTerms
我们这里,Norms are not useful. Disabling norms can save a significant amount of memory.
So,我认为我们的算法里的norm是不需要的。比如说视频关键字,其关键字的多少对相似度是没有影响的。
PUT /my_index
{
"mappings": {
"doc": {
"properties": {
"text": {
"type": "string",
"norms": { "enabled": false }
}
}
}
}
}
使用elasticsearch的关键技术点的更多相关文章
- 互联网DSP广告系统架构及关键技术解析
互联网DSP广告系统架构及关键技术解析 宿逆 关注 1.9 2017.10.09 17:05* 字数 8206 阅读 10271评论 2喜欢 60 广告和网络游戏是互联网企业主要的盈利模式 广告是广告 ...
- DSP广告系统架构及关键技术解析(转)
广告和网络游戏是互联网企业主要的盈利模式 广告是广告主通过媒体以尽可能低成本的方式与用户达成接触的商业行为.也就是说按照某种市场意图接触相应人群,影响其中潜在用户,使其选择广告主产品的几率增加,或对广 ...
- 5G关键技术评述
业内重大事件: 张 平:无线通信领域专家,北京邮电大学教授,博士生导师,现任北京邮电大学无线新技术研究所(WTI)所长.泛网无线通信教育部重点实验室主任以及中德软件研究所副所长.张平教授是国家宽带无 ...
- 大型网站提速关键技术(页面静态化,memcached,MySql优化)(一)
一:关键技术介绍: 衡量是否为大型网站的要素: A:PV值(page views 页面浏览量) 访问量大: 带来的问题:1:流量大 -->解决方案:增加带宽,优化程序(视频和图片较浪费带宽,尽量 ...
- Java进阶(三)多线程开发关键技术
原创文章,同步发自作者个人博客,转载请务必以超链接形式在文章开头处注明出处http://www.jasongj.com/java/multi_thread/. sleep和wait到底什么区别 其实这 ...
- (1)RGB-D SLAM系列- 工具篇(硬件+关键技术)
/*************************************************************************************************** ...
- 操作PDF文件的关键技术点
一个PDF文档从大到小可以分成如下几个要素:文档.章节.小节.段落.表格.列表. com.lowagie.text.Document表示PDF文档.必须为它创建一个PDF写入器,即com.lowagi ...
- 实时视频应用之QoS关键技术分析
转自:http://www.aiweibang.com/m/detail/104476372.html?from=p 随着WebRTC标准的逐步推广,实时音视频通讯技术受到越来越多公司和技术人员的关注 ...
- Java Hotspot G1 GC的一些关键技术
G1 GC,全称Garbage-First Garbage Collector,通过-XX:+UseG1GC参数来启用,作为体验版随着JDK 6u14版本面世,在JDK 7u4版本发行时被正式推出,相 ...
随机推荐
- Java经典实例:纪元秒和本地日期时间互换
Java版本:1.8开始 import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; /** ...
- this指向问题
我今天下午本来想做个就是tr鼠标移出之后过三秒把对应的input添加hiddens类 然后我就这样写了 $('.table>tbody>tr').mouseout(function(){ ...
- [deviceone开发]-大家比较关注的应用内部升级
一.简介 这个示例详细介绍了应用内升级的步骤,应用内升级是开发App必须的功能.推荐初学者学习. 二.效果图 三.相关下载 https://github.com/do-project/code4do/ ...
- javascript笔记图
1.this 2.对象 3.继承 4.跨域 5.事件 6.基础
- PyCharm使用(完全图解(最新经典))
PyCharm使用 一.PyCharm设置(版本:PyCharm 2016.1.2) 1.python环境设置 1.1.pycharm新建程序自动补全编码和环境: pycharm设置在 ...
- 关于SAP的事务提交和回滚(LUW)
1 Sap的更新的类型 在sap中,可以使用CALL FUNCTION ... IN UPDATE TASK将多个数据更新绑定到一个database LUW中.程序使用COMMIT WORK提交修改请 ...
- ReCap 360 photo照片建模技术的又一个例子
这是我做的又一个利用Autodesk ReCap 360 照片建模技术做的一个例子.你可以下载模型自己把玩,或者下载原始照片自己试一试. 拍摄工具: 小米手机 照片数量:约120张 后期处理工具: p ...
- android HorizontalScrollView
第一个控件,借鉴网上的资料,自己稍加修改,横向滑动图片浏览功能,纪念下 布局文件 <?xml version="1.0" encoding="utf-8" ...
- Android Handler机制(二)---MessageQueue源码解析
MessageQueue 1.变量 private final boolean mQuitAllowed;//表示MessageQueue是否允许退出 @SuppressWarnings(" ...
- mac 终端启动 jboss 停留在(Starting) 不能启动joss
今天下载了jboss,解压后发现standalone.sh,不能够在终端中执行,于是google了一下,原来是由于jdk版本的问题导致jboss一直停在调试状态(貌似是这样说的). 需要把mac系统的 ...