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可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
6月前
|
人工智能 数据可视化 Java
ElasticSearch安装、插件介绍及Kibana的安装与使用详解
ElasticSearch安装、插件介绍及Kibana的安装与使用详解
ElasticSearch安装、插件介绍及Kibana的安装与使用详解
|
4月前
|
安全 大数据 Java
elasticsearch|大数据|低版本的elasticsearch集群的官方安全插件x-pack的详解
elasticsearch|大数据|低版本的elasticsearch集群的官方安全插件x-pack的详解
53 0
|
9月前
|
安全 Java 关系型数据库
elasticsearch安装dynamic-synonym插件
如何快速掌握Elasticsearch8.x同义词插件的使用,本文将层层深入带你从0到1理解ES的插件开发
elasticsearch安装dynamic-synonym插件
|
2月前
|
测试技术 定位技术 API
万字长文:一文彻底搞懂Elasticsearch中Geo数据类型查询、聚合、排序
万字长文:一文彻底搞懂Elasticsearch中Geo数据类型查询、聚合、排序
94607 140
|
3月前
|
存储 自然语言处理 Java
SpringBoot集成ElasticSearch时分页排序查询时遇到的坑每次只能返回10条数据
SpringBoot集成ElasticSearch时分页排序查询时遇到的坑每次只能返回10条数据
54 0
|
8月前
|
自然语言处理 Java Maven
Elasticsearch系列——安装中文分词插件elasticsearch-analysis-ik
Elasticsearch系列——安装中文分词插件elasticsearch-analysis-ik
|
4月前
|
自然语言处理 Java 关系型数据库
Elasticsearch【环境搭建 01】elasticsearch-6.4.3 单机版不能以root用户运行es 及 max_map_count 问题解决(含 安装包+分词插件 云盘资源)
Elasticsearch【环境搭建 01】elasticsearch-6.4.3 单机版不能以root用户运行es 及 max_map_count 问题解决(含 安装包+分词插件 云盘资源)
31 0
|
8月前
|
SQL AliSQL API
阿里云ElasticSearch安装开源插件实践
阿里云ElasticSearch安装开源插件实践,包含较为常用的NLPchina/elasticsearch-sql与medcl/elasticsearch-analysis-stconvert两个插件的安装、使用测试及同类插件的差异对比
|
8月前
|
存储 自然语言处理 搜索推荐
Elasticsearch插件管理(ik分词器、附件文本抽取插件)
Elasticsearch插件管理(ik分词器、附件文本抽取插件)
188 0
|
11月前
|
算法 索引
Elasticsearch - 聚合获取原始数据并分页&排序&模糊查询
Elasticsearch - 聚合获取原始数据并分页&排序&模糊查询
215 0
Elasticsearch - 聚合获取原始数据并分页&排序&模糊查询

热门文章

最新文章