Elastic 5.3 Native Script implementation

Source: Internet
Author: User
Tags deprecated static class log4j
Native (Java) Scripts

Elastic provides a rich sort, mostly based on TF/IDF computing score. Then sometimes the business needs a custom sort, which computes the score according to a rule, and then sorts it according to the score. There are two scenarios for implementing custom sorting at this time:
-Function Score
-Script
1.1 Groovy Scripts
1.2 Native Scripts

This article focuses on implementing Elatic custom ordering in the form of native scripts Plug-ins.

Note : The elastic version is updated more quickly and is implemented differently in different versions. Note the elastic version when referring to this article. Native scripts can be used normally in 5.0~5.4, is deprecated in version 5.5, and the 6.0 version is completely removed. Using the elastic 5.5 version requires the use of scriptengine.
-sometimes groovy and expression aren ' t enough. For those, you can implement a native script.
-Native Scripts were deprecated in v5.5.0 and removed in v6.0.0. Consider migrating your native scripts to the ScriptEngine. Custom Sort Implementation

Use the Scriptplugin plug-in to implement a simple sort:
Define a "feature" field, and the scoring rules for that field are set by ourselves. The rules are as follows:
-If the Query field feature is equal to the feature length of the queried field, the document being queried is scored 90
-If the query field feature length is smaller than the feature length of the queried field, the document being queried is scored 60
-If the query field feature length is larger than the feature length of the query field, the document being queried at this point Java code Implementation

Refer to the Native (Java) scripts Help documentation for the following code implementation:

Package com.zz.localservice.es.plugin;
Import Org.apache.logging.log4j.LogManager;
Import Org.apache.logging.log4j.Logger;
Import org.elasticsearch.common.Nullable;
Import org.elasticsearch.common.xcontent.support.XContentMapValues;
Import Org.elasticsearch.plugins.Plugin;
Import Org.elasticsearch.plugins.ScriptPlugin;
Import Org.elasticsearch.script.AbstractDoubleSearchScript;
Import Org.elasticsearch.script.ExecutableScript;

Import Org.elasticsearch.script.NativeScriptFactory;
Import java.util.Collections;
Import java.util.List;

