本文整理匯總了Java中org.apache.lucene.index.IndexReader類的典型用法代碼示例。如果您正苦於以下問題:Java IndexReader類的具體用法?Java IndexReader怎麽用?Java IndexReader使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
IndexReader類屬於org.apache.lucene.index包,在下文中一共展示了IndexReader類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testScriptedMetricWithCombine
import org.apache.lucene.index.IndexReader; //導入依賴的package包/類
/**
* test that combine script sums the list produced by the "mapScript"
*/
public void testScriptedMetricWithCombine() throws IOException {
try (Directory directory = newDirectory()) {
Integer numDocs = randomInt(100);
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
for (int i = 0; i < numDocs; i++) {
indexWriter.addDocument(singleton(new SortedNumericDocValuesField("number", i)));
}
}
try (IndexReader indexReader = DirectoryReader.open(directory)) {
ScriptedMetricAggregationBuilder aggregationBuilder = new ScriptedMetricAggregationBuilder(AGG_NAME);
aggregationBuilder.initScript(INIT_SCRIPT).mapScript(MAP_SCRIPT).combineScript(COMBINE_SCRIPT);
ScriptedMetric scriptedMetric = search(newSearcher(indexReader, true, true), new MatchAllDocsQuery(), aggregationBuilder);
assertEquals(AGG_NAME, scriptedMetric.getName());
assertNotNull(scriptedMetric.aggregation());
assertEquals(numDocs, scriptedMetric.aggregation());
}
}
}
示例2: testRandom
import org.apache.lucene.index.IndexReader; //導入依賴的package包/類
public void testRandom() throws IOException {
final int numDocs = randomIntBetween(10, 200);
final Document doc = new Document();
final Directory dir = newDirectory();
final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
for (int i = 0; i < numDocs; ++i) {
w.addDocument(doc);
}
final IndexReader reader = w.getReader();
final IndexSearcher searcher = newSearcher(reader);
for (int i = 0; i <= numDocs; ++i) {
assertEquals(numDocs - i, searcher.count(new MinDocQuery(i)));
}
w.close();
reader.close();
dir.close();
}
示例3: countTestCase
import org.apache.lucene.index.IndexReader; //導入依賴的package包/類
private void countTestCase(Query query, IndexReader reader, boolean shouldCollect) throws Exception {
TestSearchContext context = new TestSearchContext(null);
context.parsedQuery(new ParsedQuery(query));
context.setSize(0);
context.setTask(new SearchTask(123L, "", "", "", null));
IndexSearcher searcher = new IndexSearcher(reader);
final AtomicBoolean collected = new AtomicBoolean();
IndexSearcher contextSearcher = new IndexSearcher(reader) {
protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException {
collected.set(true);
super.search(leaves, weight, collector);
}
};
final boolean rescore = QueryPhase.execute(context, contextSearcher);
assertFalse(rescore);
assertEquals(searcher.count(query), context.queryResult().topDocs().totalHits);
assertEquals(shouldCollect, collected.get());
}
示例4: innerExecute
import org.apache.lucene.index.IndexReader; //導入依賴的package包/類
@Override
public TermSuggestion innerExecute(String name, TermSuggestionContext suggestion, IndexSearcher searcher, CharsRefBuilder spare) throws IOException {
DirectSpellChecker directSpellChecker = SuggestUtils.getDirectSpellChecker(suggestion.getDirectSpellCheckerSettings());
final IndexReader indexReader = searcher.getIndexReader();
TermSuggestion response = new TermSuggestion(
name, suggestion.getSize(), suggestion.getDirectSpellCheckerSettings().sort()
);
List<Token> tokens = queryTerms(suggestion, spare);
for (Token token : tokens) {
// TODO: Extend DirectSpellChecker in 4.1, to get the raw suggested words as BytesRef
SuggestWord[] suggestedWords = directSpellChecker.suggestSimilar(
token.term, suggestion.getShardSize(), indexReader, suggestion.getDirectSpellCheckerSettings().suggestMode()
);
Text key = new Text(new BytesArray(token.term.bytes()));
TermSuggestion.Entry resultEntry = new TermSuggestion.Entry(key, token.startOffset, token.endOffset - token.startOffset);
for (SuggestWord suggestWord : suggestedWords) {
Text word = new Text(suggestWord.string);
resultEntry.addOption(new TermSuggestion.Entry.Option(word, suggestWord.freq, suggestWord.score));
}
response.addTerm(resultEntry);
}
return response;
}
示例5: buildReferenceCountingIndexReader
import org.apache.lucene.index.IndexReader; //導入依賴的package包/類
private IndexReader buildReferenceCountingIndexReader(String id, long size) throws IOException
{
IndexReader reader;
File location = new File(indexDirectory, id).getCanonicalFile();
double folderSize = getSizeInMb(location);
if (IndexReader.indexExists(location))
{
if ((size < maxDocsForInMemoryIndex) && (folderSize < maxRamInMbForInMemoryIndex))
{
RAMDirectory rd = new RAMDirectory(location);
reader = IndexReader.open(rd);
}
else
{
reader = IndexReader.open(location);
}
}
else
{
reader = IndexReader.open(emptyIndex);
}
reader = ReferenceCountingReadOnlyIndexReaderFactory.createReader(id, reader, true, config);
return reader;
}
示例6: reset
import org.apache.lucene.index.IndexReader; //導入依賴的package包/類
@PostConstruct
public void reset() {
String indexDir = appConfig.getAllSpellCheckerDir();
try {
Directory spellcheckDir = FSDirectory.open(new File(indexDir));
if (!IndexReader.indexExists(spellcheckDir)) {
logger.info("Please reset index firstly!");
return;
}
SpellChecker newSpellChecker = new SpellChecker(spellcheckDir);
newSpellChecker.setStringDistance(new JaroWinklerDistance());
newSpellChecker.setAccuracy(0.7f);
if (spellChecker == null) {
spellChecker = newSpellChecker;
} else {
final Closeable preSpellChecker = spellChecker;
spellChecker = newSpellChecker;
IOUtils.closeQuietly(preSpellChecker);
}
} catch (Exception e) {
logger.error("Exception", e);
}
}
示例7: testSortValues
import org.apache.lucene.index.IndexReader; //導入依賴的package包/類
public void testSortValues() throws Exception {
Directory dir = new RAMDirectory();
IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
for (int i = 0; i < 10; i++) {
Document document = new Document();
String text = new String(new char[]{(char) (97 + i), (char) (97 + i)});
document.add(new TextField("str", text, Field.Store.YES));
document.add(new SortedDocValuesField("str", new BytesRef(text)));
indexWriter.addDocument(document);
}
IndexReader reader = SlowCompositeReaderWrapper.wrap(DirectoryReader.open(indexWriter));
IndexSearcher searcher = new IndexSearcher(reader);
TopFieldDocs docs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("str", SortField.Type.STRING)));
for (int i = 0; i < 10; i++) {
FieldDoc fieldDoc = (FieldDoc) docs.scoreDocs[i];
assertThat((BytesRef) fieldDoc.fields[0], equalTo(new BytesRef(new String(new char[]{(char) (97 + i), (char) (97 + i)}))));
}
}
示例8: rewrite
import org.apache.lucene.index.IndexReader; //導入依賴的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");
}
}
示例9: innerExecute
import org.apache.lucene.index.IndexReader; //導入依賴的package包/類
@Override
public TermSuggestion innerExecute(String name, TermSuggestionContext suggestion, IndexSearcher searcher, CharsRefBuilder spare)
throws IOException {
DirectSpellChecker directSpellChecker = suggestion.getDirectSpellCheckerSettings().createDirectSpellChecker();
final IndexReader indexReader = searcher.getIndexReader();
TermSuggestion response = new TermSuggestion(
name, suggestion.getSize(), suggestion.getDirectSpellCheckerSettings().sort()
);
List<Token> tokens = queryTerms(suggestion, spare);
for (Token token : tokens) {
// TODO: Extend DirectSpellChecker in 4.1, to get the raw suggested words as BytesRef
SuggestWord[] suggestedWords = directSpellChecker.suggestSimilar(
token.term, suggestion.getShardSize(), indexReader, suggestion.getDirectSpellCheckerSettings().suggestMode()
);
Text key = new Text(new BytesArray(token.term.bytes()));
TermSuggestion.Entry resultEntry = new TermSuggestion.Entry(key, token.startOffset, token.endOffset - token.startOffset);
for (SuggestWord suggestWord : suggestedWords) {
Text word = new Text(suggestWord.string);
resultEntry.addOption(new TermSuggestion.Entry.Option(word, suggestWord.freq, suggestWord.score));
}
response.addTerm(resultEntry);
}
return response;
}
示例10: rewrite
import org.apache.lucene.index.IndexReader; //導入依賴的package包/類
@Override
public Query rewrite(IndexReader reader) throws IOException {
FieldMaskingSpanQuery clone = null;
SpanQuery rewritten = (SpanQuery) maskedQuery.rewrite(reader);
if (rewritten != maskedQuery) {
clone = (FieldMaskingSpanQuery) this.clone();
clone.maskedQuery = rewritten;
}
if (clone != null) {
return clone;
} else {
return this;
}
}
示例11: FbEntitySearcher
import org.apache.lucene.index.IndexReader; //導入依賴的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();
}
示例12: testRewriteEmptyReader
import org.apache.lucene.index.IndexReader; //導入依賴的package包/類
public void testRewriteEmptyReader() throws Exception {
IndexService indexService = createIndex("test");
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties")
.startObject("foo")
.field("type", "date")
.endObject()
.endObject()
.endObject().endObject().string();
indexService.mapperService().merge("type",
new CompressedXContent(mapping), MergeReason.MAPPING_UPDATE, false);
IndexReader reader = new MultiReader();
QueryRewriteContext context = new QueryShardContext(0, indexService.getIndexSettings(), null, null, indexService.mapperService(),
null, null, xContentRegistry(), null, reader, null);
RangeQueryBuilder range = new RangeQueryBuilder("foo");
// no values -> DISJOINT
assertEquals(Relation.DISJOINT, range.getRelation(context));
}
示例13: searchRequestToBitSet
import org.apache.lucene.index.IndexReader; //導入依賴的package包/類
private OpenBitSet searchRequestToBitSet(@Nullable final Search searchreq, IndexSearcher searcher,
IndexReader reader) throws IOException
{
if( searchreq != null )
{
Filter filters = getFilter(searchreq);
Query query = getQuery(searchreq, null, false);
BitSetCollector collector = new BitSetCollector();
searcher.search(query, filters, collector);
return collector.getBitSet();
}
else
{
return (OpenBitSet) new InstitutionFilter().getDocIdSet(reader);
}
}
示例14: testNoDocs
import org.apache.lucene.index.IndexReader; //導入依賴的package包/類
public void testNoDocs() throws IOException {
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
// intentionally not writing any docs
indexWriter.close();
IndexReader indexReader = DirectoryReader.open(directory);
testCase(new MatchAllDocsQuery(), newSearcher(indexReader, false, true), parentToChild -> {
assertEquals(0, parentToChild.getDocCount());
assertEquals(Double.POSITIVE_INFINITY, ((InternalMin) parentToChild.getAggregations().get("in_child")).getValue(),
Double.MIN_VALUE);
});
indexReader.close();
directory.close();
}
示例15: testMissingShard
import org.apache.lucene.index.IndexReader; //導入依賴的package包/類
public void testMissingShard() throws IOException {
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
w.addDocument(new Document());
try (IndexReader reader = w.getReader()) {
ShardCoreKeyMap map = new ShardCoreKeyMap();
for (LeafReaderContext ctx : reader.leaves()) {
try {
map.add(ctx.reader());
fail();
} catch (IllegalArgumentException expected) {
// ok
}
}
}
}
}