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


Java PhraseQuery.add方法代碼示例

本文整理匯總了Java中org.apache.lucene.search.PhraseQuery.add方法的典型用法代碼示例。如果您正苦於以下問題:Java PhraseQuery.add方法的具體用法?Java PhraseQuery.add怎麽用?Java PhraseQuery.add使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.lucene.search.PhraseQuery的用法示例。


在下文中一共展示了PhraseQuery.add方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testPositionIncrementMultiFields

import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
public void testPositionIncrementMultiFields() throws Exception {
  Directory dir = newDirectory();
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
  writer.addDocument(makeDocumentWithFields());
  IndexReader reader = writer.getReader();
  
  IndexSearcher searcher = newSearcher(reader);
  PhraseQuery query = new PhraseQuery();
  query.add(new Term("indexed_not_tokenized", "test1"));
  query.add(new Term("indexed_not_tokenized", "test2"));
  
  ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
  assertEquals(1, hits.length);
  
  doAssert(searcher.doc(hits[0].doc), true);
  writer.close();
  reader.close();
  dir.close();    
}
 
開發者ID:europeana,項目名稱:search,代碼行數:20,代碼來源:TestDocument.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: 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:yintaoxue,項目名稱:read-open-source-code,代碼行數:27,代碼來源:FieldQuery.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: testWithPendingDeletes

import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
public void testWithPendingDeletes() throws IOException {
  // main directory
  Directory dir = newDirectory();
  // auxiliary directory
  Directory aux = newDirectory();

  setUpDirs(dir, aux);
  IndexWriter writer = newWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND));
  writer.addIndexes(aux);

  // Adds 10 docs, then replaces them with another 10
  // docs, so 10 pending deletes:
  for (int i = 0; i < 20; i++) {
    Document doc = new Document();
    doc.add(newStringField("id", "" + (i % 10), Field.Store.NO));
    doc.add(newTextField("content", "bbb " + i, Field.Store.NO));
    writer.updateDocument(new Term("id", "" + (i%10)), doc);
  }
  // Deletes one of the 10 added docs, leaving 9:
  PhraseQuery q = new PhraseQuery();
  q.add(new Term("content", "bbb"));
  q.add(new Term("content", "14"));
  writer.deleteDocuments(q);

  writer.forceMerge(1);
  writer.commit();

  verifyNumDocs(dir, 1039);
  verifyTermDocs(dir, new Term("content", "aaa"), 1030);
  verifyTermDocs(dir, new Term("content", "bbb"), 9);

  writer.close();
  dir.close();
  aux.close();
}
 
開發者ID:europeana,項目名稱:search,代碼行數:36,代碼來源:TestAddIndexes.java

示例6: clause

import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
private Query clause( String field, float boost, String... terms ) {
  Query q;
  if ( terms.length == 1 ) {
    q = new TermQuery( new Term( field, terms[ 0 ] ) );
  } else {
    PhraseQuery pq = new PhraseQuery();
    for ( String term: terms ) {
      pq.add( new Term( field, term ) );
    }
    q = pq;
  }
  q.setBoost( boost );
  return q;
}
 
開發者ID:europeana,項目名稱:search,代碼行數:15,代碼來源:FastVectorHighlighterTest.java

示例7: testDisableSlop

import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
public void testDisableSlop() {
  PhraseQuery expectedPhrase = new PhraseQuery();
  expectedPhrase.add(new Term("field", "foo"));
  expectedPhrase.add(new Term("field", "bar"));

  BooleanQuery expected = new BooleanQuery();
  expected.add(expectedPhrase, Occur.MUST);
  expected.add(new TermQuery(new Term("field", "~2")), Occur.MUST);
  assertEquals(expected, parse("\"foo bar\"~2", ~NEAR_OPERATOR));
}
 
開發者ID:europeana,項目名稱:search,代碼行數:11,代碼來源:TestSimpleQueryParser.java

示例8: testBuddhism

import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
public void testBuddhism() throws Exception {
  String text = "This eight-volume set brings together seminal papers in Buddhist studies from a vast " +
                "range of academic disciplines published over the last forty years. With a new introduction " + 
                "by the editor, this collection is a unique and unrivalled research resource for both " + 
                "student and scholar. Coverage includes: - Buddhist origins; early history of Buddhism in " + 
                "South and Southeast Asia - early Buddhist Schools and Doctrinal History; Theravada Doctrine " + 
                "- the Origins and nature of Mahayana Buddhism; some Mahayana religious topics - Abhidharma " + 
                "and Madhyamaka - Yogacara, the Epistemological tradition, and Tathagatagarbha - Tantric " + 
                "Buddhism (Including China and Japan); Buddhism in Nepal and Tibet - Buddhism in South and " + 
                "Southeast Asia, and - Buddhism in China, East Asia, and Japan.";
  Directory dir = newDirectory();
  Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true);
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, analyzer);
  
  FieldType positionsType = new FieldType(TextField.TYPE_STORED);
  positionsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
  Field body = new Field("body", text, positionsType);
  Document document = new Document();
  document.add(body);
  iw.addDocument(document);
  IndexReader ir = iw.getReader();
  iw.close();
  IndexSearcher searcher = newSearcher(ir);
  PhraseQuery query = new PhraseQuery();
  query.add(new Term("body", "buddhist"));
  query.add(new Term("body", "origins"));
  TopDocs topDocs = searcher.search(query, 10);
  assertEquals(1, topDocs.totalHits);
  PostingsHighlighter highlighter = new PostingsHighlighter();
  String snippets[] = highlighter.highlight("body", query, searcher, topDocs, 2);
  assertEquals(1, snippets.length);
  assertTrue(snippets[0].contains("<b>Buddhist</b> <b>origins</b>"));
  ir.close();
  dir.close();
}
 
開發者ID:europeana,項目名稱:search,代碼行數:36,代碼來源:TestPostingsHighlighter.java

示例9: testCuriousGeorge

import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
public void testCuriousGeorge() throws Exception {
  String text = "It’s the formula for success for preschoolers—Curious George and fire trucks! " + 
                "Curious George and the Firefighters is a story based on H. A. and Margret Rey’s " +
                "popular primate and painted in the original watercolor and charcoal style. " + 
                "Firefighters are a famously brave lot, but can they withstand a visit from one curious monkey?";
  Directory dir = newDirectory();
  Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true);
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, analyzer);
  FieldType positionsType = new FieldType(TextField.TYPE_STORED);
  positionsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
  Field body = new Field("body", text, positionsType);
  Document document = new Document();
  document.add(body);
  iw.addDocument(document);
  IndexReader ir = iw.getReader();
  iw.close();
  IndexSearcher searcher = newSearcher(ir);
  PhraseQuery query = new PhraseQuery();
  query.add(new Term("body", "curious"));
  query.add(new Term("body", "george"));
  TopDocs topDocs = searcher.search(query, 10);
  assertEquals(1, topDocs.totalHits);
  PostingsHighlighter highlighter = new PostingsHighlighter();
  String snippets[] = highlighter.highlight("body", query, searcher, topDocs, 2);
  assertEquals(1, snippets.length);
  assertFalse(snippets[0].contains("<b>Curious</b>Curious"));
  ir.close();
  dir.close();
}
 
開發者ID:europeana,項目名稱:search,代碼行數:30,代碼來源:TestPostingsHighlighter.java

示例10: testPhrase

import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
@Test
public void testPhrase() throws Exception {
  PhraseQuery query = new PhraseQuery();
  query.add(new Term("field", "seventy"));
  query.add(new Term("field", "seven"));
  checkHits(query, new int[]
    {77, 177, 277, 377, 477, 577, 677, 777, 877,
            977, 1077, 1177, 1277, 1377, 1477, 1577, 1677, 1777, 1877, 1977});
}
 
開發者ID:europeana,項目名稱:search,代碼行數:10,代碼來源:TestBasics.java

示例11: testWithPendingDeletes3

import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
public void testWithPendingDeletes3() throws IOException {
  // main directory
  Directory dir = newDirectory();
  // auxiliary directory
  Directory aux = newDirectory();

  setUpDirs(dir, aux);
  IndexWriter writer = newWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND));

  // Adds 10 docs, then replaces them with another 10
  // docs, so 10 pending deletes:
  for (int i = 0; i < 20; i++) {
    Document doc = new Document();
    doc.add(newStringField("id", "" + (i % 10), Field.Store.NO));
    doc.add(newTextField("content", "bbb " + i, Field.Store.NO));
    writer.updateDocument(new Term("id", "" + (i%10)), doc);
  }

  // Deletes one of the 10 added docs, leaving 9:
  PhraseQuery q = new PhraseQuery();
  q.add(new Term("content", "bbb"));
  q.add(new Term("content", "14"));
  writer.deleteDocuments(q);

  writer.addIndexes(aux);

  writer.forceMerge(1);
  writer.commit();

  verifyNumDocs(dir, 1039);
  verifyTermDocs(dir, new Term("content", "aaa"), 1030);
  verifyTermDocs(dir, new Term("content", "bbb"), 9);

  writer.close();
  dir.close();
  aux.close();
}
 
開發者ID:europeana,項目名稱:search,代碼行數:38,代碼來源:TestAddIndexes.java

示例12: testWithPendingDeletes2

import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
public void testWithPendingDeletes2() throws IOException {
  // main directory
  Directory dir = newDirectory();
  // auxiliary directory
  Directory aux = newDirectory();

  setUpDirs(dir, aux);
  IndexWriter writer = newWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND));

  // Adds 10 docs, then replaces them with another 10
  // docs, so 10 pending deletes:
  for (int i = 0; i < 20; i++) {
    Document doc = new Document();
    doc.add(newStringField("id", "" + (i % 10), Field.Store.NO));
    doc.add(newTextField("content", "bbb " + i, Field.Store.NO));
    writer.updateDocument(new Term("id", "" + (i%10)), doc);
  }
  
  writer.addIndexes(aux);
  
  // Deletes one of the 10 added docs, leaving 9:
  PhraseQuery q = new PhraseQuery();
  q.add(new Term("content", "bbb"));
  q.add(new Term("content", "14"));
  writer.deleteDocuments(q);

  writer.forceMerge(1);
  writer.commit();

  verifyNumDocs(dir, 1039);
  verifyTermDocs(dir, new Term("content", "aaa"), 1030);
  verifyTermDocs(dir, new Term("content", "bbb"), 9);

  writer.close();
  dir.close();
  aux.close();
}
 
開發者ID:europeana,項目名稱:search,代碼行數:38,代碼來源:TestAddIndexes.java

示例13: testPhrase2

import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
@Test
public void testPhrase2() throws Exception {
  PhraseQuery query = new PhraseQuery();
  query.add(new Term("field", "seventish"));
  query.add(new Term("field", "sevenon"));
  checkHits(query, new int[] {});
}
 
開發者ID:europeana,項目名稱:search,代碼行數:8,代碼來源:TestBasics.java

示例14: testCJKBoostedPhrase

import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
public void testCJKBoostedPhrase() throws Exception {
  // individual CJK chars as terms
  SimpleCJKAnalyzer analyzer = new SimpleCJKAnalyzer();
  
  PhraseQuery expected = new PhraseQuery();
  expected.setBoost(0.5f);
  expected.add(new Term("field", "中"));
  expected.add(new Term("field", "國"));
  
  assertEquals(expected, getQuery("\"中國\"^0.5", analyzer));
}
 
開發者ID:europeana,項目名稱:search,代碼行數:12,代碼來源:TestQPHelper.java

示例15: search

import org.apache.lucene.search.PhraseQuery; //導入方法依賴的package包/類
private ScoreDoc[] search() throws IOException {
    // create PhraseQuery "term1 term2" and search
    PhraseQuery pq = new PhraseQuery();
    pq.add(new Term(this.field, this.term1));
    pq.add(new Term(this.field, this.term2));
    return this.searcher.search(pq, null, 1000).scoreDocs;        
}
 
開發者ID:europeana,項目名稱:search,代碼行數:8,代碼來源:TestLazyProxSkipping.java


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