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

com.jaeksoft.searchlib.facet.Facet Maven / Gradle / Ivy

Go to download

OpenSearchServer is a powerful, enterprise-class, search engine program. Using the web user interface, the crawlers (web, file, database, ...) and the REST/RESTFul API you will be able to integrate quickly and easily advanced full-text search capabilities in your application. OpenSearchServer runs on Windows and Linux/Unix/BSD.

There is a newer version: 1.5.14
Show newest version
/**   
 * License Agreement for OpenSearchServer
 *
 * Copyright (C) 2008-2014 Emmanuel Keller / Jaeksoft
 * 
 * http://www.open-search-server.com
 * 
 * This file is part of OpenSearchServer.
 *
 * OpenSearchServer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * OpenSearchServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenSearchServer. 
 *  If not, see .
 **/

package com.jaeksoft.searchlib.facet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;

import com.jaeksoft.searchlib.SearchLibException;
import com.jaeksoft.searchlib.index.FieldCacheIndex;
import com.jaeksoft.searchlib.index.ReaderAbstract;
import com.jaeksoft.searchlib.result.collector.DocIdInterface;
import com.jaeksoft.searchlib.util.External;
import com.jaeksoft.searchlib.util.Timer;
import com.jaeksoft.searchlib.util.bitset.BitSetInterface;

public class Facet implements Iterable,
		External.Collecter {

	protected FacetField facetField;
	private Map facetMap;
	protected transient List list = null;

	public Facet() {
		list = null;
		facetMap = new TreeMap();
	}

	public Facet(FacetField facetField) {
		this();
		this.facetField = facetField;
	}

	private Facet(FacetField facetField, String[] terms, int[] counts) {
		this(facetField);
		int i = 0;
		int minCount = facetField.getMinCount();
		for (int count : counts) {
			String term = terms[i];
			if (term != null && count >= minCount) {
				FacetItem facetItem = new FacetItem(term, count);
				facetMap.put(term, facetItem);
			}
			i++;
		}
	}

	public FacetField getFacetField() {
		return this.facetField;
	}

	protected void sum(Facet facet) {
		if (facet == null)
			return;
		for (FacetItem facetItem : facet) {
			if (facetItem.term == null)
				continue;
			FacetItem currentFacetItem = facetMap.get(facetItem.term);
			if (currentFacetItem != null)
				currentFacetItem.count += facetItem.count;
			else
				facetMap.put(facetItem.term, facetItem);
		}
	}

	public List getList() {
		synchronized (this) {
			if (list != null)
				return list;
			list = new ArrayList(facetMap.values());
			return list;
		}
	}

	public Map getMap() {
		synchronized (this) {
			return facetMap;
		}
	}

	private FacetItem get(int i) {
		return getList().get(i);
	}

	@Override
	public Iterator iterator() {
		return facetMap.values().iterator();
	}

	public int getTermCount() {
		return getList().size();
	}

	public String getTerm(int i) {
		return get(i).term;
	}

	public int getCount(int i) {
		return get(i).count;
	}

	final static protected Facet facetMultivalued(ReaderAbstract reader,
			DocIdInterface docIdInterface, FacetField facetField, Timer timer)
			throws IOException, SearchLibException {
		String fieldName = facetField.getName();
		FieldCacheIndex stringIndex = reader.getStringIndex(fieldName);
		int[] countIndex = computeMultivalued(reader, fieldName, stringIndex,
				docIdInterface);
		return new Facet(facetField, stringIndex.lookup, countIndex);
	}

	final static protected Facet facetSingleValue(ReaderAbstract reader,
			DocIdInterface collector, FacetField facetField, Timer timer)
			throws IOException {
		String fieldName = facetField.getName();
		FieldCacheIndex stringIndex = reader.getStringIndex(fieldName);
		int[] countIndex = computeSinglevalued(stringIndex, collector);
		return new Facet(facetField, stringIndex.lookup, countIndex);
	}

	@Override
	public void addObject(FacetItem facetItem) {
		facetMap.put(facetItem.term, facetItem);
	}

	final private static int[] computeMultivalued(ReaderAbstract reader,
			String fieldName, FieldCacheIndex stringIndex,
			DocIdInterface docIdInterface) throws IOException,
			SearchLibException {
		int[] countIndex = new int[stringIndex.lookup.length];
		int i = 0;
		if (docIdInterface.getSize() == 0)
			return countIndex;
		BitSetInterface bitset = docIdInterface.getBitSet();
		for (String term : stringIndex.lookup) {
			if (term != null) {
				Term t = new Term(fieldName, term);
				TermDocs termDocs = reader.getTermDocs(t);
				while (termDocs.next())
					if (termDocs.freq() > 0)
						if (bitset.get(termDocs.doc()))
							countIndex[i]++;
				termDocs.close();
			}
			i++;
		}
		return countIndex;
	}

	final private static int[] computeSinglevalued(FieldCacheIndex stringIndex,
			DocIdInterface collector) throws IOException {
		int[] countArray = new int[stringIndex.lookup.length];
		int[] order = stringIndex.order;
		int i = collector.getSize();
		for (int id : collector.getIds()) {
			if (i == 0)
				break;
			countArray[order[id]]++;
			i--;
		}
		return countArray;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy