Apache Lucene open-source search software
LUCENE-4113: FVH: FieldTermStack.TermInfo should provide term-weight, part of LUCENE-3440
Koji Sekiguchi2fc08cec9eff
diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldPhraseList.java b/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldPhraseList.javaindex 00fa187ceb3..84227391a9c 100644--- a/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldPhraseList.java+++ b/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldPhraseList.java@@ -105,12 +105,17 @@ public class FieldPhraseList {}}}-+public void addIfNoOverlap( WeightedPhraseInfo wpi ){- for( WeightedPhraseInfo existWpi : phraseList ){- if( existWpi.isOffsetOverlap( wpi ) ) return;+ for( WeightedPhraseInfo existWpi : getPhraseList() ){+ if( existWpi.isOffsetOverlap( wpi ) ) {+ // WeightedPhraseInfo.addIfNoOverlap() dumps the second part of, for example, hyphenated words (social-economics).+ // The result is that all informations in TermInfo are lost and not available for further operations.+ existWpi.getTermsInfos().addAll( wpi.getTermsInfos() );+ return;+ }}- phraseList.add( wpi );+ getPhraseList().add( wpi );}public static class WeightedPhraseInfo {@@ -121,6 +126,8 @@ public class FieldPhraseList {private float boost; // query boostprivate int seqnum;+ private ArrayList<TermInfo> termsInfos;+/*** @return the text*/@@ -142,6 +149,13 @@ public class FieldPhraseList {return boost;}+ /**+ * @return the termInfos+ */+ public List<TermInfo> getTermsInfos() {+ return termsInfos;+ }+public WeightedPhraseInfo( LinkedList<TermInfo> terms, float boost ){this( terms, boost, 0 );}@@ -149,6 +163,10 @@ public class FieldPhraseList {public WeightedPhraseInfo( LinkedList<TermInfo> terms, float boost, int seqnum ){this.boost = boost;this.seqnum = seqnum;++ // now we keep TermInfos for further operations+ termsInfos = new ArrayList<TermInfo>( terms );+termsOffsets = new ArrayList<Toffs>( terms.size() );TermInfo ti = terms.get( 0 );termsOffsets.add( new Toffs( ti.getStartOffset(), ti.getEndOffset() ) );diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldTermStack.java b/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldTermStack.javaindex f31e85fe00b..d2d6ad43c3b 100644--- a/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldTermStack.java+++ b/lucene/highlighter/src/java/org/apache/lucene/search/vectorhighlight/FieldTermStack.java@@ -94,6 +94,10 @@ public class FieldTermStack {final TermsEnum termsEnum = vector.iterator(null);DocsAndPositionsEnum dpEnum = null;BytesRef text;++ int numDocs = reader.numDocs() - reader.numDeletedDocs();+ float weight = 0;+while ((text = termsEnum.next()) != null) {UnicodeUtil.UTF8toUTF16(text, spare);final String term = spare.toString();@@ -112,7 +116,9 @@ public class FieldTermStack {for(int i = 0;i < freq;i++) {int pos = dpEnum.nextPosition();- termList.add(new TermInfo(term, dpEnum.startOffset(), dpEnum.endOffset(), pos));+ // For weight look here: http://lucene.apache.org/core/3_6_0/api/core/org/apache/lucene/search/DefaultSimilarity.html+ weight = ( float ) ( Math.log( numDocs / ( double ) ( reader.docFreq( fieldName, text ) + 1 ) ) + 1.0 );+ termList.add( new TermInfo( term, dpEnum.startOffset(), dpEnum.endOffset(), pos, weight ) );}}@@ -152,22 +158,27 @@ public class FieldTermStack {public static class TermInfo implements Comparable<TermInfo>{- final String text;- final int startOffset;- final int endOffset;- final int position;+ private final String text;+ private final int startOffset;+ private final int endOffset;+ private final int position;- TermInfo( String text, int startOffset, int endOffset, int position ){+ // IDF-weight of this term+ private final float weight;++ public TermInfo( String text, int startOffset, int endOffset, int position, float weight ){this.text = text;this.startOffset = startOffset;this.endOffset = endOffset;this.position = position;+ this.weight = weight;}public String getText(){ return text; }public int getStartOffset(){ return startOffset; }public int getEndOffset(){ return endOffset; }public int getPosition(){ return position; }+ public float getWeight(){ return weight; }@Overridepublic String toString(){@@ -176,7 +187,8 @@ public class FieldTermStack {return sb.toString();}- public int compareTo( TermInfo o ) {+ @Override+ public int compareTo( TermInfo o ){return ( this.position - o.position );}}diff --git a/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FieldQueryTest.java b/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FieldQueryTest.javaindex c6aec47704e..34376c278fa 100644--- a/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FieldQueryTest.java+++ b/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FieldQueryTest.java@@ -810,13 +810,13 @@ public class FieldQueryTest extends AbstractTestCase {// "a"List<TermInfo> phraseCandidate = new ArrayList<TermInfo>();- phraseCandidate.add( new TermInfo( "a", 0, 1, 0 ) );+ phraseCandidate.add( new TermInfo( "a", 0, 1, 0, 1 ) );assertNull( fq.searchPhrase( F, phraseCandidate ) );// "a b"- phraseCandidate.add( new TermInfo( "b", 2, 3, 1 ) );+ phraseCandidate.add( new TermInfo( "b", 2, 3, 1, 1 ) );assertNull( fq.searchPhrase( F, phraseCandidate ) );// "a b c"- phraseCandidate.add( new TermInfo( "c", 4, 5, 2 ) );+ phraseCandidate.add( new TermInfo( "c", 4, 5, 2, 1 ) );assertNotNull( fq.searchPhrase( F, phraseCandidate ) );assertNull( fq.searchPhrase( "x", phraseCandidate ) );@@ -832,13 +832,13 @@ public class FieldQueryTest extends AbstractTestCase {// "a"phraseCandidate.clear();- phraseCandidate.add( new TermInfo( "a", 0, 1, 0 ) );+ phraseCandidate.add( new TermInfo( "a", 0, 1, 0, 1 ) );assertNotNull( fq.searchPhrase( F, phraseCandidate ) );// "a b"- phraseCandidate.add( new TermInfo( "b", 2, 3, 1 ) );+ phraseCandidate.add( new TermInfo( "b", 2, 3, 1, 1 ) );assertNull( fq.searchPhrase( F, phraseCandidate ) );// "a b c"- phraseCandidate.add( new TermInfo( "c", 4, 5, 2 ) );+ phraseCandidate.add( new TermInfo( "c", 4, 5, 2, 1 ) );assertNotNull( fq.searchPhrase( F, phraseCandidate ) );assertNull( fq.searchPhrase( "x", phraseCandidate ) );}@@ -852,9 +852,9 @@ public class FieldQueryTest extends AbstractTestCase {// "a b c" w/ position-gap = 2List<TermInfo> phraseCandidate = new ArrayList<TermInfo>();- phraseCandidate.add( new TermInfo( "a", 0, 1, 0 ) );- phraseCandidate.add( new TermInfo( "b", 2, 3, 2 ) );- phraseCandidate.add( new TermInfo( "c", 4, 5, 4 ) );+ phraseCandidate.add( new TermInfo( "a", 0, 1, 0, 1 ) );+ phraseCandidate.add( new TermInfo( "b", 2, 3, 2, 1 ) );+ phraseCandidate.add( new TermInfo( "c", 4, 5, 4, 1 ) );assertNull( fq.searchPhrase( F, phraseCandidate ) );// "a b c"~1@@ -868,9 +868,9 @@ public class FieldQueryTest extends AbstractTestCase {// "a b c" w/ position-gap = 3phraseCandidate.clear();- phraseCandidate.add( new TermInfo( "a", 0, 1, 0 ) );- phraseCandidate.add( new TermInfo( "b", 2, 3, 3 ) );- phraseCandidate.add( new TermInfo( "c", 4, 5, 6 ) );+ phraseCandidate.add( new TermInfo( "a", 0, 1, 0, 1 ) );+ phraseCandidate.add( new TermInfo( "b", 2, 3, 3, 1 ) );+ phraseCandidate.add( new TermInfo( "c", 4, 5, 6, 1 ) );assertNull( fq.searchPhrase( F, phraseCandidate ) );}@@ -901,7 +901,7 @@ public class FieldQueryTest extends AbstractTestCase {assertNotNull (qpm);assertNull (fq.getFieldTermMap(F, "dog"));List<TermInfo> phraseCandidate = new ArrayList<TermInfo>();- phraseCandidate.add( new TermInfo( "defg", 0, 12, 0 ) );+ phraseCandidate.add( new TermInfo( "defg", 0, 12, 0, 1 ) );assertNotNull (fq.searchPhrase(F, phraseCandidate));}