當前位置: 首頁>>代碼示例>>Java>>正文


Java PhraseQuery.setSlop方法代碼示例

本文整理匯總了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 );
    }
  }
}
 
開發者ID:europeana,項目名稱:search,代碼行數:27,代碼來源:FieldQuery.java

示例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);
}
 
開發者ID:europeana,項目名稱:search,代碼行數:20,代碼來源:MemoryIndexTest.java

示例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;
}
 
開發者ID:SeanTater,項目名稱:uncc2014watsonsim,代碼行數:21,代碼來源:LuceneSearcher.java

示例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); 	     
}
 
開發者ID:unsw-cse-soc,項目名稱:Data-curation-API,代碼行數:10,代碼來源:DataSearch.java

示例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;
}
 
開發者ID:europeana,項目名稱:search,代碼行數:10,代碼來源:AbstractTestCase.java

示例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();
  }
}
 
開發者ID:europeana,項目名稱:search,代碼行數:41,代碼來源:HighlighterPhraseTest.java

示例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();
  }
}
 
開發者ID:europeana,項目名稱:search,代碼行數:39,代碼來源:HighlighterPhraseTest.java

示例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));
}
 
開發者ID:europeana,項目名稱:search,代碼行數:12,代碼來源:TestQPHelper.java

示例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"));
}
 
開發者ID:europeana,項目名稱:search,代碼行數:37,代碼來源:TestSimpleQueryParser.java

示例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));
}
 
開發者ID:europeana,項目名稱:search,代碼行數:13,代碼來源:TestQueryBuilder.java

示例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();
  }
}
 
開發者ID:pkarmstr,項目名稱:NYBC,代碼行數:41,代碼來源:HighlighterPhraseTest.java

示例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();
  }
}
 
開發者ID:pkarmstr,項目名稱:NYBC,代碼行數:39,代碼來源:HighlighterPhraseTest.java

示例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();
  }
}
 
開發者ID:europeana,項目名稱:search,代碼行數:42,代碼來源:HighlighterPhraseTest.java

示例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();
  }
}
 
開發者ID:pkarmstr,項目名稱:NYBC,代碼行數:42,代碼來源:HighlighterPhraseTest.java


注:本文中的org.apache.lucene.search.PhraseQuery.setSlop方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。