Java Code Examples for org.apache.lucene.facet.FacetsConfig#setIndexFieldName()

The following examples show how to use org.apache.lucene.facet.FacetsConfig#setIndexFieldName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AssociationsFacetsExample.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Empty constructor */
public AssociationsFacetsExample() {
  config = new FacetsConfig();
  config.setMultiValued("tags", true);
  config.setIndexFieldName("tags", "$tags");
  config.setMultiValued("genre", true);
  config.setIndexFieldName("genre", "$genre");
}
 
Example 2
Source File: TestTaxonomyFacetAssociations.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  dir = newDirectory();
  taxoDir = newDirectory();
  // preparations - index, taxonomy, content
  
  TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);

  // Cannot mix ints & floats in the same indexed field:
  config = new FacetsConfig();
  config.setIndexFieldName("int", "$facets.int");
  config.setMultiValued("int", true);
  config.setIndexFieldName("float", "$facets.float");
  config.setMultiValued("float", true);

  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);

  // index documents, 50% have only 'b' and all have 'a'
  for (int i = 0; i < 110; i++) {
    Document doc = new Document();
    // every 11th document is added empty, this used to cause the association
    // aggregators to go into an infinite loop
    if (i % 11 != 0) {
      doc.add(new IntAssociationFacetField(2, "int", "a"));
      doc.add(new FloatAssociationFacetField(0.5f, "float", "a"));
      if (i % 2 == 0) { // 50
        doc.add(new IntAssociationFacetField(3, "int", "b"));
        doc.add(new FloatAssociationFacetField(0.2f, "float", "b"));
      }
    }
    writer.addDocument(config.build(taxoWriter, doc));
  }
  
  taxoWriter.close();
  reader = writer.getReader();
  writer.close();
  taxoReader = new DirectoryTaxonomyReader(taxoDir);
}
 
