2

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

4
  • You need to paste the class file.. and field definition too.. Commented May 21, 2020 at 9:53
  • @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 field Commented May 21, 2020 at 10:15
  • There is some issue in the class definition it seems.. Commented May 21, 2020 at 10:26
  • @hacker315....edited question with class defination Commented May 21, 2020 at 10:44

1 Answer 1

0

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" /> 
Sign up to request clarification or add additional context in comments.

3 Comments

@Override is only relevant as a decorator that gives an error if there is no method to override; extending the class doesn't require it - it's only an extra failsafe to verify that you're not "overriding" a non-existing method by error. The Solr log should also show the name of all the jar files that are being loaded.
@hacker315.....actually after changing regex file path previous error resolved but now I am getting another error i.e "SchemaSimilarityFactory can not be used until SolrCoreAware.inform has been called"
@PujaAlagi Kindly create a new question for it. and tick mark the correct answer, if it helped..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.