最近项目中遇到需要支持对搜索结果根据复杂权限规则来进行过滤,网上没有找到直接相关的资料,最后去官方文档找到了答案. 有时间还是多读读官方文档
使用query script可以进行自定义查询和过滤,ElasticSearch5.5以前默认使用groovy脚本,5.5之后默认使用painless脚本.我们用的ElasticSearch是2.4版本,ElasticSearch默认支持groovy脚本,groovy性能较低,官方建议使用groovy用于开发,生产建议使用nativeScript,也就是使用java编写plugin.
接下来说一下ElasticSearch2.4版本怎么写自定义过滤插件,其他版本基本上一样.
插件编写
官方演示代码
1. 插件项目结构
project
--src
----main
--------assemblies
------------plugin.xml
--------java
------------org.elasticsearch
------------plugin
----------------MyFilterScriptPlugin.java
----------------script
----------------FilterScript.java
--------resources
------------es-plugin.properties
----test
--pom.xml
2. 插件信息
public class GroupIdFilterScriptPlugin extends Plugin {
@Override
public String name() {
return "MyFilter";
}
@Override
public String description() {
return "the MyFilter native script use to handle complex Filter";
}
public void onModule(ScriptModule scriptModule) {
scriptModule.registerScript("GroupIdsFilter", MyNativeScriptFactory.class);
}
public static class MyNativeScriptFactory implements NativeScriptFactory {
@Override
public ExecutableScript newScript(@Nullable Map<String, Object> params) {
return new FilterScript(params);
}
@Override
public boolean needsScores() {
return false;
}
}
}
3. 插件script代码
public class FilterScript extends AbstractSearchScript {
private Map<String, Object> params;
public FilterScript(@Nullable Map<String, Object> params) {
this.params = params;
}
@Override
public Object run() {
List<String> docValue = null;
if (doc().get("userGroupIds") != null) {
docValue = ((ScriptDocValues.Strings) doc().get("userGroupIds")).getValues();
}
if (docValue != null && docValue.size() > 0) {
if (params.get("userGroupIds") != null) {
return true;
}
return false;
}
return true;//返回值作为Filter的判断值,返回true,该文档命中
}
}
4. POM文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Infinitus-search</groupId>
<artifactId>GroupIdsFilter</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<elasticsearch.version>2.4.0</elasticsearch.version>
<elasticsearch.java.version>1.8</elasticsearch.java.version>
<elasticsearch.plugin.classname>org.elasticsearch.plugin.GroupIdFilterScriptPlugin
</elasticsearch.plugin.classname>
<elasticsearch.plugin.name>GroupIdsFilter</elasticsearch.plugin.name>
<elasticsearch.plugin.jvm>true</elasticsearch.plugin.jvm>
<maven.compiler.target>1.8</maven.compiler.target>
<tests.rest.load_packaged>false</tests.rest.load_packaged>
<skip.unit.tests>true</skip.unit.tests>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.4.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>*.properties</exclude>
</excludes>
</resource>
</resources>
<plugins>
<!-- ============================================================= -->
<!-- Elasticsearch is using java 1.8 at the moment. -->
<!-- ============================================================= -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- ============================================================= -->
<!-- This plugin assembles plugin .zip package. -->
<!-- ============================================================= -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<outputDirectory>${project.build.directory}/releases/</outputDirectory>
<descriptors>
<descriptor>${basedir}/src/main/assemblies/plugin.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
5. es-plugin.properties
description=${project.description}
version=${project.version}
name=${elasticsearch.plugin.name}
jvm=true
classname=${elasticsearch.plugin.classname}
elasticsearch.version=${elasticsearch.version}
java.version=1.8
6. plugin.xml
<?xml version="1.0"?>
<assembly>
<id>GroupIdFilterScriptPlugin_release</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>${project.basedir}/src/main/resources/plugin-descriptor.properties</source>
<outputDirectory>elasticsearch</outputDirectory>
<filtered>true</filtered>
</file>
</files>
<dependencySets>
<dependencySet>
<outputDirectory>elasticsearch</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<useTransitiveFiltering>true</useTransitiveFiltering>
</dependencySet>
</dependencySets>
</assembly>
7. 打包
pom文件中配置了maven的打包方式,使用maven打包,mvn clean complie install,在target\releases文件夹下,找到打包后的zip文件
<outputDirectory>${project.build.directory}/releases/</outputDirectory>
8. 插件安装
bin/plugin install file:../GroupIdsFilter-1.0-SNAPSHOT.zip //使用指定文件安装插件
bin/plugin remove GroupIdsFilter //使用插件名卸载插件
9. DSL调用自定义过滤插件
http://localhost:9200/index/type/
_search POST
{
"query": {
"bool": {
"filter": {
"script": {
"script": {
"inline": "GroupIdsFilter",
"lang": "native",
"params": {
"ids": "3505,1234"
}
}
}
}
}
}
}
10. Java API调用
BoolQueryBuilder filter = QueryBuilders.boolQuery();
Map<String, Object> params = new HashMap<>();
filter.should(QueryBuilders.scriptQuery(new Script("GroupIdsFilter","native", params)));
SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(filter).build();