Java Code Examples for org.apache.lucene.index.IndexWriter#close()

The following examples show how to use org.apache.lucene.index.IndexWriter#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LuceneIndex.java    From cjs_ssms with GNU General Public License v2.0 6 votes vote down vote up
public void addIndex(UUser user) throws Exception {
  IndexWriter writer = getWriter();
  Document doc = new Document();
  /*
 * yes是会将数据存进索引,如果查询结果中需要将记录显示出来就要存进去,如果查询结果
 * 只是显示标题之类的就可以不用存,而且内容过长不建议存进去
 * 使用TextField类是可以用于查询的。
 */
  try {
    doc.add(new StringField("userid", String.valueOf(user.getId()), Field.Store.YES));
    doc.add(new TextField("username", user.getUsername(), Field.Store.YES));

    writer.addDocument(doc);
  } catch (Exception e) {
    e.printStackTrace();
    throw e;
  } finally {
    writer.close();
  }
}
 
Example 2
Source File: TestMultiFieldQPHelper.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testStopWordSearching() throws Exception {
  Analyzer analyzer = new MockAnalyzer(random());
  Directory ramDir = newDirectory();
  IndexWriter iw = new IndexWriter(ramDir, newIndexWriterConfig(analyzer));
  Document doc = new Document();
  doc.add(newTextField("body", "blah the footest blah", Field.Store.NO));
  iw.addDocument(doc);
  iw.close();

  StandardQueryParser mfqp = new StandardQueryParser();

  mfqp.setMultiFields(new String[] { "body" });
  mfqp.setAnalyzer(analyzer);
  mfqp.setDefaultOperator(StandardQueryConfigHandler.Operator.AND);
  Query q = mfqp.parse("the footest", null);
  IndexReader ir = DirectoryReader.open(ramDir);
  IndexSearcher is = newSearcher(ir);
  ScoreDoc[] hits = is.search(q, 1000).scoreDocs;
  assertEquals(1, hits.length);
  ir.close();
  ramDir.close();
}
 
Example 3
Source File: TestScorerPerf.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void createRandomTerms(int nDocs, int nTerms, double power, Directory dir) throws Exception {
  int[] freq = new int[nTerms];
  Term[] terms = new Term[nTerms];
  for (int i=0; i<nTerms; i++) {
    int f = (nTerms+1)-i;  // make first terms less frequent
    freq[i] = (int)Math.ceil(Math.pow(f,power));
    terms[i] = new Term("f",Character.toString((char)('A'+i)));
  }

  IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE));
  for (int i=0; i<nDocs; i++) {
    Document d = new Document();
    for (int j=0; j<nTerms; j++) {
      if (random().nextInt(freq[j]) == 0) {
        d.add(newStringField("f", terms[j].text(), Field.Store.NO));
        //System.out.println(d);
      }
    }
    iw.addDocument(d);
  }
  iw.forceMerge(1);
  iw.close();
}
 
Example 4
Source File: RangeFacetsExample.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Build the example index. */
public void index() throws IOException {
  IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(
      new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));

  // Add documents with a fake timestamp, 1000 sec before
  // "now", 2000 sec before "now", ...:
  for(int i=0;i<100;i++) {
    Document doc = new Document();
    long then = nowSec - i * 1000;
    // Add as doc values field, so we can compute range facets:
    doc.add(new NumericDocValuesField("timestamp", then));
    // Add as numeric field so we can drill-down:
    doc.add(new LongPoint("timestamp", then));
    indexWriter.addDocument(doc);
  }

  // Open near-real-time searcher
  searcher = new IndexSearcher(DirectoryReader.open(indexWriter));
  indexWriter.close();
}
 
Example 5
Source File: TestFacetsConfig.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testAddSameDocTwice() throws Exception {
  // LUCENE-5367: this was a problem with the previous code, making sure it
  // works with the new code.
  Directory indexDir = newDirectory(), taxoDir = newDirectory();
  IndexWriter indexWriter = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  FacetsConfig facetsConfig = new FacetsConfig();
  Document doc = new Document();
  doc.add(new FacetField("a", "b"));
  doc = facetsConfig.build(taxoWriter, doc);
  // these two addDocument() used to fail
  indexWriter.addDocument(doc);
  indexWriter.addDocument(doc);
  indexWriter.close();
  IOUtils.close(taxoWriter);
  
  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
  IndexSearcher searcher = newSearcher(indexReader);
  FacetsCollector fc = new FacetsCollector();
  searcher.search(new MatchAllDocsQuery(), fc);
  
  Facets facets = getTaxonomyFacetCounts(taxoReader, facetsConfig, fc);
  FacetResult res = facets.getTopChildren(10, "a");
  assertEquals(1, res.labelValues.length);
  assertEquals(2, res.labelValues[0].value);
  IOUtils.close(indexReader, taxoReader);
  
  IOUtils.close(indexDir, taxoDir);
}
 
