use of org.apache.lucene.search.spell.Dictionary in project lucene-solr by apache.
the class DocumentValueSourceDictionaryTest method testValueSourceEmptyReader.
@Test
public void testValueSourceEmptyReader() throws IOException {
Directory dir = newDirectory();
Analyzer analyzer = new MockAnalyzer(random());
IndexWriterConfig iwc = newIndexWriterConfig(analyzer);
iwc.setMergePolicy(newLogMergePolicy());
// Make sure the index is created?
RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
writer.commit();
writer.close();
IndexReader ir = DirectoryReader.open(dir);
Dictionary dictionary = new DocumentValueSourceDictionary(ir, FIELD_NAME, LongValuesSource.constant(10), PAYLOAD_FIELD_NAME);
InputIterator inputIterator = dictionary.getEntryIterator();
assertNull(inputIterator.next());
assertEquals(inputIterator.weight(), 0);
assertNull(inputIterator.payload());
IOUtils.close(ir, analyzer, dir);
}
use of org.apache.lucene.search.spell.Dictionary in project lucene-solr by apache.
the class DocumentDictionaryTest method testWithDeletions.
@Test
public void testWithDeletions() throws IOException {
Directory dir = newDirectory();
Analyzer analyzer = new MockAnalyzer(random());
IndexWriterConfig iwc = newIndexWriterConfig(analyzer);
iwc.setMergePolicy(newLogMergePolicy());
RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
Map.Entry<List<String>, Map<String, Document>> res = generateIndexDocuments(atLeast(1000), false);
Map<String, Document> docs = res.getValue();
List<String> invalidDocTerms = res.getKey();
Random rand = random();
List<String> termsToDel = new ArrayList<>();
for (Document doc : docs.values()) {
IndexableField f = doc.getField(FIELD_NAME);
if (rand.nextBoolean() && f != null && !invalidDocTerms.contains(f.stringValue())) {
termsToDel.add(doc.get(FIELD_NAME));
}
writer.addDocument(doc);
}
writer.commit();
Term[] delTerms = new Term[termsToDel.size()];
for (int i = 0; i < termsToDel.size(); i++) {
delTerms[i] = new Term(FIELD_NAME, termsToDel.get(i));
}
for (Term delTerm : delTerms) {
writer.deleteDocuments(delTerm);
}
writer.commit();
writer.close();
for (String termToDel : termsToDel) {
assertTrue(null != docs.remove(termToDel));
}
IndexReader ir = DirectoryReader.open(dir);
assertEquals(ir.numDocs(), docs.size());
Dictionary dictionary = new DocumentDictionary(ir, FIELD_NAME, WEIGHT_FIELD_NAME);
InputIterator inputIterator = dictionary.getEntryIterator();
BytesRef f;
while ((f = inputIterator.next()) != null) {
Document doc = docs.remove(f.utf8ToString());
assertTrue(f.equals(new BytesRef(doc.get(FIELD_NAME))));
IndexableField weightField = doc.getField(WEIGHT_FIELD_NAME);
assertEquals(inputIterator.weight(), (weightField != null) ? weightField.numericValue().longValue() : 0);
assertNull(inputIterator.payload());
}
for (String invalidTerm : invalidDocTerms) {
assertNotNull(docs.remove(invalidTerm));
}
assertTrue(docs.isEmpty());
IOUtils.close(ir, analyzer, dir);
}
use of org.apache.lucene.search.spell.Dictionary in project lucene-solr by apache.
the class DocumentDictionaryTest method testEmptyReader.
@Test
public void testEmptyReader() throws IOException {
Directory dir = newDirectory();
Analyzer analyzer = new MockAnalyzer(random());
IndexWriterConfig iwc = newIndexWriterConfig(analyzer);
iwc.setMergePolicy(newLogMergePolicy());
// Make sure the index is created?
RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
writer.commit();
writer.close();
IndexReader ir = DirectoryReader.open(dir);
Dictionary dictionary = new DocumentDictionary(ir, FIELD_NAME, WEIGHT_FIELD_NAME, PAYLOAD_FIELD_NAME);
InputIterator inputIterator = dictionary.getEntryIterator();
assertNull(inputIterator.next());
assertEquals(inputIterator.weight(), 0);
assertNull(inputIterator.payload());
IOUtils.close(ir, analyzer, dir);
}
use of org.apache.lucene.search.spell.Dictionary in project lucene-solr by apache.
the class TestHighFrequencyDictionary method testEmpty.
public void testEmpty() throws Exception {
Directory dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
writer.commit();
writer.close();
IndexReader ir = DirectoryReader.open(dir);
Dictionary dictionary = new HighFrequencyDictionary(ir, "bogus", 0.1f);
BytesRefIterator tf = dictionary.getEntryIterator();
assertNull(tf.next());
dir.close();
}
use of org.apache.lucene.search.spell.Dictionary in project jackrabbit-oak by apache.
the class SuggestHelper method updateSuggester.
public static void updateSuggester(Directory directory, Analyzer analyzer, IndexReader reader) throws IOException {
File tempDir = null;
try {
//Analyzing infix suggester takes a file parameter. It uses its path to getDirectory()
//for actual storage of suggester data. BUT, while building it also does getDirectory() to
//a temporary location (original path + ".tmp"). So, instead we create a temp dir and also
//create a placeholder non-existing-sub-child which would mark the location when we want to return
//our internal suggestion OakDirectory. After build is done, we'd delete the temp directory
//thereby removing any temp stuff that suggester created in the interim.
tempDir = Files.createTempDir();
File tempSubChild = new File(tempDir, "non-existing-sub-child");
if (reader.getDocCount(FieldNames.SUGGEST) > 0) {
Dictionary dictionary = new LuceneDictionary(reader, FieldNames.SUGGEST);
getLookup(directory, analyzer, tempSubChild).build(dictionary);
}
} catch (RuntimeException e) {
log.debug("could not update the suggester", e);
} finally {
//cleanup temp dir
if (tempDir != null && !FileUtils.deleteQuietly(tempDir)) {
log.error("Cleanup failed for temp dir {}", tempDir.getAbsolutePath());
}
}
}
Aggregations