2
votes

I need to sort SOLR search results based on position of search query.For example I have 4 documents

1.demo of solr lucene

2.lucene focuses mainly on text indexing

3.explain lucene with example

4.lucene is an open source

when I will search with query text lucene then I need result in following order

2.lucene focuses mainly on text indexing

4.lucene is an open source

3.explain lucene with example

1.demo of solr lucene

i.e. boost search query in first and second position higher than other

Following is field defination I have used on which I need position based sort

*<fieldType name="text_general" class="solr.TextField" positionIncrementGap="100" multiValued="true">
    <analyzer type="index">
        <charFilter class="solr.MappingCharFilterFactory" mapping="mapping-ISOLatin1Accent.txt"/>
        <tokenizer class="solr.WhitespaceTokenizerFactory"/>
        <filter class="solr.StopFilterFactory" words="stopwords.txt" ignoreCase="true"/>
        <filter class="solr.WordDelimiterGraphFilterFactory" catenateNumbers="1" generateNumberParts="1" protected="protwords.txt" splitOnCaseChange="1" generateWordParts="0" preserveOriginal="1" catenateAll="0" catenateWords="1"/>
        <filter class="solr.LowerCaseFilterFactory"/>
    </analyzer>
    <analyzer type="query">
        <charFilter class="solr.MappingCharFilterFactory" mapping="mapping-ISOLatin1Accent.txt"/>        
        <tokenizer class="solr.WhitespaceTokenizerFactory"/>
        <filter class="solr.SynonymFilterFactory" expand="true" ignoreCase="true" synonyms="synonyms.txt"/>
        <filter class="solr.StopFilterFactory" words="stopwords.txt" ignoreCase="true"/>
        <filter class="solr.WordDelimiterGraphFilterFactory" catenateNumbers="1" generateNumberParts="1" protected="protwords.txt" splitOnCaseChange="1" generateWordParts="0" preserveOriginal="1" catenateAll="0" catenateWords="1"/>
        <filter class="solr.LowerCaseFilterFactory"/>
    </analyzer>
   <similarity class="CustomSimilarity" />
</fieldType>*

for this I have tried to disable termfrequency by adding customSimilarity class in solr but I am getting error as

Plugin init failure for [schema.xml] fieldType : Error loading class

Following is the class defination for CustomSimilarity classs

import org.apache.lucene.analysis.payloads.PayloadHelper;
import org.apache.lucene.search.similarities.DefaultSimilarity;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.util.BytesRef;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.schema.SimilarityFactory;
import org.apache.solr.search.similarities.SchemaSimilarityFactory;


public class CustomSimilarity extends SchemaSimilarityFactory{

    public float tf(float freq) {
        return 1.0f;
    }

}

And jar file path in solrconfig

  <lib dir="${solr.install.dir:../../../..}/dist/"  regex=".*.jar" /> 

Any help will be appriciated

1
You need to paste the class file.. and field definition too..hacker315
@hacker315..... I have generated jar file for the custom class and provided path in solr config and used that class as similarity class in managed schema for the fieldPuja Alagi
There is some issue in the class definition it seems..hacker315
@hacker315....edited question with class definationPuja Alagi

1 Answers

0
votes

There are two things.

1) You forgot to write @Override, so that your definition of tf is picked up.

2) In specifying jar path, regex has to be ".*.jar"

import org.apache.lucene.analysis.payloads.PayloadHelper;
import org.apache.lucene.search.similarities.DefaultSimilarity;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.util.BytesRef;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.schema.SimilarityFactory;
import org.apache.solr.search.similarities.SchemaSimilarityFactory;


public class CustomSimilarity extends SchemaSimilarityFactory{

    @Override
    public float tf(float freq) {
        return 1.0f;
    }

}

Regex:

<lib dir="${solr.install.dir:../../../..}/dist/"  regex=".*\.jar" />