本文整理匯總了Java中org.apache.lucene.search.PhraseQuery.getTerms方法的典型用法代碼示例。如果您正苦於以下問題:Java PhraseQuery.getTerms方法的具體用法?Java PhraseQuery.getTerms怎麽用?Java PhraseQuery.getTerms使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.lucene.search.PhraseQuery
的用法示例。
在下文中一共展示了PhraseQuery.getTerms方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: applySlop
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
private Query applySlop(Query q, int slop) {
if (q instanceof PhraseQuery) {
PhraseQuery pq = (PhraseQuery) q;
PhraseQuery.Builder builder = new PhraseQuery.Builder();
builder.setSlop(slop);
final Term[] terms = pq.getTerms();
final int[] positions = pq.getPositions();
for (int i = 0; i < terms.length; ++i) {
builder.add(terms[i], positions[i]);
}
pq = builder.build();
pq.setBoost(q.getBoost());
return pq;
} else if (q instanceof MultiPhraseQuery) {
((MultiPhraseQuery) q).setSlop(slop);
return q;
} else {
return q;
}
}
示例2: phraseFilter
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
private static Optional<Query> phraseFilter(PhraseQuery query, boolean isNegated) {
Term[] terms = query.getTerms();
if (terms.length == 0) {
return Optional.absent();
} else if (terms.length == 1) {
return Optional.<Query>of(new TermQuery(terms[0]));
} else if (!isNegated) {
List<Query> ret = new ArrayList<>();
for (Term t : terms) {
ret.add(new TermQuery(t));
}
return Optional.<Query>of(all(ret));
} else {
return Optional.absent();
}
}
示例3: testBuildPhraseQuery
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
@Test
public void testBuildPhraseQuery() {
String field = "testField";
String text = "Foo Bar Baz";
PhraseQuery pq = LuceneQueryFunctions.toPhraseQuery.apply(field,text);
Term[] terms = pq.getTerms();
assertThat(terms[0].field(),is(field));
assertThat(terms[0].text(),is("Foo"));
assertThat(terms[1].field(),is(field));
assertThat(terms[1].text(),is("Bar"));
assertThat(terms[2].field(),is(field));
assertThat(terms[2].text(),is("Baz"));
}
示例4: FieldQuery
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
FieldQuery( Query query, IndexReader reader, boolean phraseHighlight, boolean fieldMatch ) throws IOException {
this.fieldMatch = fieldMatch;
Set<Query> flatQueries = new LinkedHashSet<>();
flatten( query, reader, flatQueries );
saveTerms( flatQueries, reader );
Collection<Query> expandQueries = expand( flatQueries );
for( Query flatQuery : expandQueries ){
QueryPhraseMap rootMap = getRootMap( flatQuery );
rootMap.add( flatQuery, reader );
if( !phraseHighlight && flatQuery instanceof PhraseQuery ){
PhraseQuery pq = (PhraseQuery)flatQuery;
if( pq.getTerms().length > 1 ){
for( Term term : pq.getTerms() )
rootMap.addTerm( term, flatQuery.getBoost() );
}
}
}
}
示例5: add
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
void add( Query query, IndexReader reader ) {
if( query instanceof TermQuery ){
addTerm( ((TermQuery)query).getTerm(), query.getBoost() );
}
else if( query instanceof PhraseQuery ){
PhraseQuery pq = (PhraseQuery)query;
Term[] terms = pq.getTerms();
Map<String, QueryPhraseMap> map = subMap;
QueryPhraseMap qpm = null;
for( Term term : terms ){
qpm = getOrNewMap( map, term.text() );
map = qpm.subMap;
}
qpm.markTerminal( pq.getSlop(), pq.getBoost() );
}
else
throw new RuntimeException( "query \"" + query.toString() + "\" must be flatten first." );
}
示例6: testPositionIncrement
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
public void testPositionIncrement() throws Exception {
StandardQueryParser qp = new StandardQueryParser();
qp.setAnalyzer(
new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET));
qp.setEnablePositionIncrements(true);
String qtxt = "\"the words in poisitions pos02578 are stopped in this phrasequery\"";
// 0 2 5 7 8
int expectedPositions[] = { 1, 3, 4, 6, 9 };
PhraseQuery pq = (PhraseQuery) qp.parse(qtxt, "a");
// System.out.println("Query text: "+qtxt);
// System.out.println("Result: "+pq);
Term t[] = pq.getTerms();
int pos[] = pq.getPositions();
for (int i = 0; i < t.length; i++) {
// System.out.println(i+". "+t[i]+" pos: "+pos[i]);
assertEquals("term " + i + " = " + t[i] + " has wrong term-position!",
expectedPositions[i], pos[i]);
}
}
示例7: FieldQuery
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
FieldQuery( Query query, IndexReader reader, boolean phraseHighlight, boolean fieldMatch ) throws IOException {
this.fieldMatch = fieldMatch;
Set<Query> flatQueries = new LinkedHashSet<Query>();
flatten( query, reader, flatQueries );
saveTerms( flatQueries, reader );
Collection<Query> expandQueries = expand( flatQueries );
for( Query flatQuery : expandQueries ){
QueryPhraseMap rootMap = getRootMap( flatQuery );
rootMap.add( flatQuery, reader );
if( !phraseHighlight && flatQuery instanceof PhraseQuery ){
PhraseQuery pq = (PhraseQuery)flatQuery;
if( pq.getTerms().length > 1 ){
for( Term term : pq.getTerms() )
rootMap.addTerm( term, flatQuery.getBoost() );
}
}
}
}
示例8: testPositionIncrement
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
public void testPositionIncrement() throws Exception {
StandardQueryParser qp = new StandardQueryParser();
qp.setAnalyzer(
new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET, true));
qp.setEnablePositionIncrements(true);
String qtxt = "\"the words in poisitions pos02578 are stopped in this phrasequery\"";
// 0 2 5 7 8
int expectedPositions[] = { 1, 3, 4, 6, 9 };
PhraseQuery pq = (PhraseQuery) qp.parse(qtxt, "a");
// System.out.println("Query text: "+qtxt);
// System.out.println("Result: "+pq);
Term t[] = pq.getTerms();
int pos[] = pq.getPositions();
for (int i = 0; i < t.length; i++) {
// System.out.println(i+". "+t[i]+" pos: "+pos[i]);
assertEquals("term " + i + " = " + t[i] + " has wrong term-position!",
expectedPositions[i], pos[i]);
}
}
示例9: applySlop
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
private Query applySlop(Query q, int slop) {
if (q instanceof PhraseQuery) {
PhraseQuery.Builder builder = new PhraseQuery.Builder();
builder.setSlop(slop);
PhraseQuery pq = (PhraseQuery) q;
org.apache.lucene.index.Term[] terms = pq.getTerms();
int[] positions = pq.getPositions();
for (int i = 0; i < terms.length; ++i) {
builder.add(terms[i], positions[i]);
}
q = builder.build();
}
else if (q instanceof MultiPhraseQuery) {
MultiPhraseQuery mpq = (MultiPhraseQuery) q;
if (slop != mpq.getSlop()) {
q = new MultiPhraseQuery.Builder(mpq).setSlop(slop).build();
}
}
return q;
}
示例10: testPositionIncrement
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
public void testPositionIncrement() throws Exception {
CommonQueryParserConfiguration qp = getParserConfig( new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET));
qp.setEnablePositionIncrements(true);
String qtxt = "\"the words in poisitions pos02578 are stopped in this phrasequery\"";
// 0 2 5 7 8
int expectedPositions[] = {1,3,4,6,9};
PhraseQuery pq = (PhraseQuery) getQuery(qtxt,qp);
//System.out.println("Query text: "+qtxt);
//System.out.println("Result: "+pq);
Term t[] = pq.getTerms();
int pos[] = pq.getPositions();
for (int i = 0; i < t.length; i++) {
//System.out.println(i+". "+t[i]+" pos: "+pos[i]);
assertEquals("term "+i+" = "+t[i]+" has wrong term-position!",expectedPositions[i],pos[i]);
}
}
示例11: addSlopToPhrase
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
/**
* Rebuild a phrase query with a slop value
*/
private PhraseQuery addSlopToPhrase(PhraseQuery query, int slop) {
PhraseQuery.Builder builder = new PhraseQuery.Builder();
builder.setSlop(slop);
final Term[] terms = query.getTerms();
final int[] positions = query.getPositions();
for (int i = 0; i < terms.length; ++i) {
builder.add(terms[i], positions[i]);
}
return builder.build();
}
示例12: checkOverlap
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
private void checkOverlap( Collection<Query> expandQueries, PhraseQuery a, PhraseQuery b ){
if( a.getSlop() != b.getSlop() ) return;
Term[] ats = a.getTerms();
Term[] bts = b.getTerms();
if( fieldMatch && !ats[0].field().equals( bts[0].field() ) ) return;
checkOverlap( expandQueries, ats, bts, a.getSlop(), a.getBoost() );
checkOverlap( expandQueries, bts, ats, b.getSlop(), b.getBoost() );
}
示例13: getKey
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
private String getKey( Query query ){
if( !fieldMatch ) return null;
if( query instanceof TermQuery )
return ((TermQuery)query).getTerm().field();
else if ( query instanceof PhraseQuery ){
PhraseQuery pq = (PhraseQuery)query;
Term[] terms = pq.getTerms();
return terms[0].field();
}
else if (query instanceof MultiTermQuery) {
return ((MultiTermQuery)query).getField();
}
else
throw new RuntimeException( "query \"" + query.toString() + "\" must be flatten first." );
}
示例14: addSlopToPhrase
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
/**
* Rebuild a phrase query with a slop value
*/
private PhraseQuery addSlopToPhrase(PhraseQuery query, int slop) {
PhraseQuery.Builder builder = new PhraseQuery.Builder();
builder.setSlop(slop);
org.apache.lucene.index.Term[] terms = query.getTerms();
int[] positions = query.getPositions();
for (int i = 0; i < terms.length; ++i) {
builder.add(terms[i], positions[i]);
}
return builder.build();
}
示例15: rewrite
import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
public static Query rewrite(PhraseQuery pq, Set<String> intFields) {
final List<Query> termQueries = Lists.newArrayListWithCapacity(pq.getTerms().length);
for (final org.apache.lucene.index.Term term : pq.getTerms()) {
termQueries.add(Query.newTermQuery(rewriteTerm(term, intFields)));
}
return Query.newBooleanQuery(BooleanOp.AND, termQueries);
}