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

com.browseengine.bobo.api.BrowseHit 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.api;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.lucene.document.Document;
import org.apache.lucene.search.Explanation;

/**
 * A hit from a browse
 */
public class BrowseHit
	implements Serializable
{
	private static final long serialVersionUID = 1L;
	
	public static class TermFrequencyVector implements Serializable{

	  private static final long serialVersionUID = 1L;
	  public final String[] terms;
	  public final int[] freqs;
	  
	  public TermFrequencyVector(String[] terms,int[] freqs){
	    this.terms = terms;
	    this.freqs = freqs;
	  }
	}

	/**
	 * Get the score
	 * @return score
	 * @see #setScore(float)
	 */
	public float getScore()
	{
		return score;
	}
	
	/**
	 * Get the field values
	 * @param field field name
	 * @return field value array
	 * @see #getField(String)
	 */
	public String[] getFields(String field)
	{
		return _fieldValues != null ? _fieldValues.get(field) : null;
	}
	
	/**
	 * Get the raw field values
	 * @param field field name
	 * @return field value array
	 * @see #getRawField(String)
	 */
	public Object[] getRawFields(String field)
	{
		return _rawFieldValues != null ? _rawFieldValues.get(field) : null;
	}
	
	/**
	 * Get the field value
	 * @param field field name
	 * @return field value
	 * @see #getFields(String)
	 */
	public String getField(String field)
	{
		String[] fields=getFields(field);
		if (fields!=null && fields.length > 0)
		{
			return fields[0];
		}
		else
		{
			return null;
		}
	}
	
	/**
	 * Get the raw field value
	 * @param field field name
	 * @return raw field value
	 * @see #getRawFields(String)
	 */
	public Object getRawField(String field)
	{
		Object[] fields=getRawFields(field);
		if (fields!=null && fields.length > 0)
		{
			return fields[0];
		}
		else
		{
			return null;
		}
	}
	                       

	private float score;
	private int docid;
	
	private Map _fieldValues;
	private Map _rawFieldValues;
	private transient Comparable _comparable;
	private Document _storedFields;
  private int _groupPosition; // the position of the _groupField inside groupBy request.
  private String _groupField;
  private String _groupValue;
  private Object _rawGroupValue;
  private int _groupHitsCount;
  private BrowseHit[] _groupHits;
	private Explanation _explanation;
	
	private Map _termFreqMap = new HashMap();
	
	public Map getTermFreqMap(){
	  return _termFreqMap;
	}
	
	public BrowseHit setTermFreqMap(Map termFreqMap){
	  _termFreqMap = termFreqMap;
    return this;
	}

    public int getGroupPosition() {
      return _groupPosition;
    }

    public BrowseHit setGroupPosition(int pos) {
      _groupPosition = pos;
      return this;
    }

    public String getGroupField() {
      return _groupField;
    }

    public BrowseHit setGroupField(String field) {
      _groupField = field;
      return this;
    }

    public String getGroupValue() {
      return _groupValue;
    }

    public BrowseHit setGroupValue(String group) {
      _groupValue = group;
      return this;
    }

    public Object getRawGroupValue() {
      return _rawGroupValue;
    }

    public BrowseHit setRawGroupValue(Object group) {
      _rawGroupValue = group;
      return this;
    }

    public int getGroupHitsCount() {
      return _groupHitsCount;
    }

  public BrowseHit setGroupHitsCount(int count) {
    _groupHitsCount = count;
    return this;
  }

  public BrowseHit[] getGroupHits() {
    return _groupHits;
  }

  public BrowseHit setGroupHits(BrowseHit[] hits) {
    _groupHits = hits;
    return this;
  }
	
	public Explanation getExplanation() {
		return _explanation;
	}

	public BrowseHit setExplanation(Explanation explanation) {
		_explanation = explanation;
    return this;
	}

	public BrowseHit setComparable(Comparable comparable)
	{
	  _comparable = comparable;
    return this;
	}
	
	public Comparable getComparable()
	{
	  return _comparable;
	}
	
	/**
	 * Gets the internal document id
	 * @return document id
	 * @see #setDocid(int)
	 */
	public int getDocid() {
		return docid;
	}
	
	/**
	 * Sets the internal document id
	 *
   * @param docid document id
   * @see #getDocid()
	 */
	public BrowseHit setDocid(int docid) {
		this.docid = docid;
    return this;
	}
	
	/**
	 * Gets the field values
	 * @return field value map
	 * @see #setFieldValues(Map)
	 */
	public Map getFieldValues() {
		return _fieldValues;
	}
	
	/**
	 * Sets the raw field value map
	 *
   * @param rawFieldValues raw field value map
   * @see #getRawFieldValues()
	 */
	public BrowseHit setRawFieldValues(Map rawFieldValues) {
		_rawFieldValues = rawFieldValues;
    return this;
	}
	
	/**
	 * Gets the raw field values
	 * @return raw field value map
	 * @see #setRawFieldValues(Map)
	 */
	public Map getRawFieldValues() {
		return _rawFieldValues;
	}
	
	/**
	 * Sets the field value map
	 *
   * @param fieldValues field value map
   * @see #getFieldValues()
	 */
	public BrowseHit setFieldValues(Map fieldValues) {
		_fieldValues = fieldValues;
    return this;
	}
	
	/**
	 * Sets the score
	 *
   * @param score score
   * @see #getScore()
	 */
	public BrowseHit setScore(float score) {
		this.score = score;
    return this;
	}
	
	public BrowseHit setStoredFields(Document doc){
		_storedFields = doc;
    return this;
	}
	
	public Document getStoredFields(){
		return _storedFields;
	}
	
	public String toString(Map map)
	  {
	    StringBuilder buffer = new StringBuilder();
	    Set> set = map.entrySet();
	    Iterator> iterator = set.iterator();
	    while (iterator.hasNext()) {
	      Map.Entry e = iterator.next();
	      buffer.append(e.getKey());
	      buffer.append(":");
	      String[] vals = e.getValue();
	      buffer.append(vals == null ? null: Arrays.toString(vals));
	      if (iterator.hasNext()) buffer.append(", ");
	    }
	    return buffer.toString();
	  }
	
	@Override
	public String toString() {
		StringBuffer buffer=new StringBuffer();
		buffer.append("docid: ").append(docid);
		buffer.append("score: ").append(score).append('\n');
		buffer.append("field values: ").append(toString(_fieldValues)).append('\n');
		return buffer.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy