当前位置: 首页>>代码示例>>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;未经允许,请勿转载。