elasticsearch 自定义similarity 插件开发

在搜索开发中,我们要修改打分机制,就需要自定义similarity。现在来简单说一下elasticsearch下的自定义similarity 插件开发。

网上的https://github.com/tlrx/elasticsearch-custom-similarity-provider仅仅支持0.20.0.Beta1-SNAPSHOT版本,现在我们用的版本是elasticsearch 0.90版本以上。那个例子现在不能用,我修改了一下。

1.继承DefaultSimilarity,实现自己的搜索打分机制。

package org.elasticsearch.index.similarity;
 
import org.apache.lucene.search.similarities.DefaultSimilarity;
 
/**
 * Custom similarity class
 * 
 * @author xq
 *
 */
public class CustomSimilarity extends DefaultSimilarity {
 
@Override
public float idf(long docFreq, long numDocs) {
return 1.0f;
}
 
}

2.继续AbstractSimilarityProvider,把自定义的打分机制类加载到elasticsearch中

package org.elasticsearch.index.similarity;
 
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.assistedinject.Assisted;
import org.elasticsearch.common.settings.Settings;
 
/**
 * Simple {@link SimilarityProvider} for a {@link CustomSimilarity}
 * 
 * @author xq
 *
 */
public class CustomSimilarityProvider extends AbstractSimilarityProvider {
 
private CustomSimilarity similarity;
 
@Inject
public CustomSimilarityProvider(@Assisted String name, @Assisted Settings settings) {
super(name);
this.similarity = new CustomSimilarity();
}
 
 
 
public CustomSimilarity get() {
return similarity;
}
}

3.继承AbstractPlugin作为elasticsearch插件使用

public class CustomerSimilarityPlugin extends AbstractPlugin {
 
    @Override public String name() {
        return "customer-similarity";
    }
 
 
    @Override public String description() {
        return "customer similarity";
    }
 
 
    @Override public void processModule(Module module) {
        if (module instanceof SimilarityModule) {
        SimilarityModule similarityModule = (SimilarityModule) module;
        similarityModule.addSimilarity("customer-similarity", CustomSimilarityProvider.class);
        }
    }
}

4.使用

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

在创建mapping的使用自定义的打分规则:

{
  "news" : {
    "properties" : {
      "title" : { "type" : "string", "similarity" : "my_similarity" }
    }
}

在elasticsearch.yml中配置自定义的打分规则类为默认规则。
index.similarity.default.type: my_similarity

相关程序已经放在https://github.com/awnuxkjy/es-custom-similarity-provider,有兴趣的朋友可以参考一下。

把程序打成jar包放在elasticsearch 的plugins 下的 similarity 目录下即可
参考文章:
http://www.elasticsearch.org/guide/reference/index-modules/similarity/

http://www.chepoo.com/elasticsearcharticles

http://www.chepoo.com/lucene-solr-articles

你可能感兴趣的:(elasticsearch)