Elasticsearch 实现自定义排序插件(转载)

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 转自:http://www.cnblogs.com/xffy1028/p/6383676.html插件入口:package ttd.ugc.plugin;import org.

转自:http://www.cnblogs.com/xffy1028/p/6383676.html


插件入口:

package ttd.ugc.plugin;

import org.elasticsearch.plugins.Plugin;

import org.elasticsearch.script.ScriptModule;

/**

* Created by jin_h on 2017/1/9.

*/

public class NativeScriptPlugin  extends Plugin {

@Override

public String name() {

return "comment-default-sort";

}

@Override

public String description() {

return "comment-default-sort";

}

public void onModule(ScriptModule module) {

//comment-default-sort排序算法的名称

module.registerScript("comment-default-sort", CommentDefaultSortScriptFactory.class);

}

}

插件具体实现:

package ttd.ugc.plugin;

import org.elasticsearch.common.Nullable;

import org.elasticsearch.index.fielddata.ScriptDocValues;

import org.elasticsearch.script.AbstractDoubleSearchScript;

import org.elasticsearch.script.AbstractLongSearchScript;

import org.elasticsearch.script.ExecutableScript;

import org.elasticsearch.script.NativeScriptFactory;

import org.elasticsearch.search.lookup.LeafDocLookup;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Map;

/**

* Created by jin_h on 2017/1/9.

*/

