当前位置: 首页>>代码示例>>Java>>正文


Java DirectoryReader类代码示例

本文整理汇总了Java中org.apache.lucene.index.DirectoryReader的典型用法代码示例。如果您正苦于以下问题:Java DirectoryReader类的具体用法?Java DirectoryReader怎么用?Java DirectoryReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


DirectoryReader类属于org.apache.lucene.index包,在下文中一共展示了DirectoryReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: findByAuthorSurname

import org.apache.lucene.index.DirectoryReader; //导入依赖的package包/类
/**
 * Search all books of a given author. 
 * 
 * @throws Exception never, otherwise the test fails.
 */
@Test
public void findByAuthorSurname() throws Exception {
	IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));
	
	Query query = new QueryParser("author", new StandardAnalyzer()).parse("Gazzarini");
	TopDocs matches = searcher.search(query, 10);
	
	assertEquals(1, matches.totalHits);
			
	final String id = Arrays.stream(matches.scoreDocs)
		.map(scoreDoc -> luceneDoc(scoreDoc.doc, searcher))
		.map(doc -> doc.get("id"))
		.findFirst()
		.get();
	
	assertEquals("1", id);
}
 
开发者ID:agazzarini,项目名称:as-full-text-search-server,代码行数:23,代码来源:LuceneBasicFlowExampleTestCase.java

示例2: testSimple

import org.apache.lucene.index.DirectoryReader; //导入依赖的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

示例3: isIndexed

import org.apache.lucene.index.DirectoryReader; //导入依赖的package包/类
@Override
public boolean isIndexed(Project project, ObjectId commit) {
	File indexDir = storageManager.getProjectIndexDir(project.getForkRoot().getId());
	try (Directory directory = FSDirectory.open(indexDir)) {
		if (DirectoryReader.indexExists(directory)) {
			try (IndexReader reader = DirectoryReader.open(directory)) {
				IndexSearcher searcher = new IndexSearcher(reader);
				return getCurrentCommitIndexVersion().equals(getCommitIndexVersion(searcher, commit));
			}
		} else {
			return false;
		}
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
开发者ID:jmfgdev,项目名称:gitplex-mit,代码行数:17,代码来源:DefaultIndexManager.java

示例4: wrapReader

import org.apache.lucene.index.DirectoryReader; //导入依赖的package包/类
private DirectoryReader wrapReader(DirectoryReader reader) {
    try {
        Constructor<?>[] constructors = mockContext.wrapper.getConstructors();
        Constructor<?> nonRandom = null;
        for (Constructor<?> constructor : constructors) {
            Class<?>[] parameterTypes = constructor.getParameterTypes();
            if (parameterTypes.length > 0 && parameterTypes[0] == DirectoryReader.class) {
                if (parameterTypes.length == 1) {
                    nonRandom = constructor;
                } else if (parameterTypes.length == 2 && parameterTypes[1] == Settings.class) {

                    return (DirectoryReader) constructor.newInstance(reader, mockContext.indexSettings);
                }
            }
        }
        if (nonRandom != null) {
            return (DirectoryReader) nonRandom.newInstance(reader);
        }
    } catch (Exception e) {
        throw new ElasticsearchException("Can not wrap reader", e);
    }
    return reader;
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:24,代码来源:MockEngineSupport.java

示例5: getOrCompute

import org.apache.lucene.index.DirectoryReader; //导入依赖的package包/类
BytesReference getOrCompute(CacheEntity cacheEntity, Supplier<BytesReference> loader,
        DirectoryReader reader, BytesReference cacheKey) throws Exception {
    final Key key =  new Key(cacheEntity, reader.getVersion(), cacheKey);
    Loader cacheLoader = new Loader(cacheEntity, loader);
    BytesReference value = cache.computeIfAbsent(key, cacheLoader);
    if (cacheLoader.isLoaded()) {
        key.entity.onMiss();
        // see if its the first time we see this reader, and make sure to register a cleanup key
        CleanupKey cleanupKey = new CleanupKey(cacheEntity, reader.getVersion());
        if (!registeredClosedListeners.containsKey(cleanupKey)) {
            Boolean previous = registeredClosedListeners.putIfAbsent(cleanupKey, Boolean.TRUE);
            if (previous == null) {
                ElasticsearchDirectoryReader.addReaderCloseListener(reader, cleanupKey);
            }
        }
    } else {
        key.entity.onHit();
    }
    return value;
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:21,代码来源:IndicesRequestCache.java

示例6: cacheShardLevelResult

import org.apache.lucene.index.DirectoryReader; //导入依赖的package包/类
/**
 * Cache something calculated at the shard level.
 * @param shard the shard this item is part of
 * @param reader a reader for this shard. Used to invalidate the cache when there are changes.
 * @param cacheKey key for the thing being cached within this shard
 * @param loader loads the data into the cache if needed
 * @return the contents of the cache or the result of calling the loader
 */
private BytesReference cacheShardLevelResult(IndexShard shard, DirectoryReader reader, BytesReference cacheKey, Consumer<StreamOutput> loader)
        throws Exception {
    IndexShardCacheEntity cacheEntity = new IndexShardCacheEntity(shard);
    Supplier<BytesReference> supplier = () -> {
        /* BytesStreamOutput allows to pass the expected size but by default uses
         * BigArrays.PAGE_SIZE_IN_BYTES which is 16k. A common cached result ie.
         * a date histogram with 3 buckets is ~100byte so 16k might be very wasteful
         * since we don't shrink to the actual size once we are done serializing.
         * By passing 512 as the expected size we will resize the byte array in the stream
         * slowly until we hit the page size and don't waste too much memory for small query
         * results.*/
        final int expectedSizeInBytes = 512;
        try (BytesStreamOutput out = new BytesStreamOutput(expectedSizeInBytes)) {
            loader.accept(out);
            // for now, keep the paged data structure, which might have unused bytes to fill a page, but better to keep
            // the memory properly paged instead of having varied sized bytes
            return out.bytes();
        }
    };
    return indicesRequestCache.getOrCompute(cacheEntity, supplier, reader, cacheKey);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:30,代码来源:IndicesService.java

示例7: testVectorHighlighter

import org.apache.lucene.index.DirectoryReader; //导入依赖的package包/类
public void testVectorHighlighter() 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));
    FieldType vectorsType = new FieldType(TextField.TYPE_STORED);
    vectorsType.setStoreTermVectors(true);
    vectorsType.setStoreTermVectorPositions(true);
    vectorsType.setStoreTermVectorOffsets(true);
    document.add(new Field("content", "the big bad dog", vectorsType));
    indexWriter.addDocument(document);

    IndexReader reader = DirectoryReader.open(indexWriter);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1);

    assertThat(topDocs.totalHits, equalTo(1));

    FastVectorHighlighter highlighter = new FastVectorHighlighter();
    String fragment = highlighter.getBestFragment(highlighter.getFieldQuery(new TermQuery(new Term("content", "bad"))),
            reader, topDocs.scoreDocs[0].doc, "content", 30);
    assertThat(fragment, notNullValue());
    assertThat(fragment, equalTo("the big <b>bad</b> dog"));
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:26,代码来源:VectorHighlighterTests.java

示例8: testVectorHighlighterNoStore

import org.apache.lucene.index.DirectoryReader; //导入依赖的package包/类
public void testVectorHighlighterNoStore() 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));
    FieldType vectorsType = new FieldType(TextField.TYPE_NOT_STORED);
    vectorsType.setStoreTermVectors(true);
    vectorsType.setStoreTermVectorPositions(true);
    vectorsType.setStoreTermVectorOffsets(true);
    document.add(new Field("content", "the big bad dog", vectorsType));
    indexWriter.addDocument(document);

    IndexReader reader = DirectoryReader.open(indexWriter);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1);

    assertThat(topDocs.totalHits, equalTo(1));

    FastVectorHighlighter highlighter = new FastVectorHighlighter();
    String fragment = highlighter.getBestFragment(highlighter.getFieldQuery(new TermQuery(new Term("content", "bad"))),
            reader, topDocs.scoreDocs[0].doc, "content", 30);
    assertThat(fragment, nullValue());
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:25,代码来源:VectorHighlighterTests.java

示例9: doTestDocValueRangeQueries

import org.apache.lucene.index.DirectoryReader; //导入依赖的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

示例10: search

import org.apache.lucene.index.DirectoryReader; //导入依赖的package包/类
/**
 * Search sample. 
 * 
 * @param directory the index directory.
 * @throws IOException in case of I/O failure.
 * @throws ParseException in case of Query parse exception.
 */	
public static void search(Directory directory) throws IOException, ParseException {
	IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));
	
	Query query = new QueryParser("title", new StandardAnalyzer()).parse("title:Solr");
	TopDocs matches = searcher.search(query, 10);
	
	System.out.println("Search returned " + matches.totalHits + " matches.");
	Arrays.stream(matches.scoreDocs)
		.map(scoreDoc -> luceneDoc(scoreDoc, searcher))
		.forEach(doc -> {
			System.out.println("-------------------------------------");				
			System.out.println("ID:\t" + doc.get("id"));
			System.out.println("TITLE:\t" + doc.get("title"));
			System.out.println("AUTHOR:\t" + doc.get("author"));
			System.out.println("SCORE:\t" + doc.get("score"));
			
		});
}
 
开发者ID:agazzarini,项目名称:as-full-text-search-server,代码行数:26,代码来源:LuceneBasicFlowExample.java

示例11: rewrite

import org.apache.lucene.index.DirectoryReader; //导入依赖的package包/类
@Override
public Query rewrite(IndexReader reader) throws IOException {
    if (getBoost() != 1.0F) {
        return super.rewrite(reader);
    }
    if (reader instanceof DirectoryReader) {
        String joinField = ParentFieldMapper.joinField(parentType);
        IndexSearcher indexSearcher = new IndexSearcher(reader);
        indexSearcher.setQueryCache(null);
        indexSearcher.setSimilarity(similarity);
        IndexParentChildFieldData indexParentChildFieldData = parentChildIndexFieldData.loadGlobal((DirectoryReader) reader);
        MultiDocValues.OrdinalMap ordinalMap = ParentChildIndexFieldData.getOrdinalMap(indexParentChildFieldData, parentType);
        return JoinUtil.createJoinQuery(joinField, innerQuery, toQuery, indexSearcher, scoreMode, ordinalMap, minChildren, maxChildren);
    } else {
        if (reader.leaves().isEmpty() && reader.numDocs() == 0) {
            // asserting reader passes down a MultiReader during rewrite which makes this
            // blow up since for this query to work we have to have a DirectoryReader otherwise
            // we can't load global ordinals - for this to work we simply check if the reader has no leaves
            // and rewrite to match nothing
            return new MatchNoDocsQuery();
        }
        throw new IllegalStateException("can't load global ordinals for reader of type: " + reader.getClass() + " must be a DirectoryReader");
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:25,代码来源:HasChildQueryParser.java

示例12: search

import org.apache.lucene.index.DirectoryReader; //导入依赖的package包/类
public SearchResult search(String index, String queryString, int page) {
    SearchResult searchResult = null;

    try {
        IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(Properties.getProperties().getProperty(Values.INDEX_LOCATION, Values.DEFAULT_INDEX_LOCATION))));
        IndexSearcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new StandardAnalyzer();

        // Search over the titles only for the moment
        QueryParser parser = new QueryParser(index, analyzer);
        Query query = parser.parse(queryString);

        searchResult = this.doPagingSearch(reader, searcher, query, queryString, page);
        reader.close();
    }
    catch(Exception ex) {}

    return searchResult;
}
 
开发者ID:boyter,项目名称:freemoz,代码行数:20,代码来源:Searcher.java

示例13: testAddingAClosedReader

import org.apache.lucene.index.DirectoryReader; //导入依赖的package包/类
public void testAddingAClosedReader() throws Exception {
    LeafReader reader;
    try (Directory dir = newDirectory();
            RandomIndexWriter writer = new RandomIndexWriter(random(), dir)) {
        writer.addDocument(new Document());
        try (DirectoryReader dirReader = ElasticsearchDirectoryReader.wrap(writer.getReader(), new ShardId("index1", "_na_", 1))) {
            reader = dirReader.leaves().get(0).reader();
        }
    }
    ShardCoreKeyMap map = new ShardCoreKeyMap();
    try {
        map.add(reader);
        fail("Expected AlreadyClosedException");
    } catch (AlreadyClosedException e) {
        // What we wanted
    }
    assertEquals(0, map.size());
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:19,代码来源:ShardCoreKeyMapTests.java

示例14: FbEntitySearcher

import org.apache.lucene.index.DirectoryReader; //导入依赖的package包/类
public FbEntitySearcher(String indexDir, int numOfDocs, String searchingStrategy) throws IOException {

    LogInfo.begin_track("Constructing Searcher");
    if (!searchingStrategy.equals("exact") && !searchingStrategy.equals("inexact"))
      throw new RuntimeException("Bad searching strategy: " + searchingStrategy);
    this.searchStrategy = searchingStrategy;

    queryParser = new QueryParser(
        Version.LUCENE_44,
        FbIndexField.TEXT.fieldName(),
        searchingStrategy.equals("exact") ? new KeywordAnalyzer() : new StandardAnalyzer(Version.LUCENE_44));
    LogInfo.log("Opening index dir: " + indexDir);
    IndexReader indexReader = DirectoryReader.open(SimpleFSDirectory.open(new File(indexDir)));
    indexSearcher = new IndexSearcher(indexReader);
    LogInfo.log("Opened index with " + indexReader.numDocs() + " documents.");

    this.numOfDocs = numOfDocs;
    LogInfo.end_track();
  }
 
开发者ID:cgraywang,项目名称:TextHIN,代码行数:20,代码来源:FbEntitySearcher.java

示例15: facetsWithSearch

import org.apache.lucene.index.DirectoryReader; //导入依赖的package包/类
/** User runs a query and counts facets. */
private List<FacetResult> facetsWithSearch() throws IOException {
  DirectoryReader indexReader = DirectoryReader.open(indexDir);
  IndexSearcher searcher = new IndexSearcher(indexReader);
  TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

  FacetsCollector fc = new FacetsCollector();

  // MatchAllDocsQuery is for "browsing" (counts facets
  // for all non-deleted docs in the index); normally
  // you'd use a "normal" query:
  FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);

  // Retrieve results
  List<FacetResult> results = new ArrayList<FacetResult>();

  // Count both "Publish Date" and "Author" dimensions
  Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
  results.add(facets.getTopChildren(10, "Author"));
  results.add(facets.getTopChildren(10, "Publish Date"));
  
  indexReader.close();
  taxoReader.close();
  
  return results;
}
 
开发者ID:skeychen,项目名称:dswork,代码行数:27,代码来源:SimpleFacetsExample.java


注:本文中的org.apache.lucene.index.DirectoryReader类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。