All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.browseengine.bobo.facets.attribute.AttributesFacetHandler Maven / Gradle / Ivy

There is a newer version: 3.1.2
Show newest version
/**
 * This software is licensed to you under the Apache License, Version 2.0 (the
 * "Apache License").
 *
 * LinkedIn's contributions are made under the Apache License. If you contribute
 * to the Software, the contributions will be deemed to have been made under the
 * Apache License, unless you expressly indicate otherwise. Please do not make any
 * contributions that would be inconsistent with the Apache License.
 *
 * You may obtain a copy of the Apache License at http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, this software
 * distributed under the Apache License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Apache
 * License for the specific language governing permissions and limitations for the
 * software governed under the Apache License.
 *
 * © 2012 LinkedIn Corp. All Rights Reserved.  
 */

package com.browseengine.bobo.facets.attribute;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;

import org.apache.lucene.index.Term;

import com.browseengine.bobo.api.BoboIndexReader;
import com.browseengine.bobo.api.BrowseSelection;
import com.browseengine.bobo.api.FacetSpec;
import com.browseengine.bobo.facets.FacetCountCollector;
import com.browseengine.bobo.facets.FacetCountCollectorSource;
import com.browseengine.bobo.facets.data.MultiValueFacetDataCache;
import com.browseengine.bobo.facets.data.TermListFactory;
import com.browseengine.bobo.facets.filter.RandomAccessFilter;
import com.browseengine.bobo.facets.range.MultiRangeFacetHandler;

public class AttributesFacetHandler extends MultiRangeFacetHandler {
  public static final char DEFAULT_SEPARATOR = '=';
  private char separator;
  private int numFacetsPerKey = 7;
  public static final String SEPARATOR_PROP_NAME = "separator";
  public static final String MAX_FACETS_PER_KEY_PROP_NAME = "maxFacetsPerKey";
  
  public AttributesFacetHandler(String name, String indexFieldName, TermListFactory termListFactory, Term sizePayloadTerm, Map facetProps) {
    super(name, indexFieldName, sizePayloadTerm, termListFactory, Collections.EMPTY_LIST);
    if (facetProps.containsKey(SEPARATOR_PROP_NAME)) {
      this.separator = narrow(facetProps.get(SEPARATOR_PROP_NAME)).charAt(0); 
    } else {
      this.separator = DEFAULT_SEPARATOR;
    }
    if (facetProps.containsKey(MAX_FACETS_PER_KEY_PROP_NAME)) {
      this.numFacetsPerKey = Integer.parseInt(narrow(facetProps.get(MAX_FACETS_PER_KEY_PROP_NAME))); 
    }
  }
  private String narrow(String string) {   
    return string.replaceAll("\\[", "").replaceAll("\\]", "");
  }
  public char getSeparator(BrowseSelection browseSelection) {
    if (browseSelection == null || !browseSelection.getSelectionProperties().containsKey(SEPARATOR_PROP_NAME)) {
      return separator;
    }
    return browseSelection.getSelectionProperties().get(SEPARATOR_PROP_NAME).toString().charAt(0);
  }
  
  @Override
  public RandomAccessFilter buildRandomAccessFilter(String value, Properties prop) throws IOException {    
    return super.buildRandomAccessFilter(convertToRangeString(value, separator), prop);
  }
  public static String convertToRangeString(String key, char separator) {
    if (key.startsWith("[") && key.contains(" TO ")) {
      return key;
    }
    return "[" + key + separator + " TO " + key + (char)(separator + 1) + ")";
  }
  @Override
  public RandomAccessFilter buildRandomAccessOrFilter(final String[] vals, Properties prop, boolean isNot) throws IOException {
    String[] ranges = new String [vals.length];
    for (int i = 0; i < vals.length; i++) {
      ranges[i] = convertToRangeString(vals[i], separator);
    }
    return super.buildRandomAccessOrFilter(ranges, prop, isNot);
  }
  
  
  
  public int getFacetsPerKey(BrowseSelection browseSelection) {
    if (browseSelection == null || !browseSelection.getSelectionProperties().containsKey(MAX_FACETS_PER_KEY_PROP_NAME)) {
      return numFacetsPerKey;
    }
    return Integer.valueOf(browseSelection.getSelectionProperties().get(MAX_FACETS_PER_KEY_PROP_NAME).toString());
  }
  
  @Override
  public FacetCountCollectorSource getFacetCountCollectorSource(final BrowseSelection browseSelection, final FacetSpec ospec){
   
    return new FacetCountCollectorSource(){
    
    @Override
    public FacetCountCollector getFacetCountCollector(
        BoboIndexReader reader, int docBase) {
      int facetsPerKey = getFacetsPerKey(browseSelection);
      if (ospec.getProperties() != null && ospec.getProperties().containsKey(MAX_FACETS_PER_KEY_PROP_NAME)) {
        facetsPerKey = Integer.parseInt(ospec.getProperties().get(MAX_FACETS_PER_KEY_PROP_NAME));
      }
      MultiValueFacetDataCache dataCache = (MultiValueFacetDataCache) reader.getFacetData(_name);
      return new AttributesFacetCountCollector(AttributesFacetHandler.this, _name,dataCache,docBase,browseSelection, ospec, facetsPerKey, getSeparator(browseSelection));
    }
  };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy