Java Code Examples for org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField

The following examples show how to use org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField. These examples are extracted from open source projects. 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 Project: lucene-solr   Source File: FacetsConfig.java    License: Apache License 2.0 6 votes vote down vote up
private void processSSDVFacetFields(Map<String,List<SortedSetDocValuesFacetField>> byField, Document doc) throws IOException {
  //System.out.println("process SSDV: " + byField);
  for(Map.Entry<String,List<SortedSetDocValuesFacetField>> ent : byField.entrySet()) {

    String indexFieldName = ent.getKey();
    //System.out.println("  field=" + indexFieldName);

    for(SortedSetDocValuesFacetField facetField : ent.getValue()) {
      FacetLabel cp = new FacetLabel(facetField.dim, facetField.label);
      String fullPath = pathToString(cp.components, cp.length);
      //System.out.println("add " + fullPath);

      // For facet counts:
      doc.add(new SortedSetDocValuesField(indexFieldName, new BytesRef(fullPath)));

      // For drill-down:
      doc.add(new StringField(indexFieldName, fullPath, Field.Store.NO));

      FacetsConfig.DimConfig ft = getDimConfig(facetField.dim);        
      if (ft.requireDimensionDrillDown) {
        doc.add(new StringField(indexFieldName, facetField.dim, Field.Store.NO));
      }
    }
  }
}
 
Example 2
Source Project: yes-cart   Source File: LuceneDocumentAdapterUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Adds a simple facet field (not taxonomy)
 *
 * @param document document
 * @param name     field name
 * @param value    value
 */
public static void addFacetField(final Document document, final String name, final String value) {
    if (value != null) {
        /*
            SortedSetDocValuesFacetField is basic implementation of facets which is kept in the same
            index as the original document, which is what we want most of the time because we want to
            drill down on facets inclusive the search criteria.

            Taxonomy Facets are kept in separate index from the main index because they are stored
            differently in Lucene for efficiency and hence are not suitable.
         */
        document.add(new SortedSetDocValuesFacetField(name, value));
    }
}
 
Example 3
Source Project: yes-cart   Source File: IndexBuilderLuceneImpl.java    License: 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 4
Source Project: lucene-solr   Source File: TestFacetLabel.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testEmptyNullComponents() throws Exception {
  // LUCENE-4724: CategoryPath should not allow empty or null components
  String[][] components_tests = new String[][] {
    new String[] { "", "test" }, // empty in the beginning
    new String[] { "test", "" }, // empty in the end
    new String[] { "test", "", "foo" }, // empty in the middle
    new String[] { null, "test" }, // null at the beginning
    new String[] { "test", null }, // null in the end
    new String[] { "test", null, "foo" }, // null in the middle
  };

  // empty or null components should not be allowed.
  for (String[] components : components_tests) {
    expectThrows(IllegalArgumentException.class, () -> {
      new FacetLabel(components);
    });
    expectThrows(IllegalArgumentException.class, () -> {
      new FacetField("dim", components);
    });
    expectThrows(IllegalArgumentException.class, () -> {
      new AssociationFacetField(new BytesRef(), "dim", components);
    });
    expectThrows(IllegalArgumentException.class, () -> {
      new IntAssociationFacetField(17, "dim", components);
    });
    expectThrows(IllegalArgumentException.class, () -> {
      new FloatAssociationFacetField(17.0f, "dim", components);
    });
  }

  expectThrows(IllegalArgumentException.class, () -> {
    new FacetField(null, new String[] {"abc"});
  });
  expectThrows(IllegalArgumentException.class, () -> {
    new FacetField("", new String[] {"abc"});
  });
  expectThrows(IllegalArgumentException.class, () -> {
    new IntAssociationFacetField(17, null, new String[] {"abc"});
  });
  expectThrows(IllegalArgumentException.class, () -> {
    new IntAssociationFacetField(17, "", new String[] {"abc"});
  });
  expectThrows(IllegalArgumentException.class, () -> {
    new FloatAssociationFacetField(17.0f, null, new String[] {"abc"});
  });
  expectThrows(IllegalArgumentException.class, () -> {
    new FloatAssociationFacetField(17.0f, "", new String[] {"abc"});
  });
  expectThrows(IllegalArgumentException.class, () -> {
    new AssociationFacetField(new BytesRef(), null, new String[] {"abc"});
  });
  expectThrows(IllegalArgumentException.class, () -> {
    new AssociationFacetField(new BytesRef(), "", new String[] {"abc"});
  });
  expectThrows(IllegalArgumentException.class, () -> {
    new SortedSetDocValuesFacetField(null, "abc");
  });
  expectThrows(IllegalArgumentException.class, () -> {
    new SortedSetDocValuesFacetField("", "abc");
  });
  expectThrows(IllegalArgumentException.class, () -> {
    new SortedSetDocValuesFacetField("dim", null);
  });
  expectThrows(IllegalArgumentException.class, () -> {
    new SortedSetDocValuesFacetField("dim", "");
  });
}