本文整理汇总了Java中org.apache.lucene.search.PhraseQuery.setSlop方法的典型用法代码示例。如果您正苦于以下问题:Java PhraseQuery.setSlop方法的具体用法?Java PhraseQuery.setSlop怎么用?Java PhraseQuery.setSlop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.lucene.search.PhraseQuery
的用法示例。
在下文中一共展示了PhraseQuery.setSlop方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkOverlap
import org.apache.lucene.search.PhraseQuery; //导入方法依赖的package包/类
private void checkOverlap( Collection<Query> expandQueries, Term[] src, Term[] dest, int slop, float boost ){
// beginning from 1 (not 0) is safe because that the PhraseQuery has multiple terms
// is guaranteed in flatten() method (if PhraseQuery has only one term, flatten()
// converts PhraseQuery to TermQuery)
for( int i = 1; i < src.length; i++ ){
boolean overlap = true;
for( int j = i; j < src.length; j++ ){
if( ( j - i ) < dest.length && !src[j].text().equals( dest[j-i].text() ) ){
overlap = false;
break;
}
}
if( overlap && src.length - i < dest.length ){
PhraseQuery pq = new PhraseQuery();
for( Term srcTerm : src )
pq.add( srcTerm );
for( int k = src.length - i; k < dest.length; k++ ){
pq.add( new Term( src[0].field(), dest[k].text() ) );
}
pq.setSlop( slop );
pq.setBoost( boost );
if(!expandQueries.contains( pq ) )
expandQueries.add( pq );
}
}
}
示例2: testSameFieldAddedMultipleTimes
import org.apache.lucene.search.PhraseQuery; //导入方法依赖的package包/类
public void testSameFieldAddedMultipleTimes() throws IOException {
MemoryIndex mindex = new MemoryIndex(random().nextBoolean(), random().nextInt(50) * 1024 * 1024);
MockAnalyzer mockAnalyzer = new MockAnalyzer(random());
mindex.addField("field", "the quick brown fox", mockAnalyzer);
mindex.addField("field", "jumps over the", mockAnalyzer);
AtomicReader reader = (AtomicReader) mindex.createSearcher().getIndexReader();
assertEquals(7, reader.terms("field").getSumTotalTermFreq());
PhraseQuery query = new PhraseQuery();
query.add(new Term("field", "fox"));
query.add(new Term("field", "jumps"));
assertTrue(mindex.search(query) > 0.1);
mindex.reset();
mockAnalyzer.setPositionIncrementGap(1 + random().nextInt(10));
mindex.addField("field", "the quick brown fox", mockAnalyzer);
mindex.addField("field", "jumps over the", mockAnalyzer);
assertEquals(0, mindex.search(query), 0.00001f);
query.setSlop(10);
assertTrue("posGap" + mockAnalyzer.getPositionIncrementGap("field") , mindex.search(query) > 0.0001);
}
示例3: queryFromSkipBigrams
import org.apache.lucene.search.PhraseQuery; //导入方法依赖的package包/类
/**
* Create a Lucene query using the bigrams in the given text
* @param text
*/
public BooleanQuery queryFromSkipBigrams(String text) {
BooleanQuery q = new BooleanQuery();
String prev_word = null;
for (String word : text.split("\\W+")) {
if (prev_word != null) {
PhraseQuery pq = new PhraseQuery();
pq.setSlop(1);
pq.add(new Term("text", prev_word));
pq.add(new Term("text", word));
q.add(pq, BooleanClause.Occur.SHOULD);
}
q.add(new TermQuery(new Term("text", word)), BooleanClause.Occur.SHOULD);
prev_word = word;
}
return q;
}
示例4: search
import org.apache.lucene.search.PhraseQuery; //导入方法依赖的package包/类
public TopDocs search(String searchText, int slop) throws IOException, ParseException
{
PhraseQuery query = new PhraseQuery();
query.setSlop(slop);
String [] searchTerms=searchText.split(" ");
for(String searchWord:searchTerms)
query.add(new Term("body",searchWord.toLowerCase()));
return indSearch.search(query, 100);
}
示例5: pq
import org.apache.lucene.search.PhraseQuery; //导入方法依赖的package包/类
protected Query pq( float boost, int slop, String field, String... texts ){
PhraseQuery query = new PhraseQuery();
for( String text : texts ){
query.add( new Term( field, text ) );
}
query.setBoost( boost );
query.setSlop( slop );
return query;
}
示例6: testConcurrentPhrase
import org.apache.lucene.search.PhraseQuery; //导入方法依赖的package包/类
public void testConcurrentPhrase() throws IOException, InvalidTokenOffsetsException {
final String TEXT = "the fox jumped";
final Directory directory = newDirectory();
final IndexWriter indexWriter = new IndexWriter(directory,
newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
try {
final Document document = new Document();
FieldType customType = new FieldType(TextField.TYPE_NOT_STORED);
customType.setStoreTermVectorOffsets(true);
customType.setStoreTermVectorPositions(true);
customType.setStoreTermVectors(true);
document.add(new Field(FIELD, new TokenStreamConcurrent(), customType));
indexWriter.addDocument(document);
} finally {
indexWriter.close();
}
final IndexReader indexReader = DirectoryReader.open(directory);
try {
assertEquals(1, indexReader.numDocs());
final IndexSearcher indexSearcher = newSearcher(indexReader);
final PhraseQuery phraseQuery = new PhraseQuery();
phraseQuery.add(new Term(FIELD, "fox"));
phraseQuery.add(new Term(FIELD, "jumped"));
phraseQuery.setSlop(0);
TopDocs hits = indexSearcher.search(phraseQuery, 1);
assertEquals(1, hits.totalHits);
final Highlighter highlighter = new Highlighter(
new SimpleHTMLFormatter(), new SimpleHTMLEncoder(),
new QueryScorer(phraseQuery));
final TokenStream tokenStream = TokenSources
.getTokenStream(indexReader.getTermVector(
0, FIELD), false);
assertEquals(highlighter.getBestFragment(new TokenStreamConcurrent(),
TEXT), highlighter.getBestFragment(tokenStream, TEXT));
} finally {
indexReader.close();
directory.close();
}
}
示例7: testSparsePhraseWithNoPositions
import org.apache.lucene.search.PhraseQuery; //导入方法依赖的package包/类
public void testSparsePhraseWithNoPositions() throws IOException, InvalidTokenOffsetsException {
final String TEXT = "the fox did not jump";
final Directory directory = newDirectory();
final IndexWriter indexWriter = new IndexWriter(directory,
newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
try {
final Document document = new Document();
FieldType customType = new FieldType(TextField.TYPE_STORED);
customType.setStoreTermVectorOffsets(true);
customType.setStoreTermVectors(true);
document.add(new Field(FIELD, TEXT, customType));
indexWriter.addDocument(document);
} finally {
indexWriter.close();
}
final IndexReader indexReader = DirectoryReader.open(directory);
try {
assertEquals(1, indexReader.numDocs());
final IndexSearcher indexSearcher = newSearcher(indexReader);
final PhraseQuery phraseQuery = new PhraseQuery();
phraseQuery.add(new Term(FIELD, "did"));
phraseQuery.add(new Term(FIELD, "jump"));
phraseQuery.setSlop(1);
TopDocs hits = indexSearcher.search(phraseQuery, 1);
assertEquals(1, hits.totalHits);
final Highlighter highlighter = new Highlighter(
new SimpleHTMLFormatter(), new SimpleHTMLEncoder(),
new QueryScorer(phraseQuery));
final TokenStream tokenStream = TokenSources.getTokenStream(
indexReader.getTermVector(0, FIELD), true);
assertEquals("the fox <B>did</B> not <B>jump</B>", highlighter
.getBestFragment(tokenStream, TEXT));
} finally {
indexReader.close();
directory.close();
}
}
示例8: testCJKSloppyPhrase
import org.apache.lucene.search.PhraseQuery; //导入方法依赖的package包/类
public void testCJKSloppyPhrase() throws Exception {
// individual CJK chars as terms
SimpleCJKAnalyzer analyzer = new SimpleCJKAnalyzer();
PhraseQuery expected = new PhraseQuery();
expected.setSlop(3);
expected.add(new Term("field", "中"));
expected.add(new Term("field", "国"));
assertEquals(expected, getQuery("\"中国\"~3", analyzer));
}
示例9: testPhraseWithSlop
import org.apache.lucene.search.PhraseQuery; //导入方法依赖的package包/类
/** test a simple phrase with various slop settings */
public void testPhraseWithSlop() throws Exception {
PhraseQuery expectedWithSlop = new PhraseQuery();
expectedWithSlop.add(new Term("field", "foo"));
expectedWithSlop.add(new Term("field", "bar"));
expectedWithSlop.setSlop(2);
assertEquals(expectedWithSlop, parse("\"foo bar\"~2"));
PhraseQuery expectedWithMultiDigitSlop = new PhraseQuery();
expectedWithMultiDigitSlop.add(new Term("field", "foo"));
expectedWithMultiDigitSlop.add(new Term("field", "bar"));
expectedWithMultiDigitSlop.setSlop(10);
assertEquals(expectedWithMultiDigitSlop, parse("\"foo bar\"~10"));
PhraseQuery expectedNoSlop = new PhraseQuery();
expectedNoSlop.add(new Term("field", "foo"));
expectedNoSlop.add(new Term("field", "bar"));
assertEquals("Ignore trailing tilde with no slop", expectedNoSlop, parse("\"foo bar\"~"));
assertEquals("Ignore non-numeric trailing slop", expectedNoSlop, parse("\"foo bar\"~a"));
assertEquals("Ignore non-numeric trailing slop", expectedNoSlop, parse("\"foo bar\"~1a"));
assertEquals("Ignore negative trailing slop", expectedNoSlop, parse("\"foo bar\"~-1"));
PhraseQuery pq = new PhraseQuery();
pq.add(new Term("field", "foo"));
pq.add(new Term("field", "bar"));
pq.setSlop(12);
BooleanQuery expectedBoolean = new BooleanQuery();
expectedBoolean.add(pq, Occur.MUST);
expectedBoolean.add(new TermQuery(new Term("field", "baz")), Occur.MUST);
assertEquals(expectedBoolean, parse("\"foo bar\"~12 baz"));
}
示例10: testCJKSloppyPhrase
import org.apache.lucene.search.PhraseQuery; //导入方法依赖的package包/类
public void testCJKSloppyPhrase() throws Exception {
// individual CJK chars as terms
SimpleCJKAnalyzer analyzer = new SimpleCJKAnalyzer();
PhraseQuery expected = new PhraseQuery();
expected.setSlop(3);
expected.add(new Term("field", "中"));
expected.add(new Term("field", "国"));
QueryBuilder builder = new QueryBuilder(analyzer);
assertEquals(expected, builder.createPhraseQuery("field", "中国", 3));
}
示例11: testConcurrentPhrase
import org.apache.lucene.search.PhraseQuery; //导入方法依赖的package包/类
public void testConcurrentPhrase() throws IOException, InvalidTokenOffsetsException {
final String TEXT = "the fox jumped";
final Directory directory = newDirectory();
final IndexWriter indexWriter = new IndexWriter(directory,
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
try {
final Document document = new Document();
FieldType customType = new FieldType(TextField.TYPE_NOT_STORED);
customType.setStoreTermVectorOffsets(true);
customType.setStoreTermVectorPositions(true);
customType.setStoreTermVectors(true);
document.add(new Field(FIELD, new TokenStreamConcurrent(), customType));
indexWriter.addDocument(document);
} finally {
indexWriter.close();
}
final IndexReader indexReader = DirectoryReader.open(directory);
try {
assertEquals(1, indexReader.numDocs());
final IndexSearcher indexSearcher = newSearcher(indexReader);
final PhraseQuery phraseQuery = new PhraseQuery();
phraseQuery.add(new Term(FIELD, "fox"));
phraseQuery.add(new Term(FIELD, "jumped"));
phraseQuery.setSlop(0);
TopDocs hits = indexSearcher.search(phraseQuery, 1);
assertEquals(1, hits.totalHits);
final Highlighter highlighter = new Highlighter(
new SimpleHTMLFormatter(), new SimpleHTMLEncoder(),
new QueryScorer(phraseQuery));
final TokenStream tokenStream = TokenSources
.getTokenStream(indexReader.getTermVector(
0, FIELD), false);
assertEquals(highlighter.getBestFragment(new TokenStreamConcurrent(),
TEXT), highlighter.getBestFragment(tokenStream, TEXT));
} finally {
indexReader.close();
directory.close();
}
}
示例12: testSparsePhraseWithNoPositions
import org.apache.lucene.search.PhraseQuery; //导入方法依赖的package包/类
public void testSparsePhraseWithNoPositions() throws IOException, InvalidTokenOffsetsException {
final String TEXT = "the fox did not jump";
final Directory directory = newDirectory();
final IndexWriter indexWriter = new IndexWriter(directory,
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
try {
final Document document = new Document();
FieldType customType = new FieldType(TextField.TYPE_STORED);
customType.setStoreTermVectorOffsets(true);
customType.setStoreTermVectors(true);
document.add(new Field(FIELD, TEXT, customType));
indexWriter.addDocument(document);
} finally {
indexWriter.close();
}
final IndexReader indexReader = DirectoryReader.open(directory);
try {
assertEquals(1, indexReader.numDocs());
final IndexSearcher indexSearcher = newSearcher(indexReader);
final PhraseQuery phraseQuery = new PhraseQuery();
phraseQuery.add(new Term(FIELD, "did"));
phraseQuery.add(new Term(FIELD, "jump"));
phraseQuery.setSlop(1);
TopDocs hits = indexSearcher.search(phraseQuery, 1);
assertEquals(1, hits.totalHits);
final Highlighter highlighter = new Highlighter(
new SimpleHTMLFormatter(), new SimpleHTMLEncoder(),
new QueryScorer(phraseQuery));
final TokenStream tokenStream = TokenSources.getTokenStream(
indexReader.getTermVector(0, FIELD), true);
assertEquals("the fox <B>did</B> not <B>jump</B>", highlighter
.getBestFragment(tokenStream, TEXT));
} finally {
indexReader.close();
directory.close();
}
}
示例13: testSparsePhrase
import org.apache.lucene.search.PhraseQuery; //导入方法依赖的package包/类
public void testSparsePhrase() throws IOException, InvalidTokenOffsetsException {
final String TEXT = "the fox did not jump";
final Directory directory = newDirectory();
final IndexWriter indexWriter = new IndexWriter(directory,
newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
try {
final Document document = new Document();
FieldType customType = new FieldType(TextField.TYPE_NOT_STORED);
customType.setStoreTermVectorOffsets(true);
customType.setStoreTermVectorPositions(true);
customType.setStoreTermVectors(true);
document.add(new Field(FIELD, new TokenStreamSparse(), customType));
indexWriter.addDocument(document);
} finally {
indexWriter.close();
}
final IndexReader indexReader = DirectoryReader.open(directory);
try {
assertEquals(1, indexReader.numDocs());
final IndexSearcher indexSearcher = newSearcher(indexReader);
final PhraseQuery phraseQuery = new PhraseQuery();
phraseQuery.add(new Term(FIELD, "did"));
phraseQuery.add(new Term(FIELD, "jump"));
phraseQuery.setSlop(0);
TopDocs hits = indexSearcher.search(phraseQuery, 1);
assertEquals(0, hits.totalHits);
final Highlighter highlighter = new Highlighter(
new SimpleHTMLFormatter(), new SimpleHTMLEncoder(),
new QueryScorer(phraseQuery));
final TokenStream tokenStream = TokenSources
.getTokenStream(indexReader.getTermVector(
0, FIELD), false);
assertEquals(
highlighter.getBestFragment(new TokenStreamSparse(), TEXT),
highlighter.getBestFragment(tokenStream, TEXT));
} finally {
indexReader.close();
directory.close();
}
}
示例14: testSparsePhrase
import org.apache.lucene.search.PhraseQuery; //导入方法依赖的package包/类
public void testSparsePhrase() throws IOException, InvalidTokenOffsetsException {
final String TEXT = "the fox did not jump";
final Directory directory = newDirectory();
final IndexWriter indexWriter = new IndexWriter(directory,
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
try {
final Document document = new Document();
FieldType customType = new FieldType(TextField.TYPE_NOT_STORED);
customType.setStoreTermVectorOffsets(true);
customType.setStoreTermVectorPositions(true);
customType.setStoreTermVectors(true);
document.add(new Field(FIELD, new TokenStreamSparse(), customType));
indexWriter.addDocument(document);
} finally {
indexWriter.close();
}
final IndexReader indexReader = DirectoryReader.open(directory);
try {
assertEquals(1, indexReader.numDocs());
final IndexSearcher indexSearcher = newSearcher(indexReader);
final PhraseQuery phraseQuery = new PhraseQuery();
phraseQuery.add(new Term(FIELD, "did"));
phraseQuery.add(new Term(FIELD, "jump"));
phraseQuery.setSlop(0);
TopDocs hits = indexSearcher.search(phraseQuery, 1);
assertEquals(0, hits.totalHits);
final Highlighter highlighter = new Highlighter(
new SimpleHTMLFormatter(), new SimpleHTMLEncoder(),
new QueryScorer(phraseQuery));
final TokenStream tokenStream = TokenSources
.getTokenStream(indexReader.getTermVector(
0, FIELD), false);
assertEquals(
highlighter.getBestFragment(new TokenStreamSparse(), TEXT),
highlighter.getBestFragment(tokenStream, TEXT));
} finally {
indexReader.close();
directory.close();
}
}