public void testQueryPhraseMap1PhraseAnother() throws Exception { Query query = pqF( "search", "engines" ); // phraseHighlight = true, fieldMatch = true FieldQuery fq = new FieldQuery( query, true, true ); Map<String, QueryPhraseMap> map = fq.rootMaps; assertEquals( 1, map.size() ); assertNull( map.get( null ) ); assertNotNull( map.get( F ) ); QueryPhraseMap qpm = map.get( F ); assertEquals( 1, qpm.subMap.size() ); assertNotNull( qpm.subMap.get( "search" ) ); QueryPhraseMap qpm2 = qpm.subMap.get( "search" ); assertFalse( qpm2.terminal ); assertEquals( 1, qpm2.subMap.size() ); assertNotNull( qpm2.subMap.get( "engines" ) ); QueryPhraseMap qpm3 = qpm2.subMap.get( "engines" ); assertTrue( qpm3.terminal ); assertEquals( 1F, qpm3.boost, 0); }
public void testGetFieldTermMap() throws Exception { Query query = tq( "a" ); FieldQuery fq = new FieldQuery( query, true, true ); QueryPhraseMap pqm = fq.getFieldTermMap( F, "a" ); assertNotNull( pqm ); assertTrue( pqm.isTerminal() ); pqm = fq.getFieldTermMap( F, "b" ); assertNull( pqm ); pqm = fq.getFieldTermMap( F1, "a" ); assertNull( pqm ); }
public void testGetRootMap() throws Exception { Query dummy = pqF( "DUMMY" ); FieldQuery fq = new FieldQuery( dummy, true, true ); QueryPhraseMap rootMap1 = fq.getRootMap( tq( "a" ) ); QueryPhraseMap rootMap2 = fq.getRootMap( tq( "a" ) ); assertTrue( rootMap1 == rootMap2 ); QueryPhraseMap rootMap3 = fq.getRootMap( tq( "b" ) ); assertTrue( rootMap1 == rootMap3 ); QueryPhraseMap rootMap4 = fq.getRootMap( tq( F1, "b" ) ); assertFalse( rootMap4 == rootMap3 ); }
public void testGetRootMapNotFieldMatch() throws Exception { Query dummy = pqF( "DUMMY" ); FieldQuery fq = new FieldQuery( dummy, true, false ); QueryPhraseMap rootMap1 = fq.getRootMap( tq( "a" ) ); QueryPhraseMap rootMap2 = fq.getRootMap( tq( "a" ) ); assertTrue( rootMap1 == rootMap2 ); QueryPhraseMap rootMap3 = fq.getRootMap( tq( "b" ) ); assertTrue( rootMap1 == rootMap3 ); QueryPhraseMap rootMap4 = fq.getRootMap( tq( F1, "b" ) ); assertTrue( rootMap4 == rootMap3 ); }
public void testQueryPhraseMap2Phrases() throws Exception { BooleanQuery query = new BooleanQuery(); query.add( pqF( "a", "b" ), Occur.SHOULD ); query.add( pqF( 2, "c", "d" ), Occur.SHOULD ); // phraseHighlight = true, fieldMatch = true FieldQuery fq = new FieldQuery( query, true, true ); Map<String, QueryPhraseMap> map = fq.rootMaps; assertEquals( 1, map.size() ); assertNull( map.get( null ) ); assertNotNull( map.get( F ) ); QueryPhraseMap qpm = map.get( F ); assertEquals( 2, qpm.subMap.size() ); // "a b" assertNotNull( qpm.subMap.get( "a" ) ); QueryPhraseMap qpm2 = qpm.subMap.get( "a" ); assertFalse( qpm2.terminal ); assertEquals( 1, qpm2.subMap.size() ); assertNotNull( qpm2.subMap.get( "b" ) ); QueryPhraseMap qpm3 = qpm2.subMap.get( "b" ); assertTrue( qpm3.terminal ); assertEquals( 1F, qpm3.boost, 0); // "c d"^2 assertNotNull( qpm.subMap.get( "c" ) ); qpm2 = qpm.subMap.get( "c" ); assertFalse( qpm2.terminal ); assertEquals( 1, qpm2.subMap.size() ); assertNotNull( qpm2.subMap.get( "d" ) ); qpm3 = qpm2.subMap.get( "d" ); assertTrue( qpm3.terminal ); assertEquals( 2F, qpm3.boost, 0); }
public void testQueryPhraseMapOverlapPhrases2() throws Exception { BooleanQuery query = new BooleanQuery(); query.add( pqF( "a", "b" ), Occur.SHOULD ); query.add( pqF( 2, "a", "b", "c" ), Occur.SHOULD ); // phraseHighlight = true, fieldMatch = true FieldQuery fq = new FieldQuery( query, true, true ); Map<String, QueryPhraseMap> map = fq.rootMaps; assertEquals( 1, map.size() ); assertNull( map.get( null ) ); assertNotNull( map.get( F ) ); QueryPhraseMap qpm = map.get( F ); assertEquals( 1, qpm.subMap.size() ); // "a b" assertNotNull( qpm.subMap.get( "a" ) ); QueryPhraseMap qpm2 = qpm.subMap.get( "a" ); assertFalse( qpm2.terminal ); assertEquals( 1, qpm2.subMap.size() ); assertNotNull( qpm2.subMap.get( "b" ) ); QueryPhraseMap qpm3 = qpm2.subMap.get( "b" ); assertTrue( qpm3.terminal ); assertEquals( 1F, qpm3.boost, 0); // "a b c"^2 assertEquals( 1, qpm3.subMap.size() ); assertNotNull( qpm3.subMap.get( "c" ) ); QueryPhraseMap qpm4 = qpm3.subMap.get( "c" ); assertTrue( qpm4.terminal ); assertEquals( 2F, qpm4.boost, 0); }
private void defgMultiTermQueryTest(Query query) throws IOException { FieldQuery fq = new FieldQuery( query, reader, true, true ); QueryPhraseMap qpm = fq.getFieldTermMap(F, "defg"); assertNotNull (qpm); assertNull (fq.getFieldTermMap(F, "dog")); List<TermInfo> phraseCandidate = new ArrayList<>(); phraseCandidate.add( new TermInfo( "defg", 0, 12, 0, 1 ) ); assertNotNull (fq.searchPhrase(F, phraseCandidate)); }
private void defgMultiTermQueryTest(Query query) throws IOException { FieldQuery fq = new FieldQuery( query, reader, true, true ); QueryPhraseMap qpm = fq.getFieldTermMap(F, "defg"); assertNotNull (qpm); assertNull (fq.getFieldTermMap(F, "dog")); List<TermInfo> phraseCandidate = new ArrayList<TermInfo>(); phraseCandidate.add( new TermInfo( "defg", 0, 12, 0, 1 ) ); assertNotNull (fq.searchPhrase(F, phraseCandidate)); }
/** * a constructor. * * @param fieldTermStack FieldTermStack object * @param fieldQuery FieldQuery object * @param phraseLimit maximum size of phraseList */ public FieldPhraseList( FieldTermStack fieldTermStack, FieldQuery fieldQuery, int phraseLimit ){ final String field = fieldTermStack.getFieldName(); LinkedList<TermInfo> phraseCandidate = new LinkedList<>(); QueryPhraseMap currMap = null; QueryPhraseMap nextMap = null; while( !fieldTermStack.isEmpty() && (phraseList.size() < phraseLimit) ) { phraseCandidate.clear(); TermInfo ti = null; TermInfo first = null; first = ti = fieldTermStack.pop(); currMap = fieldQuery.getFieldTermMap( field, ti.getText() ); while (currMap == null && ti.getNext() != first) { ti = ti.getNext(); currMap = fieldQuery.getFieldTermMap( field, ti.getText() ); } // if not found, discard top TermInfo from stack, then try next element if( currMap == null ) continue; // if found, search the longest phrase phraseCandidate.add( ti ); while( true ){ first = ti = fieldTermStack.pop(); nextMap = null; if( ti != null ) { nextMap = currMap.getTermMap( ti.getText() ); while (nextMap == null && ti.getNext() != first) { ti = ti.getNext(); nextMap = currMap.getTermMap( ti.getText() ); } } if( ti == null || nextMap == null ){ if( ti != null ) fieldTermStack.push( ti ); if( currMap.isValidTermOrPhrase( phraseCandidate ) ){ addIfNoOverlap( new WeightedPhraseInfo( phraseCandidate, currMap.getBoost(), currMap.getTermOrPhraseNumber() ) ); } else{ while( phraseCandidate.size() > 1 ){ fieldTermStack.push( phraseCandidate.removeLast() ); currMap = fieldQuery.searchPhrase( field, phraseCandidate ); if( currMap != null ){ addIfNoOverlap( new WeightedPhraseInfo( phraseCandidate, currMap.getBoost(), currMap.getTermOrPhraseNumber() ) ); break; } } } break; } else{ phraseCandidate.add( ti ); currMap = nextMap; } } } }
public void testQueryPhraseMap1Term() throws Exception { Query query = tq( "a" ); // phraseHighlight = true, fieldMatch = true FieldQuery fq = new FieldQuery( query, true, true ); Map<String, QueryPhraseMap> map = fq.rootMaps; assertEquals( 1, map.size() ); assertNull( map.get( null ) ); assertNotNull( map.get( F ) ); QueryPhraseMap qpm = map.get( F ); assertEquals( 1, qpm.subMap.size() ); assertTrue( qpm.subMap.get( "a" ) != null ); assertTrue( qpm.subMap.get( "a" ).terminal ); assertEquals( 1F, qpm.subMap.get( "a" ).boost, 0); // phraseHighlight = true, fieldMatch = false fq = new FieldQuery( query, true, false ); map = fq.rootMaps; assertEquals( 1, map.size() ); assertNull( map.get( F ) ); assertNotNull( map.get( null ) ); qpm = map.get( null ); assertEquals( 1, qpm.subMap.size() ); assertTrue( qpm.subMap.get( "a" ) != null ); assertTrue( qpm.subMap.get( "a" ).terminal ); assertEquals( 1F, qpm.subMap.get( "a" ).boost, 0); // phraseHighlight = false, fieldMatch = true fq = new FieldQuery( query, false, true ); map = fq.rootMaps; assertEquals( 1, map.size() ); assertNull( map.get( null ) ); assertNotNull( map.get( F ) ); qpm = map.get( F ); assertEquals( 1, qpm.subMap.size() ); assertTrue( qpm.subMap.get( "a" ) != null ); assertTrue( qpm.subMap.get( "a" ).terminal ); assertEquals( 1F, qpm.subMap.get( "a" ).boost, 0); // phraseHighlight = false, fieldMatch = false fq = new FieldQuery( query, false, false ); map = fq.rootMaps; assertEquals( 1, map.size() ); assertNull( map.get( F ) ); assertNotNull( map.get( null ) ); qpm = map.get( null ); assertEquals( 1, qpm.subMap.size() ); assertTrue( qpm.subMap.get( "a" ) != null ); assertTrue( qpm.subMap.get( "a" ).terminal ); assertEquals( 1F, qpm.subMap.get( "a" ).boost, 0); // boost != 1 query = tq( 2, "a" ); fq = new FieldQuery( query, true, true ); map = fq.rootMaps; qpm = map.get( F ); assertEquals( 2F, qpm.subMap.get( "a" ).boost, 0); }
public void testQueryPhraseMap2PhrasesFields() throws Exception { BooleanQuery query = new BooleanQuery(); query.add( pq( F1, "a", "b" ), Occur.SHOULD ); query.add( pq( 2F, F2, "c", "d" ), Occur.SHOULD ); // phraseHighlight = true, fieldMatch = true FieldQuery fq = new FieldQuery( query, true, true ); Map<String, QueryPhraseMap> map = fq.rootMaps; assertEquals( 2, map.size() ); assertNull( map.get( null ) ); // "a b" assertNotNull( map.get( F1 ) ); QueryPhraseMap qpm = map.get( F1 ); assertEquals( 1, qpm.subMap.size() ); assertNotNull( qpm.subMap.get( "a" ) ); QueryPhraseMap qpm2 = qpm.subMap.get( "a" ); assertFalse( qpm2.terminal ); assertEquals( 1, qpm2.subMap.size() ); assertNotNull( qpm2.subMap.get( "b" ) ); QueryPhraseMap qpm3 = qpm2.subMap.get( "b" ); assertTrue( qpm3.terminal ); assertEquals( 1F, qpm3.boost, 0); // "c d"^2 assertNotNull( map.get( F2 ) ); qpm = map.get( F2 ); assertEquals( 1, qpm.subMap.size() ); assertNotNull( qpm.subMap.get( "c" ) ); qpm2 = qpm.subMap.get( "c" ); assertFalse( qpm2.terminal ); assertEquals( 1, qpm2.subMap.size() ); assertNotNull( qpm2.subMap.get( "d" ) ); qpm3 = qpm2.subMap.get( "d" ); assertTrue( qpm3.terminal ); assertEquals( 2F, qpm3.boost, 0); // phraseHighlight = true, fieldMatch = false fq = new FieldQuery( query, true, false ); map = fq.rootMaps; assertEquals( 1, map.size() ); assertNull( map.get( F1 ) ); assertNull( map.get( F2 ) ); assertNotNull( map.get( null ) ); qpm = map.get( null ); assertEquals( 2, qpm.subMap.size() ); // "a b" assertNotNull( qpm.subMap.get( "a" ) ); qpm2 = qpm.subMap.get( "a" ); assertFalse( qpm2.terminal ); assertEquals( 1, qpm2.subMap.size() ); assertNotNull( qpm2.subMap.get( "b" ) ); qpm3 = qpm2.subMap.get( "b" ); assertTrue( qpm3.terminal ); assertEquals( 1F, qpm3.boost, 0); // "c d"^2 assertNotNull( qpm.subMap.get( "c" ) ); qpm2 = qpm.subMap.get( "c" ); assertFalse( qpm2.terminal ); assertEquals( 1, qpm2.subMap.size() ); assertNotNull( qpm2.subMap.get( "d" ) ); qpm3 = qpm2.subMap.get( "d" ); assertTrue( qpm3.terminal ); assertEquals( 2F, qpm3.boost, 0); }
public void testQueryPhraseMapOverlapPhrases() throws Exception { BooleanQuery query = new BooleanQuery(); query.add( pqF( "a", "b", "c" ), Occur.SHOULD ); query.add( pqF( 2, "b", "c", "d" ), Occur.SHOULD ); query.add( pqF( 3, "b", "d" ), Occur.SHOULD ); // phraseHighlight = true, fieldMatch = true FieldQuery fq = new FieldQuery( query, true, true ); Map<String, QueryPhraseMap> map = fq.rootMaps; assertEquals( 1, map.size() ); assertNull( map.get( null ) ); assertNotNull( map.get( F ) ); QueryPhraseMap qpm = map.get( F ); assertEquals( 2, qpm.subMap.size() ); // "a b c" assertNotNull( qpm.subMap.get( "a" ) ); QueryPhraseMap qpm2 = qpm.subMap.get( "a" ); assertFalse( qpm2.terminal ); assertEquals( 1, qpm2.subMap.size() ); assertNotNull( qpm2.subMap.get( "b" ) ); QueryPhraseMap qpm3 = qpm2.subMap.get( "b" ); assertFalse( qpm3.terminal ); assertEquals( 1, qpm3.subMap.size() ); assertNotNull( qpm3.subMap.get( "c" ) ); QueryPhraseMap qpm4 = qpm3.subMap.get( "c" ); assertTrue( qpm4.terminal ); assertEquals( 1F, qpm4.boost, 0); assertNotNull( qpm4.subMap.get( "d" ) ); QueryPhraseMap qpm5 = qpm4.subMap.get( "d" ); assertTrue( qpm5.terminal ); assertEquals( 1F, qpm5.boost, 0); // "b c d"^2, "b d"^3 assertNotNull( qpm.subMap.get( "b" ) ); qpm2 = qpm.subMap.get( "b" ); assertFalse( qpm2.terminal ); assertEquals( 2, qpm2.subMap.size() ); assertNotNull( qpm2.subMap.get( "c" ) ); qpm3 = qpm2.subMap.get( "c" ); assertFalse( qpm3.terminal ); assertEquals( 1, qpm3.subMap.size() ); assertNotNull( qpm3.subMap.get( "d" ) ); qpm4 = qpm3.subMap.get( "d" ); assertTrue( qpm4.terminal ); assertEquals( 2F, qpm4.boost, 0); assertNotNull( qpm2.subMap.get( "d" ) ); qpm3 = qpm2.subMap.get( "d" ); assertTrue( qpm3.terminal ); assertEquals( 3F, qpm3.boost, 0); }
public void testQueryPhraseMapOverlapPhrases3() throws Exception { BooleanQuery query = new BooleanQuery(); query.add( pqF( "a", "a", "a", "a" ), Occur.SHOULD ); query.add( pqF( 2, "a", "a", "a" ), Occur.SHOULD ); // phraseHighlight = true, fieldMatch = true FieldQuery fq = new FieldQuery( query, true, true ); Map<String, QueryPhraseMap> map = fq.rootMaps; assertEquals( 1, map.size() ); assertNull( map.get( null ) ); assertNotNull( map.get( F ) ); QueryPhraseMap qpm = map.get( F ); assertEquals( 1, qpm.subMap.size() ); // "a a a" assertNotNull( qpm.subMap.get( "a" ) ); QueryPhraseMap qpm2 = qpm.subMap.get( "a" ); assertFalse( qpm2.terminal ); assertEquals( 1, qpm2.subMap.size() ); assertNotNull( qpm2.subMap.get( "a" ) ); QueryPhraseMap qpm3 = qpm2.subMap.get( "a" ); assertFalse( qpm3.terminal ); assertEquals( 1, qpm3.subMap.size() ); assertNotNull( qpm3.subMap.get( "a" ) ); QueryPhraseMap qpm4 = qpm3.subMap.get( "a" ); assertTrue( qpm4.terminal ); // "a a a a" assertEquals( 1, qpm4.subMap.size() ); assertNotNull( qpm4.subMap.get( "a" ) ); QueryPhraseMap qpm5 = qpm4.subMap.get( "a" ); assertTrue( qpm5.terminal ); // "a a a a a" assertEquals( 1, qpm5.subMap.size() ); assertNotNull( qpm5.subMap.get( "a" ) ); QueryPhraseMap qpm6 = qpm5.subMap.get( "a" ); assertTrue( qpm6.terminal ); // "a a a a a a" assertEquals( 1, qpm6.subMap.size() ); assertNotNull( qpm6.subMap.get( "a" ) ); QueryPhraseMap qpm7 = qpm6.subMap.get( "a" ); assertTrue( qpm7.terminal ); }
/** * a constructor. * * @param fieldTermStack FieldTermStack object * @param fieldQuery FieldQuery object * @param phraseLimit maximum size of phraseList */ public FieldPhraseList( FieldTermStack fieldTermStack, FieldQuery fieldQuery, int phraseLimit ){ final String field = fieldTermStack.getFieldName(); LinkedList<TermInfo> phraseCandidate = new LinkedList<TermInfo>(); QueryPhraseMap currMap = null; QueryPhraseMap nextMap = null; while( !fieldTermStack.isEmpty() && (phraseList.size() < phraseLimit) ) { phraseCandidate.clear(); TermInfo ti = fieldTermStack.pop(); currMap = fieldQuery.getFieldTermMap( field, ti.getText() ); // if not found, discard top TermInfo from stack, then try next element if( currMap == null ) continue; // if found, search the longest phrase phraseCandidate.add( ti ); while( true ){ ti = fieldTermStack.pop(); nextMap = null; if( ti != null ) nextMap = currMap.getTermMap( ti.getText() ); if( ti == null || nextMap == null ){ if( ti != null ) fieldTermStack.push( ti ); if( currMap.isValidTermOrPhrase( phraseCandidate ) ){ addIfNoOverlap( new WeightedPhraseInfo( phraseCandidate, currMap.getBoost(), currMap.getTermOrPhraseNumber() ) ); } else{ while( phraseCandidate.size() > 1 ){ fieldTermStack.push( phraseCandidate.removeLast() ); currMap = fieldQuery.searchPhrase( field, phraseCandidate ); if( currMap != null ){ addIfNoOverlap( new WeightedPhraseInfo( phraseCandidate, currMap.getBoost(), currMap.getTermOrPhraseNumber() ) ); break; } } } break; } else{ phraseCandidate.add( ti ); currMap = nextMap; } } } }
/** * a constructor. * * @param fieldTermStack FieldTermStack object * @param fieldQuery FieldQuery object * @param phraseLimit maximum size of phraseList */ public FieldPhraseList( FieldTermStack fieldTermStack, FieldQuery fieldQuery, int phraseLimit ){ final String field = fieldTermStack.getFieldName(); LinkedList<TermInfo> phraseCandidate = new LinkedList<TermInfo>(); QueryPhraseMap currMap = null; QueryPhraseMap nextMap = null; while( !fieldTermStack.isEmpty() && (phraseList.size() < phraseLimit) ) { phraseCandidate.clear(); TermInfo ti = null; TermInfo first = null; first = ti = fieldTermStack.pop(); currMap = fieldQuery.getFieldTermMap( field, ti.getText() ); while (currMap == null && ti.getNext() != first) { ti = ti.getNext(); currMap = fieldQuery.getFieldTermMap( field, ti.getText() ); } // if not found, discard top TermInfo from stack, then try next element if( currMap == null ) continue; // if found, search the longest phrase phraseCandidate.add( ti ); while( true ){ first = ti = fieldTermStack.pop(); nextMap = null; if( ti != null ) { nextMap = currMap.getTermMap( ti.getText() ); while (nextMap == null && ti.getNext() != first) { ti = ti.getNext(); nextMap = currMap.getTermMap( ti.getText() ); } } if( ti == null || nextMap == null ){ if( ti != null ) fieldTermStack.push( ti ); if( currMap.isValidTermOrPhrase( phraseCandidate ) ){ addIfNoOverlap( new WeightedPhraseInfo( phraseCandidate, currMap.getBoost(), currMap.getTermOrPhraseNumber() ) ); } else{ while( phraseCandidate.size() > 1 ){ fieldTermStack.push( phraseCandidate.removeLast() ); currMap = fieldQuery.searchPhrase( field, phraseCandidate ); if( currMap != null ){ addIfNoOverlap( new WeightedPhraseInfo( phraseCandidate, currMap.getBoost(), currMap.getTermOrPhraseNumber() ) ); break; } } } break; } else{ phraseCandidate.add( ti ); currMap = nextMap; } } } }