Import Java.util.Map;
 /** * Created with IntelliJ idea. * user:smartfly2017 * DATE:2017/10/16 * time:15:53 * Description: * To change this template use File | Settings | File Templates | Includes |  File Header */public class Mynativescriptplugin extends Plugin implements scriptplugin{private final static Logger

    Logger = Logmanager.getlogger (Mynativescriptplugin.class); @Override Public list<nativescriptfactory> GETNATIVESCRIPTs () {return collections.singletonlist (new Mynativescriptfactory ()); public static class Mynativescriptfactory implements Nativescriptfactory {@Override public Executa Blescript Newscript (@Nullable map<string, object> params) {String feature = params = null? Null:xco
            Ntentmapvalues.nodestringvalue (Params.get ("feature"), null);
            if (feature = = null) {Logger.info ("feature is null!");
        Return to new Mynativescript (feature);
        @Override public boolean needsscores () {return false;
        @Override public String GetName () {return "My_script"; } public static class Mynativescript extends Abstractdoublesearchscript {private final String feature

        ;
        Public Mynativescript (String feature) {this.feature = feature; @Override public Double runasdouble() {String sourcefeature = (string) source (). Get ("feature");
            int len1 = Feature.length ();
            int len2 = Sourcefeature.length ();
            if (len1 = = len2) {return 90;
            else if (Len1 < len2) {return 60;
            else {return 30; }
        }
    }

}
configuration file

Because elastic all plug-ins must contain plugin-descriptor.properties files in the Elasticsearch folder.
Its plugin-descriptor.properties documents are as follows:

Description=${project.description}.
Version=${project.version}
Name=${project.artifactid}
classname= Com.zz.localservice.es.plugin.MyNativeScriptPlugin
java.version=1.8
elasticsearch.version=5.3.0

To ensure that the configuration files and jars are included in the Elasticsearch file, the Plugin.xml configuration file is configured with the following configuration:

<?xml version= "1.0"?>
<assembly>
    <id>plugin</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>
Compiling Packages

To ensure that the compilation is packaged correctly, you need to configure the Pom file. Its pom.xml file is configured as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "HTT" P://www.w3.org/2001/xmlschema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 Http://maven.apach E.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupid>com.zz.localservice .ai</groupid> <artifactId>es-sort</artifactId> <version>1.0-SNAPSHOT</version> &
        Lt;name>plugin:basic</name> <description>only for test</description> <properties> <es.version>5.3.0</es.version> <lucene.version>6.4.1</lucene.version> </proper 
            ties> <dependencies> <dependency> <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId> <version>${es.version}</version> <scope>prOvided</scope> </dependency> <!--testing--> <dependency> &L
            T;groupid>org.apache.logging.log4j</groupid> <artifactId>log4j-api</artifactId>
        <version>2.7</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactid>log4j
        -core</artifactid> <version>2.7</version> <scope>test</scope>
            </dependency> <dependency> <groupId>org.elasticsearch.test</groupId> <artifactId>framework</artifactId> <version>${es.version}</version> & lt;scope>test</scope> </dependency> <dependency> <groupid>org.apac He.lucene</groupid>
            <artifactId>lucene-test-framework</artifactId> <version>${lucene.version}</version>

        <scope>test</scope> </dependency> </dependencies> <build>
                <resources> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <excludes> &LT;EXCLUDE&G

        t;*.properties</exclude> </excludes> </resource> </resources> <plugins> <plugin> <artifactid>maven-assembly-plugin</artifactid > <version>2.3</version> <configuration> <appe Ndassemblyid>false</appendassemblyid> <outputdirectory>${project.build.directory}/relea ses/</oUtputdirectory> <descriptors> <descriptor>${basedir}/src/main/a
                Ssemblies/plugin.xml</descriptor> </descriptors> </configuration> <executions> <execution> <phase>package</ph
                        Ase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin&

            Gt <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>m Aven-compiler-plugin</artifactid> <version>3.7.0</version> <configura
              Tion> <source>1.8</source> <target>1.8</target>  </configuration> </plugin> </plugins> </build> </project> 

Using the same package as the generic MAVEN project, use the following command:

MVN clean Install

After the package is complete, the plug-in es-sort-1.0-snapshot.zip the file installation in the/target/releases/directory plugin

When we plugin Java, we need to install the plugin. The plugin is installed here just like any other plugin. Use Bin/elasticsearch-plugin install File:///path/to/your/plugin.

Note :

To enable elastic to support dynamic plug-ins, you need to add the following configuration to the Elasticsearch.yml profile:

Script.inline:true
Script.stored:true

If you do not add this configuration, the following error will be reported in the subsequent test:

Failed to compile inline script [my_script] using lang [native]
Custom Sort Tests to create an index for a document
Put my_index_test
{  
  "mappings": {"My_type": {"Properties": {"  
         feature": {"  
          type":  " Keyword ",  
           " index ":" Not_analyzed "  
        },  
        " tag ": {  
          " type ":  " keyword "  
        },  
        " TestName ": {  
          ' type ':  ' text '}}}  
Add Data
PUT/MY_INDEX_TEST/MY_TYPE/1
{"  
  feature": "ABC",  
  "tag": "Mytagabc",  
  "testname": "Hello World"  
}

PUT/MY_INDEX_TEST/MY_TYPE/2
{"  
  feature": "123456",  
  "tag": "2mytagabc",  
  "testname": "2Hello World "  
}

put/my_index_test/my_type/3
{"  
  feature ":" Def789kkk ",  
  " tag ":" 3mytagabc ", "  
  testname": "3Hello World"  
Search Sort Test
Post/my_index_test/my_type/_search?pretty {"Query": {"Function_score": {"  
  query": {  
       "Match_ All ': {}  
      },  
      ' Functions ': [  
        {'  
          script_score ': {'  
            script ': {' inline ': ' My_script '  
                ,  
                ' Lang ': ' Native ',  
                ' params ':   
                {  
                  ' feature ': ' aaaaaa '  
      }  
    }  
  }}}  

Test results are:

{"Took": "Timed_out": false, "_shards": {"Total": 5, "successful": 5, "failed": 0}, "h It ": {" Total ": 3," Max_score ": 90.0," hits ": [{" _index ":" My_index_test "," _typ "]
          E ":" My_type "," _id ":" 2 "," _score ": 90.0," _source ": {" feature ":" 123456 ", "Tag": "2mytagabc", "testname": "2Hello World"}}, {"_index": "My_index_tes  T "," _type ":" My_type "," _id ":" 3 "," _score ": 60.0," _source ": {" feature ": "Def789kkk", "tag": "3mytagabc", "testname": "3Hello World"}}, {"_in
          Dex ":" My_index_test "," _type ":" My_type "," _id ":" 1 "," _score ": 30.0," _source ": { 
"Feature": "abc", "Tag": "Mytagabc", "testname": "Hello World"}} }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.