
com.jaeksoft.searchlib.facet.Facet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opensearchserver Show documentation
Show all versions of opensearchserver Show documentation
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.
/**
* 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.Iterator;
import java.util.Map;
import java.util.TreeMap;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.util.OpenBitSet;
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;
public class Facet implements Iterable,
External.Collecter {
protected FacetField facetField;
private Map facetMap;
protected transient FacetItem[] array = null;
public Facet() {
array = 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 FacetItem[] getArray() {
synchronized (this) {
if (array != null)
return array;
array = new FacetItem[facetMap.size()];
facetMap.values().toArray(array);
return array;
}
}
public Map getMap() {
synchronized (this) {
return facetMap;
}
}
private FacetItem get(int i) {
return (FacetItem) getArray()[i];
}
@Override
public Iterator iterator() {
return facetMap.values().iterator();
}
public int getTermCount() {
return getArray().length;
}
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;
OpenBitSet 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.fastGet(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