From 59313cd115b2676a8b532d072433cd928b1264b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Thu, 21 Jun 2012 23:40:05 +0000 Subject: [PATCH] SOLR-3351: eDismax: ps2 and ps3 params git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1352726 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 2 + .../search/ExtendedDismaxQParserPlugin.java | 26 ++++++------ .../org/apache/solr/search/FieldParams.java | 2 +- .../org/apache/solr/util/SolrPluginUtils.java | 5 ++- .../solr/search/TestExtendedDismaxParser.java | 41 +++++++++++++++++++ .../solr/common/params/DisMaxParams.java | 12 ++++++ 6 files changed, 72 insertions(+), 16 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 9d9358849e1..d406ecbb442 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -366,6 +366,8 @@ New Features to schema.xml's declaration but as an update processor that can be combined with other processors in any order. (Jan Høydahl & hossman) +* SOLR-3351: eDismax: ps2 and ps3 params (janhoy) + * SOLR-3542: Add WeightedFragListBuilder for FVH and set it to default fragListBuilder in example solrconfig.xml. (Sebastian Lutze, koji) diff --git a/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java b/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java index 28c44012cd2..5b28def029b 100755 --- a/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java +++ b/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java @@ -15,11 +15,6 @@ * limitations under the License. */ -/* - * This parser was originally derived from DismaxQParser from Solr. - * All changes are Copyright 2008, Lucid Imagination, Inc. - */ - package org.apache.solr.search; import java.util.ArrayList; @@ -52,7 +47,8 @@ import org.apache.solr.schema.FieldType; import org.apache.solr.util.SolrPluginUtils; /** - * An advanced multi-field query parser. + * An advanced multi-field query parser based on the DisMax parser. + * See Wiki page http://wiki.apache.org/solr/ExtendedDisMax * @lucene.experimental */ public class ExtendedDismaxQParserPlugin extends QParserPlugin { @@ -134,21 +130,26 @@ class ExtendedDismaxQParser extends QParser { userFields = new UserFields(U.parseFieldBoosts(solrParams.getParams(DMP.UF))); queryFields = DisMaxQParser.parseQueryFields(req.getSchema(), solrParams); + + // Phrase slop array + int pslop[] = new int[4]; + pslop[0] = solrParams.getInt(DisMaxParams.PS, 0); + pslop[2] = solrParams.getInt(DisMaxParams.PS2, pslop[0]); + pslop[3] = solrParams.getInt(DisMaxParams.PS3, pslop[0]); + // Boosted phrase of the full query string List phraseFields = - U.parseFieldBoostsAndSlop(solrParams.getParams(DMP.PF),0); + U.parseFieldBoostsAndSlop(solrParams.getParams(DMP.PF),0,pslop[0]); // Boosted Bi-Term Shingles from the query string List phraseFields2 = - U.parseFieldBoostsAndSlop(solrParams.getParams("pf2"),2); + U.parseFieldBoostsAndSlop(solrParams.getParams(DMP.PF2),2,pslop[2]); // Boosted Tri-Term Shingles from the query string List phraseFields3 = - U.parseFieldBoostsAndSlop(solrParams.getParams("pf3"),3); - + U.parseFieldBoostsAndSlop(solrParams.getParams(DMP.PF3),3,pslop[3]); float tiebreaker = solrParams.getFloat(DisMaxParams.TIE, 0.0f); - int pslop = solrParams.getInt(DisMaxParams.PS, 0); int qslop = solrParams.getInt(DisMaxParams.QS, 0); // remove stopwords from mandatory "matching" component? @@ -332,11 +333,10 @@ class ExtendedDismaxQParser extends QParser { // full phrase and shingles for (FieldParams phraseField: allPhraseFields) { - int slop = (phraseField.getSlop() == 0) ? pslop : phraseField.getSlop(); Map pf = new HashMap(1); pf.put(phraseField.getField(),phraseField.getBoost()); addShingledPhraseQueries(query, normalClauses, pf, - phraseField.getWordGrams(),tiebreaker, slop); + phraseField.getWordGrams(),tiebreaker, phraseField.getSlop()); } } diff --git a/solr/core/src/java/org/apache/solr/search/FieldParams.java b/solr/core/src/java/org/apache/solr/search/FieldParams.java index 21352fe64f0..728369c4328 100644 --- a/solr/core/src/java/org/apache/solr/search/FieldParams.java +++ b/solr/core/src/java/org/apache/solr/search/FieldParams.java @@ -21,7 +21,7 @@ package org.apache.solr.search; **/ public class FieldParams { private final int wordGrams; // make bigrams if 2, trigrams if 3, or all if 0 - private final int slop; // null defaults to ps parameter + private final int slop; private final float boost; private final String field; public FieldParams(String field, int wordGrams, int slop, float boost) { diff --git a/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java b/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java index 935be6afdae..3b35ba9776d 100644 --- a/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java +++ b/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java @@ -434,9 +434,10 @@ public class SolrPluginUtils { * * @param fieldLists - an array of Strings eg. {"fieldOne^2.3", "fieldTwo", fieldThree~5^-0.4} * @param wordGrams - (0=all words, 2,3 = shingle size) + * @param defaultSlop - the default slop for this param * @return - FieldParams containing the fieldname,boost,slop,and shingle size */ - public static List parseFieldBoostsAndSlop(String[] fieldLists,int wordGrams) { + public static List parseFieldBoostsAndSlop(String[] fieldLists,int wordGrams,int defaultSlop) { if (null == fieldLists || 0 == fieldLists.length) { return new ArrayList(); } @@ -454,7 +455,7 @@ public class SolrPluginUtils { String[] fieldAndSlopVsBoost = caratPattern.split(s); String[] fieldVsSlop = tildePattern.split(fieldAndSlopVsBoost[0]); String field = fieldVsSlop[0]; - int slop = (2 == fieldVsSlop.length) ? Integer.valueOf(fieldVsSlop[1]) : 0; + int slop = (2 == fieldVsSlop.length) ? Integer.valueOf(fieldVsSlop[1]) : defaultSlop; Float boost = (1 == fieldAndSlopVsBoost.length) ? 1 : Float.valueOf(fieldAndSlopVsBoost[1]); FieldParams fp = new FieldParams(field,wordGrams,slop,boost); out.add(fp); diff --git a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java index d7d60177dd0..bea40f44e70 100755 --- a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java +++ b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java @@ -659,5 +659,46 @@ public class TestExtendedDismaxParser extends AbstractSolrTestCase { "//str[@name='parsedquery'][contains(.,'phrase_sw:\"zzzz xxxx cccc\"~3^333.0')]", "//str[@name='parsedquery'][contains(.,'phrase_sw:\"xxxx cccc vvvv\"~3^333.0')]" ); + + assertQ( + "ps2 not working", + req("q", "bar foo", "qf", "phrase_sw", "pf2", "phrase_sw^10", "ps2", + "2", "bf", "boost_d", "fl", "score,*", "defType", "edismax"), + "//doc[1]/str[@name='id'][.='s0']"); + + assertQ( + "Specifying slop in pf2 param not working", + req("q", "bar foo", "qf", "phrase_sw", "pf2", "phrase_sw~2^10", "bf", + "boost_d", "fl", "score,*", "defType", "edismax"), + "//doc[1]/str[@name='id'][.='s0']"); + + assertQ( + "Slop in ps2 parameter should override ps", + req("q", "bar foo", "qf", "phrase_sw", "pf2", "phrase_sw^10", "ps", + "0", "ps2", "2", "bf", "boost_d", "fl", "score,*", "defType", + "edismax"), "//doc[1]/str[@name='id'][.='s0']"); + + assertQ( + "ps3 not working", + req("q", "a bar foo", "qf", "phrase_sw", "pf3", "phrase_sw^10", "ps3", + "3", "bf", "boost_d", "fl", "score,*", "defType", "edismax"), + "//doc[1]/str[@name='id'][.='s1']"); + + assertQ( + "Specifying slop in pf3 param not working", + req("q", "a bar foo", "qf", "phrase_sw", "pf3", "phrase_sw~3^10", "bf", + "boost_d", "fl", "score,*", "defType", "edismax"), + "//doc[1]/str[@name='id'][.='s1']"); + + assertQ("ps2 should not override slop specified inline in pf2", + req("q", "zzzz xxxx cccc vvvv", + "qf", "phrase_sw", + "pf2", "phrase_sw~2^22", + "ps2", "4", + "defType", "edismax", + "debugQuery", "true"), + "//str[@name='parsedquery'][contains(.,'phrase_sw:\"zzzz xxxx\"~2^22.0')]" + ); + } } diff --git a/solr/solrj/src/java/org/apache/solr/common/params/DisMaxParams.java b/solr/solrj/src/java/org/apache/solr/common/params/DisMaxParams.java index 9a29dd3818f..c2c268e2875 100755 --- a/solr/solrj/src/java/org/apache/solr/common/params/DisMaxParams.java +++ b/solr/solrj/src/java/org/apache/solr/common/params/DisMaxParams.java @@ -34,6 +34,12 @@ public interface DisMaxParams { /** query and init param for phrase boost fields */ public static String PF = "pf"; + /** query and init param for bigram phrase boost fields */ + public static String PF2 = "pf2"; + + /** query and init param for trigram phrase boost fields */ + public static String PF3 = "pf3"; + /** query and init param for MinShouldMatch specification */ public static String MM = "mm"; @@ -43,6 +49,12 @@ public interface DisMaxParams { */ public static String PS = "ps"; + /** default phrase slop for bigram phrases (pf2) */ + public static String PS2 = "ps2"; + + /** default phrase slop for bigram phrases (pf3) */ + public static String PS3 = "ps3"; + /** * query and init param for phrase Slop value in phrases * explicitly included in the user's query string ( in qf fields)