前言

最近有一个项目用到了搜索引擎,这里记录下使用过程中遇到的一些问题和解决方案。

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的关键技术点的更多相关文章

  1. 互联网DSP广告系统架构及关键技术解析

    互联网DSP广告系统架构及关键技术解析 宿逆 关注 1.9 2017.10.09 17:05* 字数 8206 阅读 10271评论 2喜欢 60 广告和网络游戏是互联网企业主要的盈利模式 广告是广告 ...

  2. DSP广告系统架构及关键技术解析(转)

    广告和网络游戏是互联网企业主要的盈利模式 广告是广告主通过媒体以尽可能低成本的方式与用户达成接触的商业行为.也就是说按照某种市场意图接触相应人群,影响其中潜在用户,使其选择广告主产品的几率增加,或对广 ...

  3. 5G关键技术评述

    业内重大事件: 张  平:无线通信领域专家,北京邮电大学教授,博士生导师,现任北京邮电大学无线新技术研究所(WTI)所长.泛网无线通信教育部重点实验室主任以及中德软件研究所副所长.张平教授是国家宽带无 ...

  4. 大型网站提速关键技术(页面静态化,memcached,MySql优化)(一)

    一:关键技术介绍: 衡量是否为大型网站的要素: A:PV值(page views 页面浏览量) 访问量大: 带来的问题:1:流量大 -->解决方案:增加带宽,优化程序(视频和图片较浪费带宽,尽量 ...

  5. Java进阶(三)多线程开发关键技术

    原创文章,同步发自作者个人博客,转载请务必以超链接形式在文章开头处注明出处http://www.jasongj.com/java/multi_thread/. sleep和wait到底什么区别 其实这 ...

  6. (1)RGB-D SLAM系列- 工具篇(硬件+关键技术)

    /*************************************************************************************************** ...

  7. 操作PDF文件的关键技术点

    一个PDF文档从大到小可以分成如下几个要素:文档.章节.小节.段落.表格.列表. com.lowagie.text.Document表示PDF文档.必须为它创建一个PDF写入器,即com.lowagi ...

  8. 实时视频应用之QoS关键技术分析

    转自:http://www.aiweibang.com/m/detail/104476372.html?from=p 随着WebRTC标准的逐步推广,实时音视频通讯技术受到越来越多公司和技术人员的关注 ...

  9. Java Hotspot G1 GC的一些关键技术

    G1 GC,全称Garbage-First Garbage Collector,通过-XX:+UseG1GC参数来启用,作为体验版随着JDK 6u14版本面世,在JDK 7u4版本发行时被正式推出,相 ...

随机推荐

  1. 解决SublimeCodeIntel回车换行误打代码

    SublimeCodeIntel会自动匹配并联想词汇, 这在换行的时候非常麻烦, 每次点Enter 都会误打出代码, 解决办法分两步:第一步是在Perferences/setting User 中加入 ...

  2. lodash常用方法2--修改

    1.map function timesThree(n) { return n * 3; } _.map([1, 2], timesThree); // => [3, 6] 2.remove 移 ...

  3. linux kernel: possible SYN flooding on port 8080. Sending cookie

    possible SYN flooding on port 7244. Sending cookie

  4. java内存模型-重排序

    数据依赖性 如果两个操作访问同一个变量,且这两个操作中有一个为写操作,此时这两个操作之间就存在数据依赖性.数据依赖分下列三种类型: 名称 代码示例 说明 写后读 a = 1;b = a; 写一个变量之 ...

  5. Close与Dispose的区别

    Close与Dispose的区别: Close 是停业整顿,停业了,可以通过公关,再重开,物还是原来的物:只是关闭而已,没有释放真正的释放资源,可以重新打开:Close是关门Dispose是破产: D ...

  6. spring常用注解使用解析

    spring没有采用约定优于配置的策略,spring要求显示指定搜索哪些路径下的Java文件.spring将会把合适的java类全部注册成spring Bean.   问题:spring怎么知道把哪些 ...

  7. 粒子动画Particleground.js

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. Aristochart – 灵活的 HTML5 Canvas 折线图

    Aristochart 是基于 HTML5 Canvas 的折线图功能库,具有高定制性和灵活性的特点.Aristochart 会帮助你处理图形显示,让你能够专注于业务逻辑处理. 您可能感兴趣的相关文章 ...

  9. go语言 rune切片

    go语言 rune切片 示例 package main import ( "fmt" ) //http://www.cnblogs.com/osfipin/ func main() ...

  10. Sass学习之路(5)——变量

    1.定义变量:Sass中定义变量的关键字是'$'(毕竟程序员缺钱),并使用冒号(:)进行赋值,例如: $width:200px;//定义了一个名为width的变量,值为200px 2.普通变量和默认变 ...