public class CommentDefaultSortScriptFactory implements NativeScriptFactory {

@Override

public ExecutableScript newScript(@Nullable Map map) {

return new CustomScript(map);

}

@Override

public boolean needsScores() {

return false;

}

protected class CustomScript extends AbstractDoubleSearchScript {

//params 通过外部传入的参数方式进行排序干预

public CustomScript(@Nullable Map params) {

}

@Override

public double runAsDouble() {

//三种获取文档方式.

//((ScriptDocValues.Longs)doc().get("wordnumber")).getValue()

//(int)source().get("wordnumber");

//this.docFieldLongs("wordnumber");

double wordNumber;

double commentTime;

double useDate;

double numPicture;

double feedBack;

try {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String today = sdf.format(new Date());

if (source().get("wordnumber") == null) {

wordNumber = 0;

} else {

wordNumber = (int)source().get("wordnumber");

if (wordNumber >= 100) {

wordNumber = 1;

} else if (wordNumber >= 70) {

wordNumber = 0.9;

} else if (wordNumber >= 60) {

wordNumber = 0.8;

} else if (wordNumber >= 50) {

wordNumber = 0.7;

} else if (wordNumber >= 40) {

wordNumber = 0.6;

} else if (wordNumber >= 30) {

wordNumber = 0.5;

} else if (wordNumber >= 20) {

wordNumber = 0.4;

} else if (wordNumber >= 10) {

wordNumber = 0.3;

} else if (wordNumber >= 5) {

wordNumber = 0.2;

} else if (wordNumber >= 1) {

wordNumber = 0.1;

} else {

wordNumber = 0;

}

}

if (source().get("imgcount") == null) {

numPicture = 0;

} else {

numPicture = (int)source().get("imgcount");

if (numPicture >= 10) {

numPicture = 1;

} else if (numPicture >= 9) {

numPicture = 0.9;

} else if (numPicture >= 8) {

numPicture = 0.8;

} else if (numPicture >= 7) {

numPicture = 0.7;

} else if (numPicture >= 6) {

numPicture = 0.6;

} else if (numPicture >= 5) {

numPicture = 0.5;

} else if (numPicture >= 4) {

numPicture = 0.4;

} else if (numPicture >= 3) {

numPicture = 0.3;

} else if (numPicture >= 2) {

numPicture = 0.2;

} else if (numPicture >= 1) {

numPicture = 0.1;

} else {

numPicture = 0;

}

}

if (source().get("useful") == null) {

feedBack = 0;

} else {

feedBack = (int)source().get("useful");

if (feedBack >= 10) {

feedBack = 1;

} else if (feedBack >= 9) {

feedBack = 0.9;

} else if (feedBack >= 8) {

feedBack = 0.8;

} else if (feedBack >= 7) {

feedBack = 0.7;

} else if (feedBack >= 6) {

feedBack = 0.6;

} else if (feedBack >= 5) {

feedBack = 0.5;

} else if (feedBack >= 4) {

feedBack = 0.4;

} else if (feedBack >= 3) {

feedBack = 0.3;

} else if (feedBack >= 2) {

feedBack = 0.2;

} else if (feedBack >= 1) {

feedBack = 0.1;

} else {

feedBack = 0;

}

}

commentTime =source().get("cmtdate")==null?-1:(sdf.parse(today).getTime() - sdf.parse(source().get("cmtdate").toString()).getTime())/(24*60*60*1000);

if (commentTime >= 620) {

commentTime = 0.1;

} else if (commentTime >= 360) {

commentTime = 0.2;

} else if (commentTime >= 180) {

commentTime = 0.3;

} else if (commentTime >= 120) {

commentTime = 0.4;

} else if (commentTime >= 90) {

commentTime = 0.5;

} else if (commentTime >= 60) {

commentTime = 0.6;

} else if (commentTime >= 30) {

commentTime = 0.7;

} else if (commentTime >= 14) {

commentTime = 0.8;

} else if (commentTime >= 7) {

commentTime = 0.9;

} else if (commentTime >= 0) {

commentTime = 1;

} else {

commentTime = 0;

}

useDate =source().get("usedate")==null?-1: (sdf.parse(today).getTime() -  sdf.parse(source().get("usedate").toString()).getTime())/(24*60*60*1000);

if (useDate >= 620) {

useDate = 0.1;

} else if (useDate >= 360) {

useDate = 0.2;

} else if (useDate >= 180) {

useDate = 0.3;

} else if (useDate >= 120) {

useDate = 0.4;

} else if (useDate >= 90) {

useDate = 0.5;

} else if (useDate >= 60) {

useDate = 0.6;

} else if (useDate >= 30) {

useDate = 0.7;

} else if (useDate >= 14) {

useDate = 0.8;

} else if (useDate >= 7) {

useDate = 0.9;

} else if (useDate >= 0) {

useDate = 1;

} else {

useDate = 0;

}

double iw_wordNumber = 0.3;

double iw2_commentTime = 0.2;

double iw3_useDate = 0.2;

double iw4_numPicture = 0.15;

double iw5_feedBack = 0.15;

double sumW = iw_wordNumber + iw2_commentTime + iw3_useDate + iw4_numPicture + iw5_feedBack;

double sumScore = wordNumber * iw_wordNumber + commentTime * iw2_commentTime + useDate * iw3_useDate + numPicture * iw4_numPicture + feedBack * iw5_feedBack;

return (sumScore / sumW);

}catch (Exception ex){

ex.printStackTrace();

return -1;//this.docFieldLongs("wordnumber").getValue();

}

}

}

}

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
4月前
|
自然语言处理 大数据 应用服务中间件
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
101 5
|
5月前
|
数据可视化 Java Windows
Elasticsearch入门-环境安装ES和Kibana以及ES-Head可视化插件和浏览器插件es-client
本文介绍了如何在Windows环境下安装Elasticsearch(ES)、Elasticsearch Head可视化插件和Kibana,以及如何配置ES的跨域问题,确保Kibana能够连接到ES集群,并提供了安装过程中可能遇到的问题及其解决方案。
Elasticsearch入门-环境安装ES和Kibana以及ES-Head可视化插件和浏览器插件es-client
|
6月前
|
SQL JSON 大数据
ElasticSearch的简单介绍与使用【进阶检索】 实时搜索 | 分布式搜索 | 全文搜索 | 大数据处理 | 搜索过滤 | 搜索排序
这篇文章是Elasticsearch的进阶使用指南,涵盖了Search API的两种检索方式、Query DSL的基本语法和多种查询示例,包括全文检索、短语匹配、多字段匹配、复合查询、结果过滤、聚合操作以及Mapping的概念和操作,还讨论了Elasticsearch 7.x和8.x版本中type概念的变更和数据迁移的方法。
ElasticSearch的简单介绍与使用【进阶检索】 实时搜索 | 分布式搜索 | 全文搜索 | 大数据处理 | 搜索过滤 | 搜索排序
|
4月前
|
存储 JSON Java
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
这篇文章是关于Elasticsearch的学习指南,包括了解Elasticsearch、版本对应、安装运行Elasticsearch和Kibana、安装head插件和elasticsearch-ik分词器的步骤。
413 0
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
|
6月前
|
自然语言处理 应用服务中间件 nginx
一文教会你 分词器elasticsearch-analysis-ik 的安装使用【自定义分词库】
这篇文章是关于如何在Elasticsearch中安装和使用ik分词器的详细教程,包括版本匹配、安装步骤、分词测试、自定义词库配置以及创建使用ik分词器的索引的方法。
一文教会你 分词器elasticsearch-analysis-ik 的安装使用【自定义分词库】
|
5月前
|
存储 自然语言处理 关系型数据库
ElasticSearch基础3——聚合、补全、集群。黑马旅游检索高亮+自定义分词器+自动补全+前后端消息同步
聚合、补全、RabbitMQ消息同步、集群、脑裂问题、集群分布式存储、黑马旅游实现过滤和搜索补全功能
|
8月前
|
自然语言处理 搜索推荐
在Elasticsearch 7.9.2中安装IK分词器并进行自定义词典配置
在Elasticsearch 7.9.2中安装IK分词器并进行自定义词典配置
732 1
|
8月前
Elasticsearch自定义时间格式
Elasticsearch自定义时间格式
153 0
|
7月前
|
数据库
面试题ES问题之Elasticsearch的排序分页和高亮功能如何解决
面试题ES问题之Elasticsearch的排序分页和高亮功能如何解决
58 0
|
8月前
|
数据安全/隐私保护 网络架构 索引
Elasticsearch索引数据的路由规则与自定义路由分发
Elasticsearch索引数据的路由规则与自定义路由分发
143 0

热门文章

最新文章