Example 6
Source File: IcuCollationKeyAnalyzerTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
private void testFarsiRangeFilterCollating(Analyzer analyzer, BytesRef firstBeg,
                                   BytesRef firstEnd, BytesRef secondBeg,
                                   BytesRef secondEnd) throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(analyzer));
    Document doc = new Document();
    doc.add(new TextField("content", "\u0633\u0627\u0628", Field.Store.YES));
    doc.add(new StringField("body", "body", Field.Store.YES));
    writer.addDocument(doc);
    writer.close();
    IndexReader reader = DirectoryReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(reader);
    Query query = new TermQuery(new Term("body", "body"));

    // Unicode order would include U+0633 in [ U+062F - U+0698 ], but Farsi
    // orders the U+0698 character before the U+0633 character, so the single
    // index Term below should NOT be returned by a TermRangeFilter with a Farsi
    // Collator (or an Arabic one for the case when Farsi searcher not
    // supported).
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.add(query, BooleanClause.Occur.MUST);
    bq.add(new TermRangeQuery("content", firstBeg, firstEnd, true, true), BooleanClause.Occur.FILTER);

    ScoreDoc[] result = searcher.search(bq.build(), 1).scoreDocs;
    assertEquals("The index Term should not be included.", 0, result.length);

    bq = new BooleanQuery.Builder();
    bq.add(query, BooleanClause.Occur.MUST);
    bq.add(new TermRangeQuery("content", secondBeg, secondEnd, true, true), BooleanClause.Occur.FILTER);
    result = searcher.search(bq.build(), 1).scoreDocs;
    assertEquals("The index Term should be included.", 1, result.length);

    reader.close();
    dir.close();
}
 
Example 7
Source File: TestSuggestField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testNRTDeletedDocFiltering() throws Exception {
  Analyzer analyzer = new MockAnalyzer(random());
  // using IndexWriter instead of RandomIndexWriter
  IndexWriter iw = new IndexWriter(dir, iwcWithSuggestField(analyzer, "suggest_field"));

  int num = Math.min(1000, atLeast(10));

  int numLive = 0;
  List<Entry> expectedEntries = new ArrayList<>();
  for (int i = 0; i < num; i++) {
    Document document = new Document();
    document.add(new SuggestField("suggest_field", "abc_" + i, num - i));
    if (i % 2 == 0) {
      document.add(newStringField("str_field", "delete", Field.Store.YES));
    } else {
      numLive++;
      expectedEntries.add(new Entry("abc_" + i, num - i));
      document.add(newStringField("str_field", "no_delete", Field.Store.YES));
    }
    iw.addDocument(document);

    if (usually()) {
      iw.commit();
    }
  }

  iw.deleteDocuments(new Term("str_field", "delete"));

  DirectoryReader reader = DirectoryReader.open(iw);
  SuggestIndexSearcher indexSearcher = new SuggestIndexSearcher(reader);
  PrefixCompletionQuery query = new PrefixCompletionQuery(analyzer, new Term("suggest_field", "abc_"));
  TopSuggestDocs suggest = indexSearcher.suggest(query, numLive, false);
  assertSuggestions(suggest, expectedEntries.toArray(new Entry[expectedEntries.size()]));

  reader.close();
  iw.close();
}
 
Example 8
Source File: IndexBilingualFiles.java    From semanticvectors with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void runIndexer() {
	if (Files.exists(INDEX_DIR)) {
     throw new IllegalArgumentException(
         "Cannot save index to '" + INDEX_DIR + "' directory, please delete it first");
	}

	Date start = new Date();
	try {
		final File docDir1 = new File(LANGUAGE1);
		final File docDir2 = new File(LANGUAGE2);

		IndexWriterConfig writerConfig = new IndexWriterConfig(new StandardAnalyzer());
		IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR), writerConfig);
		System.out.println("Indexing to directory '" + INDEX_DIR + "'...");
		runDeepIndexer(docDir1, docDir2, writer);

		System.out.println("Optimizing...");
		writer.close();

		Date end = new Date();
		System.out.println(end.getTime() - start.getTime() + " total milliseconds");

	} catch (IOException e) {
		System.out.println(" caught a " + e.getClass() +
				"\n with message: " + e.getMessage());
	}
}
 
Example 9
Source File: TestLucene80DocValuesFormat.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void doTestSparseNumericBlocksOfVariousBitsPerValue(double density) throws Exception {
  Directory dir = newDirectory();
  IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
  conf.setMaxBufferedDocs(atLeast(Lucene80DocValuesFormat.NUMERIC_BLOCK_SIZE));
  conf.setRAMBufferSizeMB(-1);
  conf.setMergePolicy(newLogMergePolicy(random().nextBoolean()));
  IndexWriter writer = new IndexWriter(dir, conf);
  Document doc = new Document();
  Field storedField = newStringField("stored", "", Field.Store.YES);
  Field dvField = new NumericDocValuesField("dv", 0);
  doc.add(storedField);
  doc.add(dvField);

  final int numDocs = atLeast(Lucene80DocValuesFormat.NUMERIC_BLOCK_SIZE*3);
  final LongSupplier longs = blocksOfVariousBPV();
  for (int i = 0; i < numDocs; i++) {
    if (random().nextDouble() > density) {
      writer.addDocument(new Document());
      continue;
    }
    long value = longs.getAsLong();
    storedField.setStringValue(Long.toString(value));
    dvField.setLongValue(value);
    writer.addDocument(doc);
  }

  writer.forceMerge(1);

  writer.close();
  
  // compare
  assertDVIterate(dir);
  assertDVAdvance(dir, 1); // Tests all jump-lengths from 1 to maxDoc (quite slow ~= 1 minute for 200K docs)

  dir.close();
}
 
Example 10
Source File: IndexAndTaxonomyRevisionTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testRevisionRelease() throws Exception {
  Directory indexDir = newDirectory();
  IndexWriterConfig conf = new IndexWriterConfig(null);
  conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy()));
  IndexWriter indexWriter = new IndexWriter(indexDir, conf);
  
  Directory taxoDir = newDirectory();
  SnapshotDirectoryTaxonomyWriter taxoWriter = new SnapshotDirectoryTaxonomyWriter(taxoDir);
  try {
    indexWriter.addDocument(newDocument(taxoWriter));
    indexWriter.commit();
    taxoWriter.commit();
    Revision rev1 = new IndexAndTaxonomyRevision(indexWriter, taxoWriter);
    // releasing that revision should not delete the files
    rev1.release();
    assertTrue(slowFileExists(indexDir, IndexFileNames.SEGMENTS + "_1"));
    assertTrue(slowFileExists(taxoDir, IndexFileNames.SEGMENTS + "_1"));
    
    rev1 = new IndexAndTaxonomyRevision(indexWriter, taxoWriter); // create revision again, so the files are snapshotted
    indexWriter.addDocument(newDocument(taxoWriter));
    indexWriter.commit();
    taxoWriter.commit();
    assertNotNull(new IndexAndTaxonomyRevision(indexWriter, taxoWriter));
    rev1.release(); // this release should trigger the delete of segments_1
    assertFalse(slowFileExists(indexDir, IndexFileNames.SEGMENTS + "_1"));
    indexWriter.close();
  } finally {
    IOUtils.close(indexWriter, taxoWriter, taxoDir, indexDir);
  }
}
 
Example 11
Source File: MusicSearch.java    From Easy-Cassandra-samples with Apache License 2.0 5 votes vote down vote up
public void indexarAll(List<Music> musicas) throws IOException {
	IndexWriter indexWriter = LuceneUtil.INSTANCE.getIndexWriter();
	for (Music music : musicas) {
		indexWriter.addDocument(indexMusic(music));
	}
	indexWriter.close();
}
 
Example 12
Source File: TestTermRangeQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void initializeIndex(String[] values, Analyzer analyzer) throws IOException {
  IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(analyzer).setOpenMode(OpenMode.CREATE));
  for (int i = 0; i < values.length; i++) {
    insertDoc(writer, values[i]);
  }
  writer.close();
}
 
Example 13
Source File: SimpleFragmentsBuilderTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void makeUnstoredIndex() throws Exception {
  IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(analyzerW).setOpenMode(OpenMode.CREATE));
  Document doc = new Document();
  FieldType customType = new FieldType(TextField.TYPE_NOT_STORED);
  customType.setStoreTermVectors(true);
  customType.setStoreTermVectorOffsets(true);
  customType.setStoreTermVectorPositions(true);
  doc.add( new Field( F, "aaa", customType) );
  //doc.add( new Field( F, "aaa", Store.NO, Index.ANALYZED, TermVector.WITH_POSITIONS_OFFSETS ) );
  writer.addDocument( doc );
  writer.close();
  if (reader != null) reader.close();
  reader = DirectoryReader.open(dir);
}
 
Example 14
Source File: TestTaxonomyFacetSumValueSource.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testWithScore() throws Exception {
  Directory indexDir = newDirectory();
  Directory taxoDir = newDirectory();

  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));

  FacetsConfig config = new FacetsConfig();
  for (int i = 0; i < 4; i++) {
    Document doc = new Document();
    doc.add(new NumericDocValuesField("price", (i+1)));
    doc.add(new FacetField("a", Integer.toString(i % 2)));
    iw.addDocument(config.build(taxoWriter, doc));
  }
  
  DirectoryReader r = DirectoryReader.open(iw);
  DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
  
  FacetsCollector fc = new FacetsCollector(true);
  // score documents by their 'price' field - makes asserting the correct counts for the categories easier
  Query q = new FunctionQuery(new LongFieldSource("price"));
  FacetsCollector.search(newSearcher(r), q, 10, fc);
  Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, DoubleValuesSource.SCORES);
  
  assertEquals("dim=a path=[] value=10.0 childCount=2\n  1 (6.0)\n  0 (4.0)\n", facets.getTopChildren(10, "a").toString());

  iw.close();
  IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
 
Example 15
Source File: TestMatchAllDocsQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testEarlyTermination() throws IOException {

    Directory dir = newDirectory();
    IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(analyzer).setMaxBufferedDocs(2).setMergePolicy(newLogMergePolicy()));
    final int numDocs = 500;
    for (int i = 0; i < numDocs; i++) {
      addDoc("doc" + i, iw);
    }
    IndexReader ir = DirectoryReader.open(iw);

    IndexSearcher is = newSearcher(ir);

    final int totalHitsThreshold = 200;
    TopScoreDocCollector c = TopScoreDocCollector.create(10, null, totalHitsThreshold);

    is.search(new MatchAllDocsQuery(), c);
    assertEquals(totalHitsThreshold+1, c.totalHits);
    assertEquals(TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO, c.totalHitsRelation);

    TopScoreDocCollector c1 = TopScoreDocCollector.create(10, null, numDocs);

    is.search(new MatchAllDocsQuery(), c1);
    assertEquals(numDocs, c1.totalHits);
    assertEquals(TotalHits.Relation.EQUAL_TO, c1.totalHitsRelation);

    iw.close();
    ir.close();
    dir.close();

  }
 
Example 16
Source File: TestMixedDirectory.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
public void updateIndex(Directory dir, int base, int numDocs,
    IndexDeletionPolicy policy) throws IOException {
  IndexWriter writer =
      new IndexWriter(dir, false, new StandardAnalyzer(), policy);
  writer.setMaxBufferedDocs(maxBufferedDocs);
  writer.setMergeFactor(1000);
  for (int i = 0; i < numDocs; i++) {
    addDoc(writer, base + i);
  }
  writer.close();
}
 
Example 17
Source File: LuceneIndex.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void shutDown() throws IOException {
	// try-finally setup ensures that closing of an instance is not skipped
	// when an earlier instance resulted in an IOException
	// FIXME: is there a more elegant way to ensure this?
	if (closed.compareAndSet(false, true)) {
		try {
			// This close oldMonitors which hold InderReader and
			// IndexSeracher
			// Monitor close IndexReader and IndexSearcher
			ReaderMonitor toCloseCurrentMonitor = currentMonitor;
			currentMonitor = null;
			if (toCloseCurrentMonitor != null) {
				toCloseCurrentMonitor.close();
			}
		} finally {
			List<Throwable> exceptions = new ArrayList<>();
			try {
				synchronized (oldmonitors) {
					if (oldmonitors.size() > 0) {
						logger.warn(
								"LuceneSail: On shutdown {} IndexReaders were not closed. This is due to non-closed Query Iterators, which must be closed!",
								oldmonitors.size());
					}
					for (AbstractReaderMonitor monitor : oldmonitors) {
						try {
							monitor.close();
						} catch (Throwable e) {
							exceptions.add(e);
						}
					}
					oldmonitors.clear();
				}
			} finally {
				try {
					IndexWriter toCloseIndexWriter = indexWriter;
					indexWriter = null;
					if (toCloseIndexWriter != null) {
						toCloseIndexWriter.close();
					}
				} finally {
					if (!exceptions.isEmpty()) {
						throw new UndeclaredThrowableException(exceptions.get(0));
					}
				}
			}
		}
	}
}
 
Example 18
Source File: TestSynonymQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testRandomTopDocs() throws IOException {
  Directory dir = newDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
  int numDocs = TEST_NIGHTLY ? atLeast(128 * 8 * 8 * 3) : atLeast(100); // at night, make sure some terms have skip data
  for (int i = 0; i < numDocs; ++i) {
    Document doc = new Document();
    int numValues = random().nextInt(1 << random().nextInt(5));
    int start = random().nextInt(10);
    for (int j = 0; j < numValues; ++j) {
      int freq = TestUtil.nextInt(random(), 1, 1 << random().nextInt(3));
      for (int k = 0; k < freq; ++k) {
        doc.add(new TextField("foo", Integer.toString(start + j), Store.NO));
      }
    }
    w.addDocument(doc);
  }
  IndexReader reader = DirectoryReader.open(w);
  w.close();
  IndexSearcher searcher = newSearcher(reader);

  for (int term1 = 0; term1 < 15; ++term1) {
    int term2;
    do {
      term2 = random().nextInt(15);
    } while (term1 == term2);
    float boost1 = random().nextBoolean() ? Math.max(random().nextFloat(), Float.MIN_NORMAL) : 1f;
    float boost2 = random().nextBoolean() ? Math.max(random().nextFloat(), Float.MIN_NORMAL) : 1f;
    Query query = new SynonymQuery.Builder("foo")
        .addTerm(new Term("foo", Integer.toString(term1)), boost1)
        .addTerm(new Term("foo", Integer.toString(term2)), boost2)
        .build();

    TopScoreDocCollector collector1 = TopScoreDocCollector.create(10, null, Integer.MAX_VALUE); // COMPLETE
    TopScoreDocCollector collector2 = TopScoreDocCollector.create(10, null, 1); // TOP_SCORES

    searcher.search(query, collector1);
    searcher.search(query, collector2);
    CheckHits.checkEqual(query, collector1.topDocs().scoreDocs, collector2.topDocs().scoreDocs);

    int filterTerm = random().nextInt(15);
    Query filteredQuery = new BooleanQuery.Builder()
        .add(query, Occur.MUST)
        .add(new TermQuery(new Term("foo", Integer.toString(filterTerm))), Occur.FILTER)
        .build();

    collector1 = TopScoreDocCollector.create(10, null, Integer.MAX_VALUE); // COMPLETE
    collector2 = TopScoreDocCollector.create(10, null, 1); // TOP_SCORES
    searcher.search(filteredQuery, collector1);
    searcher.search(filteredQuery, collector2);
    CheckHits.checkEqual(query, collector1.topDocs().scoreDocs, collector2.topDocs().scoreDocs);
  }
  reader.close();
  dir.close();
}
 
