use of org.apache.lucene.facet.taxonomy.TaxonomyWriter in project lucene-solr by apache.
the class TestMultipleIndexFields method testTwoCustomsSameField.
@Test
public void testTwoCustomsSameField() throws Exception {
Directory indexDir = newDirectory();
Directory taxoDir = newDirectory();
// create and open an index writer
RandomIndexWriter iw = new RandomIndexWriter(random(), indexDir, newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
// create and open a taxonomy writer
TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
FacetsConfig config = getConfig();
config.setIndexFieldName("Band", "$music");
config.setIndexFieldName("Composer", "$music");
seedIndex(tw, iw, config);
IndexReader ir = iw.getReader();
tw.commit();
// prepare index reader and taxonomy.
TaxonomyReader tr = new DirectoryTaxonomyReader(taxoDir);
// prepare searcher to search against
IndexSearcher searcher = newSearcher(ir);
FacetsCollector sfc = performSearch(tr, ir, searcher);
Map<String, Facets> facetsMap = new HashMap<>();
Facets facets2 = getTaxonomyFacetCounts(tr, config, sfc, "$music");
facetsMap.put("Band", facets2);
facetsMap.put("Composer", facets2);
Facets facets = new MultiFacets(facetsMap, getTaxonomyFacetCounts(tr, config, sfc));
// Obtain facets results and hand-test them
assertCorrectResults(facets);
assertOrdinalsExist("$facets", ir);
assertOrdinalsExist("$music", ir);
assertOrdinalsExist("$music", ir);
iw.close();
IOUtils.close(tr, ir, tw, indexDir, taxoDir);
}
use of org.apache.lucene.facet.taxonomy.TaxonomyWriter in project lucene-solr by apache.
the class TestMultipleIndexFields method testDefault.
@Test
public void testDefault() throws Exception {
Directory indexDir = newDirectory();
Directory taxoDir = newDirectory();
// create and open an index writer
RandomIndexWriter iw = new RandomIndexWriter(random(), indexDir, newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
// create and open a taxonomy writer
TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
FacetsConfig config = getConfig();
seedIndex(tw, iw, config);
IndexReader ir = iw.getReader();
tw.commit();
// prepare index reader and taxonomy.
TaxonomyReader tr = new DirectoryTaxonomyReader(taxoDir);
// prepare searcher to search against
IndexSearcher searcher = newSearcher(ir);
FacetsCollector sfc = performSearch(tr, ir, searcher);
// Obtain facets results and hand-test them
assertCorrectResults(getTaxonomyFacetCounts(tr, config, sfc));
assertOrdinalsExist("$facets", ir);
iw.close();
IOUtils.close(tr, ir, tw, indexDir, taxoDir);
}
use of org.apache.lucene.facet.taxonomy.TaxonomyWriter in project lucene-solr by apache.
the class TestMultipleIndexFields method testDifferentFieldsAndText.
@Test
public void testDifferentFieldsAndText() throws Exception {
Directory indexDir = newDirectory();
Directory taxoDir = newDirectory();
// create and open an index writer
RandomIndexWriter iw = new RandomIndexWriter(random(), indexDir, newIndexWriterConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)));
// create and open a taxonomy writer
TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
FacetsConfig config = getConfig();
config.setIndexFieldName("Band", "$bands");
config.setIndexFieldName("Composer", "$composers");
seedIndex(tw, iw, config);
IndexReader ir = iw.getReader();
tw.commit();
// prepare index reader and taxonomy.
TaxonomyReader tr = new DirectoryTaxonomyReader(taxoDir);
// prepare searcher to search against
IndexSearcher searcher = newSearcher(ir);
FacetsCollector sfc = performSearch(tr, ir, searcher);
Map<String, Facets> facetsMap = new HashMap<>();
facetsMap.put("Band", getTaxonomyFacetCounts(tr, config, sfc, "$bands"));
facetsMap.put("Composer", getTaxonomyFacetCounts(tr, config, sfc, "$composers"));
Facets facets = new MultiFacets(facetsMap, getTaxonomyFacetCounts(tr, config, sfc));
// Obtain facets results and hand-test them
assertCorrectResults(facets);
assertOrdinalsExist("$facets", ir);
assertOrdinalsExist("$bands", ir);
assertOrdinalsExist("$composers", ir);
iw.close();
IOUtils.close(tr, ir, tw, indexDir, taxoDir);
}
use of org.apache.lucene.facet.taxonomy.TaxonomyWriter in project searchcode-server by boyter.
the class CodeIndexer method indexDocuments.
/**
* Given a queue of documents to index, index them by popping the queue limited to default of 1000 items.
* This method must be synchronized as we have not added any logic to deal with multiple threads writing to the
* index.
* TODO investigate how Lucene deals with multiple writes
*/
public synchronized void indexDocuments(Queue<CodeIndexDocument> codeIndexDocumentQueue) throws IOException {
Directory indexDirectory = FSDirectory.open(this.INDEX_LOCATION);
Directory facetDirectory = FSDirectory.open(this.FACET_LOCATION);
Analyzer analyzer = new CodeAnalyzer();
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
FacetsConfig facetsConfig;
indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
IndexWriter writer = new IndexWriter(indexDirectory, indexWriterConfig);
TaxonomyWriter taxonomyWriter = new DirectoryTaxonomyWriter(facetDirectory);
try {
CodeIndexDocument codeIndexDocument = codeIndexDocumentQueue.poll();
int count = 0;
while (codeIndexDocument != null) {
Singleton.getLogger().info("Indexing file " + codeIndexDocument.getRepoLocationRepoNameLocationFilename());
this.sharedService.decrementCodeIndexLinesCount(codeIndexDocument.getCodeLines());
facetsConfig = new FacetsConfig();
facetsConfig.setIndexFieldName(Values.LANGUAGENAME, Values.LANGUAGENAME);
facetsConfig.setIndexFieldName(Values.REPONAME, Values.REPONAME);
facetsConfig.setIndexFieldName(Values.CODEOWNER, Values.CODEOWNER);
Document doc = this.buildDocument(codeIndexDocument);
writer.updateDocument(new Term(Values.PATH, codeIndexDocument.getRepoLocationRepoNameLocationFilename()), facetsConfig.build(taxonomyWriter, doc));
count++;
if (count >= INDEX_QUEUE_BATCH_SIZE) {
codeIndexDocument = null;
} else {
codeIndexDocument = codeIndexDocumentQueue.poll();
}
}
} finally {
try {
writer.close();
} finally {
taxonomyWriter.close();
}
Singleton.getLogger().info("Closing writers");
}
}
Aggregations