Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

Exposed support for stop words facility through LikeThisQueryBuilder and updated example LikeThisQuery.xml to illustrate usage

Mark Harwood69299c5663ab
diff --git a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/LikeThisQueryBuilder.java b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/LikeThisQueryBuilder.java
index 18a46945f36..9c881b7c61b 100644
--- a/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/LikeThisQueryBuilder.java
+++ b/contrib/xml-query-parser/src/java/org/apache/lucene/xmlparser/builders/LikeThisQueryBuilder.java
@@ -3,7 +3,14 @@
*/
package org.apache.lucene.xmlparser.builders;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.HashSet;
+import java.util.Set;
+
import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.Token;
+import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.search.similar.MoreLikeThisQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.xmlparser.DOMUtils;
@@ -43,10 +50,41 @@ public class LikeThisQueryBuilder implements QueryBuilder {
fields[i]=fields[i].trim();
}
}
+
+ //Parse any "stopWords" attribute
+ //TODO MoreLikeThis needs to ideally have per-field stopWords lists - until then
+ //I use all analyzers/fields to generate multi-field compatible stop list
+ String stopWords=e.getAttribute("stopWords");
+ Set stopWordsSet=null;
+ if((stopWords!=null)&&(fields!=null))
+ {
+ stopWordsSet=new HashSet();
+ for (int i = 0; i < fields.length; i++)
+ {
+ TokenStream ts = analyzer.tokenStream(fields[i],new StringReader(stopWords));
+ try
+ {
+ Token stopToken=ts.next();
+ while(stopToken!=null)
+ {
+ stopWordsSet.add(stopToken.termText());
+ stopToken=ts.next();
+ }
+ }
+ catch(IOException ioe)
+ {
+ throw new ParserException("IoException parsing stop words list in "
+ +getClass().getName()+":"+ioe.getLocalizedMessage());
+ }
+ }
+ }
+
+
MoreLikeThisQuery mlt=new MoreLikeThisQuery(DOMUtils.getText(e),fields,analyzer);
mlt.setMaxQueryTerms(DOMUtils.getAttribute(e,"maxQueryTerms",defaultMaxQueryTerms));
mlt.setMinTermFrequency(DOMUtils.getAttribute(e,"minTermFrequency",defaultMinTermFrequency));
mlt.setPercentTermsToMatch(DOMUtils.getAttribute(e,"percentTermsToMatch",defaultPercentTermsToMatch)/100);
+ mlt.setStopWords(stopWordsSet);
mlt.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
diff --git a/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/LikeThisQuery.xml b/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/LikeThisQuery.xml
index 10a883b3d49..0c121676a1c 100644
--- a/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/LikeThisQuery.xml
+++ b/contrib/xml-query-parser/src/test/org/apache/lucene/xmlparser/LikeThisQuery.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
-<LikeThisQuery percentTermsToMatch="5">
+<LikeThisQuery percentTermsToMatch="5" stopWords="Reuter">
IRAQI TROOPS REPORTED PUSHING BACK IRANIANS Iraq said today its troops were pushing Iranian forces out of
positions they had initially occupied when they launched a new offensive near the southern port of
Basra early yesterday. A High Command communique said Iraqi troops had won a significant victory