Example 19
Source File: TestIndexOrDocValuesQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testUseIndexForSelectiveMultiValueQueries() throws IOException {
  Directory dir = newDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig()
      // relies on costs and PointValues.estimateCost so we need the default codec
      .setCodec(TestUtil.getDefaultCodec()));
  for (int i = 0; i < 2000; ++i) {
    Document doc = new Document();
    if (i < 1000) {
      doc.add(new StringField("f1", "bar", Store.NO));
      for (int j =0; j < 500; j++) {
        doc.add(new LongPoint("f2", 42L));
        doc.add(new SortedNumericDocValuesField("f2", 42L));
      }
    } else if (i == 1001) {
      doc.add(new StringField("f1", "foo", Store.NO));
      doc.add(new LongPoint("f2", 2L));
      doc.add(new SortedNumericDocValuesField("f2", 42L));
    } else {
      doc.add(new StringField("f1", "bar", Store.NO));
      for (int j =0; j < 100; j++) {
        doc.add(new LongPoint("f2", 2L));
        doc.add(new SortedNumericDocValuesField("f2", 2L));
      }
    }
    w.addDocument(doc);
  }
  w.forceMerge(1);
  IndexReader reader = DirectoryReader.open(w);
  IndexSearcher searcher = newSearcher(reader);
  searcher.setQueryCache(null);

  // The term query is less selective, so the IndexOrDocValuesQuery should use points
  final Query q1 = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("f1", "bar")), Occur.MUST)
      .add(new IndexOrDocValuesQuery(LongPoint.newExactQuery("f2", 2), SortedNumericDocValuesField.newSlowRangeQuery("f2", 2L, 2L)), Occur.MUST)
      .build();

  final Weight w1 = searcher.createWeight(searcher.rewrite(q1), ScoreMode.COMPLETE, 1);
  final Scorer s1 = w1.scorer(searcher.getIndexReader().leaves().get(0));
  assertNull(s1.twoPhaseIterator()); // means we use points

  // The term query is less selective, so the IndexOrDocValuesQuery should use points
  final Query q2 = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("f1", "bar")), Occur.MUST)
      .add(new IndexOrDocValuesQuery(LongPoint.newExactQuery("f2", 42), SortedNumericDocValuesField.newSlowRangeQuery("f2", 42, 42L)), Occur.MUST)
      .build();

  final Weight w2 = searcher.createWeight(searcher.rewrite(q2), ScoreMode.COMPLETE, 1);
  final Scorer s2 = w2.scorer(searcher.getIndexReader().leaves().get(0));
  assertNull(s2.twoPhaseIterator()); // means we use points

  // The term query is more selective, so the IndexOrDocValuesQuery should use doc values
  final Query q3 = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("f1", "foo")), Occur.MUST)
      .add(new IndexOrDocValuesQuery(LongPoint.newExactQuery("f2", 42), SortedNumericDocValuesField.newSlowRangeQuery("f2", 42, 42L)), Occur.MUST)
      .build();

  final Weight w3 = searcher.createWeight(searcher.rewrite(q3), ScoreMode.COMPLETE, 1);
  final Scorer s3 = w3.scorer(searcher.getIndexReader().leaves().get(0));
  assertNotNull(s3.twoPhaseIterator()); // means we use doc values

  reader.close();
  w.close();
  dir.close();
}
 
Example 20
Source File: LuceneIndexFromSemrepTriples.java    From semanticvectors with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** Index all text files under a directory. */
   public static void main(String[] args) {
String usage = "java pitt.search.lucene.LuceneIndexFromTriples [triples text file] ";
if (args.length == 0) {
    System.err.println("Usage: " + usage);
    System.exit(1);
}
      
if (Files.exists(INDEX_DIR)) {
	throw new IllegalArgumentException(
					   "Cannot save index to '" + INDEX_DIR + "' directory, please delete it first");
    }


try {
    // Create IndexWriter using WhiteSpaceAnalyzer without any stopword list.
    //IndexWriterConfig writerConfig = new IndexWriterConfig(
    //							   LUCENE_VERSION, new WhitespaceAnalyzer(LUCENE_VERSION));

    IndexWriterConfig writerConfig = new IndexWriterConfig(new WhitespaceAnalyzer());

    IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR), writerConfig);




    final File triplesTextFile = new File(args[0]);
    if (!triplesTextFile.exists() || !triplesTextFile.canRead()) {
	writer.close();
	throw new IOException("Document file '" + triplesTextFile.getAbsolutePath() +
			      "' does not exist or is not readable, please check the path");
    }

    System.out.println("Indexing to directory '" +INDEX_DIR+ "'...");
    indexDoc(writer, triplesTextFile);
    writer.close();       
} catch (IOException e) {
    System.out.println(" caught a " + e.getClass() +
		       "\n with message: " + e.getMessage());
}
   }