Skip to content

Commit

Permalink
Terms API: Support numbers/dates, closes #78.
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Mar 22, 2010
1 parent bc03d89 commit 93e0253
Show file tree
Hide file tree
Showing 19 changed files with 650 additions and 339 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class FieldTermsFreq implements Streamable, Iterable<TermFreq> {

private TermFreq[] termsFreqs;

private transient ExtTObjectIntHasMap<String> termsFreqMap;
private transient ExtTObjectIntHasMap<Object> termsFreqMap;

private FieldTermsFreq() {

Expand Down Expand Up @@ -69,15 +69,16 @@ public TermFreq[] termsFreqs() {
/**
* Returns the document frequency of a term, <tt>-1</tt> if the term does not exists.
*/
public int docFreq(String term) {
public int docFreq(Object term) {
// we use "toString" on the term so we get hits when we the termValue is Long, and we lookup with int
if (termsFreqMap == null) {
ExtTObjectIntHasMap<String> termsFreqMap = new ExtTObjectIntHasMap<String>().defaultReturnValue(-1);
ExtTObjectIntHasMap<Object> termsFreqMap = new ExtTObjectIntHasMap<Object>().defaultReturnValue(-1);
for (TermFreq termFreq : termsFreqs) {
termsFreqMap.put(termFreq.term(), termFreq.docFreq());
termsFreqMap.put(termFreq.term().toString(), termFreq.docFreq());
}
this.termsFreqMap = termsFreqMap;
}
return termsFreqMap.get(term);
return termsFreqMap.get(term.toString());
}

@Override public Iterator<TermFreq> iterator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class ShardTermsRequest extends BroadcastShardOperationRequest {

private int size = 10;

private boolean convert = true;

private TermsRequest.SortType sortType;

private boolean exact = false;
Expand All @@ -65,7 +63,6 @@ public ShardTermsRequest(String index, int shardId, TermsRequest request) {
this.prefix = request.prefix();
this.regexp = request.regexp();
this.size = request.size();
this.convert = request.convert();
this.sortType = request.sortType();
this.exact = request.exact();
}
Expand Down Expand Up @@ -102,10 +99,6 @@ public int size() {
return size;
}

public boolean convert() {
return convert;
}

public TermsRequest.SortType sortType() {
return sortType;
}
Expand Down Expand Up @@ -135,7 +128,6 @@ public boolean exact() {
regexp = in.readUTF();
}
size = in.readVInt();
convert = in.readBoolean();
sortType = TermsRequest.SortType.fromValue(in.readByte());
exact = in.readBoolean();
}
Expand Down Expand Up @@ -173,7 +165,6 @@ public boolean exact() {
out.writeUTF(regexp);
}
out.writeVInt(size);
out.writeBoolean(convert);
out.writeByte(sortType.value());
out.writeBoolean(exact);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.elasticsearch.util.gnu.trove.TObjectIntIterator;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.lucene.Lucene;

import java.io.IOException;
import java.util.HashMap;
Expand All @@ -34,7 +35,7 @@
*/
class ShardTermsResponse extends BroadcastShardOperationResponse {

private Map<String, TObjectIntHashMap<String>> fieldsTermsFreqs = new HashMap<String, TObjectIntHashMap<String>>();
private Map<String, TObjectIntHashMap<Object>> fieldsTermsFreqs = new HashMap<String, TObjectIntHashMap<Object>>();

private int numDocs;

Expand Down Expand Up @@ -64,11 +65,11 @@ int numDeletedDocs() {
return this.numDeletedDocs;
}

void put(String fieldName, TObjectIntHashMap<String> termsFreqs) {
void put(String fieldName, TObjectIntHashMap<Object> termsFreqs) {
fieldsTermsFreqs.put(fieldName, termsFreqs);
}

Map<String, TObjectIntHashMap<String>> fieldsTermsFreqs() {
Map<String, TObjectIntHashMap<Object>> fieldsTermsFreqs() {
return fieldsTermsFreqs;
}

Expand All @@ -81,10 +82,10 @@ Map<String, TObjectIntHashMap<String>> fieldsTermsFreqs() {
for (int i = 0; i < size; i++) {
String fieldName = in.readUTF();

TObjectIntHashMap<String> termsFreq = new TObjectIntHashMap<String>();
TObjectIntHashMap<Object> termsFreq = new TObjectIntHashMap<Object>();
int size1 = in.readVInt();
for (int j = 0; j < size1; j++) {
termsFreq.put(in.readUTF(), in.readVInt());
termsFreq.put(Lucene.readFieldValue(in), in.readVInt());
}

fieldsTermsFreqs.put(fieldName, termsFreq);
Expand All @@ -97,12 +98,12 @@ Map<String, TObjectIntHashMap<String>> fieldsTermsFreqs() {
out.writeVInt(maxDoc);
out.writeVInt(numDeletedDocs);
out.writeVInt(fieldsTermsFreqs.size());
for (Map.Entry<String, TObjectIntHashMap<String>> entry : fieldsTermsFreqs.entrySet()) {
for (Map.Entry<String, TObjectIntHashMap<Object>> entry : fieldsTermsFreqs.entrySet()) {
out.writeUTF(entry.getKey());
out.writeVInt(entry.getValue().size());
for (TObjectIntIterator<String> it = entry.getValue().iterator(); it.hasNext();) {
for (TObjectIntIterator<Object> it = entry.getValue().iterator(); it.hasNext();) {
it.advance();
out.writeUTF(it.key());
Lucene.writeFieldValue(out, it.key());
out.writeVInt(it.value());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable;
import org.elasticsearch.util.lucene.Lucene;

import java.io.IOException;
import java.util.Comparator;
Expand All @@ -40,7 +41,7 @@ public class TermFreq implements Streamable {
@Override public int compare(TermFreq o1, TermFreq o2) {
int i = o2.docFreq() - o1.docFreq();
if (i == 0) {
i = o1.term().compareTo(o2.term());
i = ((Comparable) o1.term()).compareTo(o2.term());
}
return i;
}
Expand All @@ -51,7 +52,7 @@ public class TermFreq implements Streamable {
*/
private static final Comparator<TermFreq> termComparator = new Comparator<TermFreq>() {
@Override public int compare(TermFreq o1, TermFreq o2) {
int i = o1.term().compareTo(o2.term());
int i = ((Comparable) o1.term()).compareTo(o2.term());
if (i == 0) {
i = o1.docFreq() - o2.docFreq();
}
Expand All @@ -73,7 +74,7 @@ public static Comparator<TermFreq> termComparator() {
return termComparator;
}

private String term;
private Object term;

private int docFreq;

Expand All @@ -87,18 +88,22 @@ private TermFreq() {
* @param term The term
* @param docFreq The document frequency
*/
TermFreq(String term, int docFreq) {
TermFreq(Object term, int docFreq) {
this.term = term;
this.docFreq = docFreq;
}

/**
* The term.
*/
public String term() {
public Object term() {
return term;
}

public String termAsString() {
return term.toString();
}

/**
* The document frequency of the term (in how many documents this term exists).
*/
Expand All @@ -113,12 +118,12 @@ public static TermFreq readTermFreq(StreamInput in) throws IOException {
}

@Override public void readFrom(StreamInput in) throws IOException {
term = in.readUTF();
term = Lucene.readFieldValue(in);
docFreq = in.readVInt();
}

@Override public void writeTo(StreamOutput out) throws IOException {
out.writeUTF(term);
Lucene.writeFieldValue(out, term);
out.writeVInt(docFreq);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,21 @@ public String from() {
* The lower bound (lex) term from which the iteration will start. Defaults to start from the
* first.
*/
public TermsRequest from(String from) {
this.from = from;
public TermsRequest from(Object from) {
this.from = from.toString();
return this;
}

/**
* Should the first from (if set using {@link #from(String)} be inclusive or not. Defaults
* Should the first from (if set using {@link #from(Object)} be inclusive or not. Defaults
* to <tt>false</tt> (not inclusive / exclusive).
*/
public boolean fromInclusive() {
return fromInclusive;
}

/**
* Should the first from (if set using {@link #from(String)} be inclusive or not. Defaults
* Should the first from (if set using {@link #from(Object)} be inclusive or not. Defaults
* to <tt>false</tt> (not inclusive / exclusive).
*/
public TermsRequest fromInclusive(boolean fromInclusive) {
Expand All @@ -211,21 +211,21 @@ public String to() {
/**
* The upper bound (lex) term to which the iteration will end. Defaults to unbound (<tt>null</tt>).
*/
public TermsRequest to(String to) {
this.to = to;
public TermsRequest to(Object to) {
this.to = to.toString();
return this;
}

/**
* Should the last to (if set using {@link #to(String)} be inclusive or not. Defaults to
* Should the last to (if set using {@link #to(Object)} be inclusive or not. Defaults to
* <tt>true</tt>.
*/
public boolean toInclusive() {
return toInclusive;
}

/**
* Should the last to (if set using {@link #to(String)} be inclusive or not. Defaults to
* Should the last to (if set using {@link #to(Object)} be inclusive or not. Defaults to
* <tt>true</tt>.
*/
public TermsRequest toInclusive(boolean toInclusive) {
Expand Down Expand Up @@ -309,23 +309,6 @@ public TermsRequest size(int size) {
return this;
}

/**
* Should an attempt be made to convert the {@link #to(String)} and {@link #from(String)}.
* Defaults to <tt>true</tt>.
*/
public boolean convert() {
return convert;
}

/**
* Should an attempt be made to convert the {@link #to(String)} and {@link #from(String)}.
* Defaults to <tt>true</tt>.
*/
public TermsRequest convert(boolean convert) {
this.convert = convert;
return this;
}

/**
* The type of sorting for term / doc freq. Can either sort on term (lex) or doc frequency. Defaults to
* {@link TermsRequest.SortType#TERM}.
Expand Down
Loading

0 comments on commit 93e0253

Please sign in to comment.