Example 3
Source File: TestTaxonomyFacetCounts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSeparateIndexedFields() throws Exception {
  Directory indexDir = newDirectory();
  Directory taxoDir = newDirectory();
  
  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
  FacetsConfig config = new FacetsConfig();
  config.setIndexFieldName("b", "$b");
  
  for(int i = atLeast(30); i > 0; --i) {
    Document doc = new Document();
    doc.add(new StringField("f", "v", Field.Store.NO));
    doc.add(new FacetField("a", "1"));
    doc.add(new FacetField("b", "1"));
    iw.addDocument(config.build(taxoWriter, doc));
  }
  
  DirectoryReader r = DirectoryReader.open(iw);
  DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
  
  FacetsCollector sfc = new FacetsCollector();
  newSearcher(r).search(new MatchAllDocsQuery(), sfc);
  Facets facets1 = getTaxonomyFacetCounts(taxoReader, config, sfc);
  Facets facets2 = getTaxonomyFacetCounts(taxoReader, config, sfc, "$b");
  assertEquals(r.maxDoc(), facets1.getTopChildren(10, "a").value.intValue());
  assertEquals(r.maxDoc(), facets2.getTopChildren(10, "b").value.intValue());
  iw.close();
  IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
 
Example 4
Source File: TestTaxonomyFacetSumValueSource.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testCountAndSumScore() throws Exception {
  Directory indexDir = newDirectory();
  Directory taxoDir = newDirectory();
  
  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
  FacetsConfig config = new FacetsConfig();
  config.setIndexFieldName("b", "$b");
  
  for(int i = atLeast(30); i > 0; --i) {
    Document doc = new Document();
    doc.add(new StringField("f", "v", Field.Store.NO));
    doc.add(new FacetField("a", "1"));
    doc.add(new FacetField("b", "1"));
    iw.addDocument(config.build(taxoWriter, doc));
  }
  
  DirectoryReader r = DirectoryReader.open(iw);
  DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
  
  FacetsCollector fc = new FacetsCollector(true);
  FacetsCollector.search(newSearcher(r), new MatchAllDocsQuery(), 10, fc);
  
  Facets facets1 = getTaxonomyFacetCounts(taxoReader, config, fc);
  Facets facets2 = new TaxonomyFacetSumValueSource(new DocValuesOrdinalsReader("$b"), taxoReader, config, fc, DoubleValuesSource.SCORES);

  assertEquals(r.maxDoc(), facets1.getTopChildren(10, "a").value.intValue());
  assertEquals(r.maxDoc(), facets2.getTopChildren(10, "b").value.doubleValue(), 1E-10);
  iw.close();
  IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
 
Example 5
Source File: IndexBuilderLuceneImpl.java    From yes-cart with Apache License 2.0 5 votes vote down vote up
/**
 * Process single entity update in the FT index.
 *
 * @param iw         index writer
 * @param indexName  index name
 * @param documents  documents to index and PK
 * @param remove     remove only
 * @param indexTime  time of this index (added as field to added documents)
 * @param counts     counts[3] = { added, removed, failed }
 *
 * @throws IOException error
 */
protected void fullTextSearchReindexSingleEntity(final IndexWriter iw,
                                                  final String indexName,
                                                  final Pair<PK, Document[]> documents,
                                                  final boolean remove,
                                                  final long indexTime,
                                                  final long[] counts) throws IOException {

    final PK primaryKey = documents.getFirst();

    // Remove all documents with primary key (could be multiple)
    iw.deleteDocuments(new Term(AdapterUtils.FIELD_PK, String.valueOf(primaryKey)));
    counts[1]++;
    LOGFTQ.trace("Removing {} document _PK:{}", indexName, primaryKey);

    if (!remove) {
        // Add documents
        final FacetsConfig facetsConfig = new FacetsConfig();
        for (final Document document : documents.getSecond()) {
            try {
                LuceneDocumentAdapterUtils.addNumericField(document, AdapterUtils.FIELD_INDEXTIME, indexTime, false);
                for (final IndexableField ixf : document) {
                    if (ixf.fieldType() == SortedSetDocValuesFacetField.TYPE) {
                        SortedSetDocValuesFacetField facetField = (SortedSetDocValuesFacetField) ixf;
                        facetsConfig.setIndexFieldName(facetField.dim, facetField.dim);
                        facetsConfig.setMultiValued(facetField.dim, true); // TODO: revisit this but for now all fields assumed to have multivalue
                    }
                }
                iw.addDocument(facetsConfig.build(document));
                counts[0]++;
            } catch (Exception sde) {
                LOGFTQ.error("Updating {} document _PK:{} failed ... cause: {}", indexName, documents.getFirst(), sde.getMessage());
                counts[2]++;
            }
        }
        LOGFTQ.trace("Updating {} document _PK:{}", indexName, primaryKey);
    }

}
 
Example 6
Source File: TestTaxonomyFacetCounts.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testWrongIndexFieldName() throws Exception {
  Directory dir = newDirectory();
  Directory taxoDir = newDirectory();

  // Writes facet ords to a separate directory from the
  // main index:
  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);

  FacetsConfig config = new FacetsConfig();
  config.setIndexFieldName("a", "$facets2");
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);

  Document doc = new Document();
  doc.add(new FacetField("a", "foo1"));
  writer.addDocument(config.build(taxoWriter, doc));

  // NRT open
  IndexSearcher searcher = newSearcher(writer.getReader());

  // NRT open
  TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);

  FacetsCollector c = new FacetsCollector();
  searcher.search(new MatchAllDocsQuery(), c);

  // Uses default $facets field:
  Facets facets;
  if (random().nextBoolean()) {
    facets = new FastTaxonomyFacetCounts(taxoReader, config, c);
  } else {
    OrdinalsReader ordsReader = new DocValuesOrdinalsReader();
    if (random().nextBoolean()) {
      ordsReader = new CachedOrdinalsReader(ordsReader);
    }
    facets = new TaxonomyFacetCounts(ordsReader, taxoReader, config, c);
  }

  // Ask for top 10 labels for any dims that have counts:
  List<FacetResult> results = facets.getAllDims(10);
  assertTrue(results.isEmpty());

  expectThrows(IllegalArgumentException.class, () -> {
    facets.getSpecificValue("a");
  });

  expectThrows(IllegalArgumentException.class, () -> {
    facets.getTopChildren(10, "a");
  });

  writer.close();
  IOUtils.close(taxoWriter, searcher.getIndexReader(), taxoReader, taxoDir, dir);
}
 
Example 7
Source File: TestTaxonomyFacetSumValueSource.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testWrongIndexFieldName() throws Exception {

    Directory dir = newDirectory();
    Directory taxoDir = newDirectory();

    // Writes facet ords to a separate directory from the
    // main index:
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);

    FacetsConfig config = new FacetsConfig();
    config.setIndexFieldName("a", "$facets2");

    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);

    Document doc = new Document();
    doc.add(new NumericDocValuesField("num", 10));
    doc.add(new FacetField("a", "foo1"));
    writer.addDocument(config.build(taxoWriter, doc));

    // NRT open
    IndexSearcher searcher = newSearcher(writer.getReader());
    writer.close();

    // NRT open
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
    taxoWriter.close();

    FacetsCollector c = new FacetsCollector();
    searcher.search(new MatchAllDocsQuery(), c);    

    TaxonomyFacetSumValueSource facets = new TaxonomyFacetSumValueSource(taxoReader, config, c, DoubleValuesSource.fromIntField("num"));

    // Ask for top 10 labels for any dims that have counts:
    List<FacetResult> results = facets.getAllDims(10);
    assertTrue(results.isEmpty());

    expectThrows(IllegalArgumentException.class, () -> {
      facets.getSpecificValue("a");
    });

    expectThrows(IllegalArgumentException.class, () -> {
      facets.getTopChildren(10, "a");
    });

    IOUtils.close(searcher.getIndexReader(), taxoReader, dir, taxoDir);
  }