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


Java IndexWriter.close方法代碼示例

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


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

示例1: test

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
@Test
public void test() throws Exception {
    Path path = FileSystems.getDefault().getPath("", "index");
    Directory directory = FSDirectory.open(path);
    Analyzer analyzer = new StandardAnalyzer();
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer).setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
    Document document = new Document();
    document.add(new LegacyLongField("id", 5499, Field.Store.YES));
    document.add(new Field("title", "小米6", TYPE_STORED));
    document.add(new Field("sellPoint", "驍龍835,6G內存,雙攝!", TYPE_STORED));
    document.
    indexWriter.addDocument(document);
    indexWriter.commit();
    indexWriter.close();
}
 
開發者ID:felayman,項目名稱:elasticsearch-full,代碼行數:17,代碼來源:CreateIndexDemo.java

示例2: testNRTSearchOnClosedWriter

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
public void testNRTSearchOnClosedWriter() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
    DirectoryReader reader = DirectoryReader.open(indexWriter);

    for (int i = 0; i < 100; i++) {
        Document document = new Document();
        TextField field = new TextField("_id", Integer.toString(i), Field.Store.YES);
        field.setBoost(i);
        document.add(field);
        indexWriter.addDocument(document);
    }
    reader = refreshReader(reader);

    indexWriter.close();

    TermsEnum termDocs = SlowCompositeReaderWrapper.wrap(reader).terms("_id").iterator();
    termDocs.next();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:20,代碼來源:SimpleLuceneTests.java

示例3: testGetParentIdNoParentField

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
public void testGetParentIdNoParentField() throws Exception {
    ParentFieldMapper fieldMapper = createParentFieldMapper();
    Directory directory = newDirectory();
    IndexWriter indexWriter = new IndexWriter(directory, newIndexWriterConfig());
    Document document = new Document();
    document.add(new SortedDocValuesField("different_field", new BytesRef("1")));
    indexWriter.addDocument(document);
    indexWriter.close();

    IndexReader indexReader = DirectoryReader.open(directory);
    String id = ParentFieldSubFetchPhase.getParentId(fieldMapper, indexReader.leaves().get(0).reader(), 0);
    assertNull(id);

    indexReader.close();
    directory.close();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:17,代碼來源:ParentFieldSubFetchPhaseTests.java

示例4: testSimple

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
/** 
 * test version lookup actually works
 */
public void testSimple() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
    Document doc = new Document();
    doc.add(new Field(UidFieldMapper.NAME, "6", UidFieldMapper.Defaults.FIELD_TYPE));
    doc.add(new NumericDocValuesField(VersionFieldMapper.NAME, 87));
    writer.addDocument(doc);
    DirectoryReader reader = DirectoryReader.open(writer);
    LeafReaderContext segment = reader.leaves().get(0);
    PerThreadIDAndVersionLookup lookup = new PerThreadIDAndVersionLookup(segment.reader());
    // found doc
    DocIdAndVersion result = lookup.lookup(new BytesRef("6"), null, segment);
    assertNotNull(result);
    assertEquals(87, result.version);
    assertEquals(0, result.docId);
    // not found doc
    assertNull(lookup.lookup(new BytesRef("7"), null, segment));
    // deleted doc
    assertNull(lookup.lookup(new BytesRef("6"), new Bits.MatchNoBits(1), segment));
    reader.close();
    writer.close();
    dir.close();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:27,代碼來源:VersionLookupTests.java

示例5: assertCompressionEquals

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
private void assertCompressionEquals(Mode expected, Codec actual) throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(null);
    iwc.setCodec(actual);
    IndexWriter iw = new IndexWriter(dir, iwc);
    iw.addDocument(new Document());
    iw.commit();
    iw.close();
    DirectoryReader ir = DirectoryReader.open(dir);
    SegmentReader sr = (SegmentReader) ir.leaves().get(0).reader();
    String v = sr.getSegmentInfo().info.getAttribute(Lucene50StoredFieldsFormat.MODE_KEY);
    assertNotNull(v);
    assertEquals(expected, Mode.valueOf(v));
    ir.close();
    dir.close();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:17,代碼來源:CodecTests.java

示例6: doTestDocValueRangeQueries

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
public void doTestDocValueRangeQueries(NumberType type, Supplier<Number> valueSupplier) throws Exception {
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
    final int numDocs = TestUtil.nextInt(random(), 100, 500);
    for (int i = 0; i < numDocs; ++i) {
        w.addDocument(type.createFields("foo", valueSupplier.get(), true, true, false));
    }
    DirectoryReader reader = DirectoryReader.open(w);
    IndexSearcher searcher = newSearcher(reader);
    w.close();
    final int iters = 10;
    for (int iter = 0; iter < iters; ++iter) {
        Query query = type.rangeQuery("foo",
                random().nextBoolean() ? null : valueSupplier.get(),
                random().nextBoolean() ? null : valueSupplier.get(),
                randomBoolean(), randomBoolean(), true);
        assertThat(query, Matchers.instanceOf(IndexOrDocValuesQuery.class));
        IndexOrDocValuesQuery indexOrDvQuery = (IndexOrDocValuesQuery) query;
        assertEquals(
                searcher.count(indexOrDvQuery.getIndexQuery()),
                searcher.count(indexOrDvQuery.getRandomAccessQuery()));
    }
    reader.close();
    dir.close();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:26,代碼來源:NumberFieldTypeTests.java

示例7: index

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
/**
 * 生成索引
 *
 * @param indexDir
 * @throws Exception
 */
public void index(String indexDir) throws Exception {
  dir = FSDirectory.open(Paths.get(indexDir));
  IndexWriter writer = getWriter();
  for (int i = 0; i < usernames.length; i++) {
    Document doc = new Document();
    doc.add(new StringField("username", usernames[i] + "", Field.Store.YES));
    doc.add(new StringField("city", cities[i], Field.Store.YES));
    doc.add(new TextField("description", descriptions[i], Field.Store.YES));
    writer.addDocument(doc); // 添加文檔
  }
  writer.close();
}
 
開發者ID:MiniPa,項目名稱:cjs_ssms,代碼行數:19,代碼來源:IndexerTest.java

示例8: updateIndex

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
/**
 * 更新博客索引
 *
 * @param user
 * @throws Exception
 */
public void updateIndex(UUser user) throws Exception {
  IndexWriter writer = getWriter();
  Document doc = new Document();
  doc.add(new StringField("userid", String.valueOf(user.getId()), Field.Store.YES));
  doc.add(new TextField("username", user.getUsername(), Field.Store.YES));
  doc.add(new TextField("description", user.getDescription(), Field.Store.YES));

  writer.updateDocument(new Term("userid", String.valueOf(user.getId())), doc);
  writer.close();
}
 
開發者ID:MiniPa,項目名稱:cjs_ssms,代碼行數:17,代碼來源:LuceneIndex.java

示例9: testRedisDirectoryWithRemoteJedisPool

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
public void
testRedisDirectoryWithRemoteJedisPool() throws IOException {
    long start = System.currentTimeMillis();
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(IndexWriterConfig
            .OpenMode.CREATE);
    JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "10.97.19.55", 6379, Constants.TIME_OUT);
    RedisDirectory redisDirectory = new RedisDirectory(new JedisPoolStream(jedisPool));
    IndexWriter indexWriter = new IndexWriter(redisDirectory, indexWriterConfig);
    for (int i = 0; i < 5000000; i++) {
        indexWriter.addDocument(addDocument(i));
    }
    indexWriter.commit();
    indexWriter.close();
    redisDirectory.close();
    long end = System.currentTimeMillis();
    log.error("RedisDirectoryWithJedisPool consumes {}s!", (end - start) / 1000);
    start = System.currentTimeMillis();
    IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(new RedisDirectory(new JedisStream("localhost",
            6379))));
    int total = 0;
    for (int i = 0; i < 1000000; i++) {
        TermQuery key1 = new TermQuery(new Term("key1", "key" + i));
        TopDocs search = indexSearcher.search(key1, 10);
        total += search.totalHits;
    }
    System.out.println(total);
    end = System.currentTimeMillis();
    log.error("RedisDirectoryWithJedisPool search consumes {}ms!", (end - start));
}
 
開發者ID:shijiebei2009,項目名稱:RedisDirectory,代碼行數:30,代碼來源:TestLucene.java

示例10: testRedisDirectoryWithJedis

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
public void testRedisDirectoryWithJedis() throws IOException {
    long start = System.currentTimeMillis();
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(IndexWriterConfig
            .OpenMode.CREATE);
    //indexWriterConfig.setInfoStream(System.out);
    //indexWriterConfig.setRAMBufferSizeMB(2048);
    //LogByteSizeMergePolicy logByteSizeMergePolicy = new LogByteSizeMergePolicy();
    //logByteSizeMergePolicy.setMinMergeMB(1);
    //logByteSizeMergePolicy.setMaxMergeMB(64);
    //logByteSizeMergePolicy.setMaxCFSSegmentSizeMB(64);
    //indexWriterConfig.setRAMBufferSizeMB(1024).setMergePolicy(logByteSizeMergePolicy).setUseCompoundFile(false);
    //GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
    //獲取連接等待時間
    //genericObjectPoolConfig.setMaxWaitMillis(3000);
    //10s超時時間
    RedisDirectory redisDirectory = new RedisDirectory(new JedisStream("localhost", 6379));
    IndexWriter indexWriter = new IndexWriter(redisDirectory, indexWriterConfig);
    for (int i = 0; i < 10000000; i++) {
        indexWriter.addDocument(addDocument(i));
    }
    indexWriter.commit();
    indexWriter.close();
    redisDirectory.close();
    long end = System.currentTimeMillis();
    log.error("RedisDirectoryWithJedis consumes {}s!", (end - start) / 1000);
    start = System.currentTimeMillis();
    IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(new RedisDirectory(new JedisStream("localhost",
            6379))));
    int total = 0;
    for (int i = 0; i < 10000000; i++) {
        TermQuery key1 = new TermQuery(new Term("key1", "key" + i));
        TopDocs search = indexSearcher.search(key1, 10);
        total += search.totalHits;
    }
    System.out.println(total);
    end = System.currentTimeMillis();
    log.error("RedisDirectoryWithJedis search consumes {}ms!", (end - start));
}
 
開發者ID:shijiebei2009,項目名稱:RedisDirectory,代碼行數:39,代碼來源:TestLucene.java

示例11: testLoadGlobal_neverCacheIfFieldIsMissing

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
public void testLoadGlobal_neverCacheIfFieldIsMissing() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(null);
    iwc.setMergePolicy(NoMergePolicy.INSTANCE);
    IndexWriter iw = new IndexWriter(dir, iwc);
    long numDocs = scaledRandomIntBetween(32, 128);

    for (int i = 1; i <= numDocs; i++) {
        Document doc = new Document();
        doc.add(new SortedSetDocValuesField("field1", new BytesRef(String.valueOf(i))));
        doc.add(new StringField("field2", String.valueOf(i), Field.Store.NO));
        iw.addDocument(doc);
        if (i % 24 == 0) {
            iw.commit();
        }
    }
    iw.close();
    DirectoryReader ir = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(dir), new ShardId("_index", "_na_", 0));

    DummyAccountingFieldDataCache fieldDataCache = new DummyAccountingFieldDataCache();
    // Testing SortedSetDVOrdinalsIndexFieldData:
    SortedSetDVOrdinalsIndexFieldData sortedSetDVOrdinalsIndexFieldData = createSortedDV("field1", fieldDataCache);
    sortedSetDVOrdinalsIndexFieldData.loadGlobal(ir);
    assertThat(fieldDataCache.cachedGlobally, equalTo(1));
    sortedSetDVOrdinalsIndexFieldData.loadGlobal(new FieldMaskingReader("field1", ir));
    assertThat(fieldDataCache.cachedGlobally, equalTo(1));

    // Testing PagedBytesIndexFieldData
    PagedBytesIndexFieldData pagedBytesIndexFieldData = createPagedBytes("field2", fieldDataCache);
    pagedBytesIndexFieldData.loadGlobal(ir);
    assertThat(fieldDataCache.cachedGlobally, equalTo(2));
    pagedBytesIndexFieldData.loadGlobal(new FieldMaskingReader("field2", ir));
    assertThat(fieldDataCache.cachedGlobally, equalTo(2));

    ir.close();
    dir.close();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:38,代碼來源:FieldDataCacheTests.java

示例12: deleteIndex

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
/**
 * 刪除指定博客的索引
 *
 * @param userid
 * @throws Exception
 */
public void deleteIndex(String userid) throws Exception {
  IndexWriter writer = getWriter();
  writer.deleteDocuments(new Term("userid", userid));

  writer.forceMergeDeletes(); // 強製刪除
  writer.commit();
  writer.close();
}
 
開發者ID:MiniPa,項目名稱:cjs_ssms,代碼行數:15,代碼來源:LuceneIndex.java

示例13: testOrdering

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
/**
 * Here, we verify that the order that we add fields to a document counts, and not the lexi order
 * of the field. This means that heavily accessed fields that use field selector should be added
 * first (with load and break).
 */
public void testOrdering() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));

    Document document = new Document();
    document.add(new TextField("_id", "1", Field.Store.YES));
    document.add(new TextField("#id", "1", Field.Store.YES));
    indexWriter.addDocument(document);

    IndexReader reader = DirectoryReader.open(indexWriter);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1);
    final ArrayList<String> fieldsOrder = new ArrayList<>();
    searcher.doc(topDocs.scoreDocs[0].doc, new StoredFieldVisitor() {
        @Override
        public Status needsField(FieldInfo fieldInfo) throws IOException {
            fieldsOrder.add(fieldInfo.name);
            return Status.YES;
        }
    });

    assertThat(fieldsOrder.size(), equalTo(2));
    assertThat(fieldsOrder.get(0), equalTo("_id"));
    assertThat(fieldsOrder.get(1), equalTo("#id"));

    indexWriter.close();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:33,代碼來源:SimpleLuceneTests.java

示例14: testUserDataRead

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
public void testUserDataRead() throws IOException {
    final ShardId shardId = new ShardId("index", "_na_", 1);
    DirectoryService directoryService = new LuceneManagedDirectoryService(random());
    Store store = new Store(shardId, INDEX_SETTINGS, directoryService, new DummyShardLock(shardId));
    IndexWriterConfig config = newIndexWriterConfig(random(), new MockAnalyzer(random())).setCodec(TestUtil.getDefaultCodec());
    SnapshotDeletionPolicy deletionPolicy = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
    config.setIndexDeletionPolicy(deletionPolicy);
    IndexWriter writer = new IndexWriter(store.directory(), config);
    Document doc = new Document();
    doc.add(new TextField("id", "1", Field.Store.NO));
    writer.addDocument(doc);
    Map<String, String> commitData = new HashMap<>(2);
    String syncId = "a sync id";
    String translogId = "a translog id";
    commitData.put(Engine.SYNC_COMMIT_ID, syncId);
    commitData.put(Translog.TRANSLOG_GENERATION_KEY, translogId);
    writer.setCommitData(commitData);
    writer.commit();
    writer.close();
    Store.MetadataSnapshot metadata;
    metadata = store.getMetadata(randomBoolean() ? null : deletionPolicy.snapshot());
    assertFalse(metadata.asMap().isEmpty());
    // do not check for correct files, we have enough tests for that above
    assertThat(metadata.getCommitUserData().get(Engine.SYNC_COMMIT_ID), equalTo(syncId));
    assertThat(metadata.getCommitUserData().get(Translog.TRANSLOG_GENERATION_KEY), equalTo(translogId));
    TestUtil.checkIndex(store.directory());
    assertDeleteContent(store, directoryService);
    IOUtils.close(store);
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:30,代碼來源:StoreTests.java

示例15: addEvent

import org.apache.lucene.index.IndexWriter; //導入方法依賴的package包/類
public void addEvent (Event newEvent) throws IOException {
	Directory index = FSDirectory.open (Paths.get(indexDirectoryPath));
	
	PerFieldAnalyzerWrapper analyzer = new PerFieldAnalyzerWrapper (new StandardAnalyzer (), fieldAnalyzerLookup);
	
	IndexWriterConfig indexWriterConfig = new IndexWriterConfig (analyzer);
	
	indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
	
	IndexWriter indexWriter = new IndexWriter (index, indexWriterConfig);
	
	Document d = eventToDocument(newEvent);	
	
	indexWriter.addDocument(d);
	
	indexWriter.commit();
	
	indexWriter.close();
	
	index.close ();
}
 
開發者ID:ser316asu,項目名稱:Reinickendorf_SER316,代碼行數:22,代碼來源:EventIndexer.java


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