Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated for ES 1.7.0, added a better example #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions README.textile
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
h1. A custom SimilarityProvider example for Elasticseach

This plugin contains a custom @Similarity@ class that can be used in an index:
This plugin contains a custom @Similarity@ class that can be used as follows.

Build the plugin with maven, then put the jar file into @$ES_PATH/lib@.

Then, declare it in the settings section of your index:

<pre>
curl -XPOST 'http://host:port/tweeter/' -d '
{
"settings": {
"similarity": {
"index": {
"type": "org.elasticsearch.index.similarity.CustomSimilarityProvider"
},
"search": {
"type": "org.elasticsearch.index.similarity.CustomSimilarityProvider"
}
"settings" : {
"similarity" : {
"my_similarity" : {
"type" : "org.elasticsearch.index.similarity.CustomSimilarityProvider"
}
}
}'
}
</pre>

After that, you can refer to @my_similarity@ when defining fields:

<pre>
"book" : {
"properties" : {
"title" : { "type" : "string", "similarity" : "my_similarity" }
}
}
</pre>

Enjoy
Expand Down
25 changes: 19 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@
<modelVersion>4.0.0</modelVersion>

<groupId>fr.tlrx</groupId>
<artifactId>elasticsearch-custom-similarity-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<artifactId>elasticsearch-constant-similarity-provider</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>A custom SimilarityProvider example for Elasticsearch</name>
<url>https://github.com/tlrx/elasticsearch-custom-similarity-provider</url>
<name>A constant SimilarityProvider for Elasticsearch</name>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<elasticsearch.version>0.20.0.Beta1-SNAPSHOT</elasticsearch.version>
<elasticsearch.version>1.7.0</elasticsearch.version>
</properties>

<dependencies>
Expand All @@ -27,5 +40,5 @@
<version>${elasticsearch.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.elasticsearch.index.similarity;

import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.similarities.DefaultSimilarity;

/**
* Custom similarity class
Expand All @@ -12,7 +12,7 @@
public class CustomSimilarity extends DefaultSimilarity {

@Override
public float idf(int docFreq, int numDocs) {
public float idf(long docFreq, long numDocs) {
return 1.0f;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@
* @author tlrx
*
*/
public class CustomSimilarityProvider extends AbstractSimilarityProvider<CustomSimilarity> {
public class CustomSimilarityProvider extends AbstractSimilarityProvider {

private CustomSimilarity similarity;
private final CustomSimilarity similarity = new CustomSimilarity();

@Inject
public CustomSimilarityProvider(Index index,
@IndexSettings Settings indexSettings, @Assisted String name,
@Assisted Settings settings) {
super(index, indexSettings, name);
this.similarity = new CustomSimilarity();
public CustomSimilarityProvider(@Assisted String name, @Assisted Settings settings) {
super(name);
}

@Override
public CustomSimilarity get() {
return similarity;
}
Expand Down