Skip to content

Commit

Permalink
Terms API: Allow to get terms for one or more field. Closes #21.
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Feb 16, 2010
1 parent 06cbc0a commit 5d78196
Show file tree
Hide file tree
Showing 56 changed files with 2,320 additions and 102 deletions.
2 changes: 2 additions & 0 deletions .idea/dictionaries/kimchy.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.elasticsearch.action.search.TransportSearchAction;
import org.elasticsearch.action.search.TransportSearchScrollAction;
import org.elasticsearch.action.search.type.*;
import org.elasticsearch.action.terms.TransportTermsAction;

/**
* @author kimchy (Shay Banon)
Expand Down Expand Up @@ -80,12 +81,13 @@ public class TransportActionModule extends AbstractModule {
bind(TransportIndexAction.class).asEagerSingleton();
bind(TransportGetAction.class).asEagerSingleton();
bind(TransportDeleteAction.class).asEagerSingleton();
bind(TransportCountAction.class).asEagerSingleton();
bind(TransportTermsAction.class).asEagerSingleton();

bind(TransportShardDeleteByQueryAction.class).asEagerSingleton();
bind(TransportIndexDeleteByQueryAction.class).asEagerSingleton();
bind(TransportDeleteByQueryAction.class).asEagerSingleton();

bind(TransportCountAction.class).asEagerSingleton();

bind(TransportSearchCache.class).asEagerSingleton();
bind(TransportSearchDfsQueryThenFetchAction.class).asEagerSingleton();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class TransportActions {

public static final String SEARCH_SCROLL = "indices/searchScroll";

public static final String TERMS = "indices/terms";

public static class Admin {

public static class Indices {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ public class IndexStatus implements Iterable<IndexShardStatus> {
public static class Docs {
public static final Docs UNKNOWN = new Docs();

int numDocs = -1;
int maxDoc = -1;
int deletedDocs = -1;
long numDocs = -1;
long maxDoc = -1;
long deletedDocs = -1;

public int numDocs() {
public long numDocs() {
return numDocs;
}

public int maxDoc() {
public long maxDoc() {
return maxDoc;
}

public int deletedDocs() {
public long deletedDocs() {
return deletedDocs;
}
}
Expand Down Expand Up @@ -83,6 +83,10 @@ public String index() {
return this.index;
}

/**
* A shard id to index shard status map (note, index shard status is the replication shard group that maps
* to the shard id).
*/
public Map<Integer, IndexShardStatus> shards() {
return this.indexShards;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ public SearchRequest size(int size) {
} else {
out.writeInt(queryBoost.size());
for (TObjectFloatIterator<String> it = queryBoost.iterator(); it.hasNext();) {
it.advance();
out.writeUTF(it.key());
out.writeFloat(it.value());
it.advance();
}
}
out.writeInt(types.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,19 @@ private void finishHim(boolean alreadyThreaded) {
if (request.listenerThreaded() && !alreadyThreaded) {
threadPool.execute(new Runnable() {
@Override public void run() {
listener.onResponse(newResponse(request, shardsResponses, clusterState));
try {
listener.onResponse(newResponse(request, shardsResponses, clusterState));
} catch (Exception e) {
listener.onFailure(e);
}
}
});
} else {
listener.onResponse(newResponse(request, shardsResponses, clusterState));
try {
listener.onResponse(newResponse(request, shardsResponses, clusterState));
} catch (Exception e) {
listener.onFailure(e);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.action.terms;

import com.google.common.collect.Iterators;
import org.elasticsearch.util.io.Streamable;
import org.elasticsearch.util.trove.ExtTObjectIntHasMap;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Iterator;

import static org.elasticsearch.action.terms.TermFreq.*;

/**
* @author kimchy (Shay Banon)
*/
public class FieldTermsFreq implements Streamable, Iterable<TermFreq> {

private String fieldName;

private TermFreq[] termsFreqs;

private transient ExtTObjectIntHasMap<String> termsFreqMap;

private FieldTermsFreq() {

}

public FieldTermsFreq(String fieldName, TermFreq[] termsFreqs) {
this.fieldName = fieldName;
this.termsFreqs = termsFreqs;
}

public String fieldName() {
return this.fieldName;
}

public TermFreq[] termsFreqs() {
return this.termsFreqs;
}

/**
* Returns the document frequency of a term, <tt>-1</tt> if the term does not exists.
*/
public int docFreq(String term) {
if (termsFreqMap == null) {
ExtTObjectIntHasMap<String> termsFreqMap = new ExtTObjectIntHasMap<String>().defaultReturnValue(-1);
for (TermFreq termFreq : termsFreqs) {
termsFreqMap.put(termFreq.term(), termFreq.docFreq());
}
this.termsFreqMap = termsFreqMap;
}
return termsFreqMap.get(term);
}

@Override public Iterator<TermFreq> iterator() {
return Iterators.forArray(termsFreqs);
}

public static FieldTermsFreq readFieldTermsFreq(DataInput in) throws IOException, ClassNotFoundException {
FieldTermsFreq fieldTermsFreq = new FieldTermsFreq();
fieldTermsFreq.readFrom(in);
return fieldTermsFreq;
}

@Override public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
fieldName = in.readUTF();
termsFreqs = new TermFreq[in.readInt()];
for (int i = 0; i < termsFreqs.length; i++) {
termsFreqs[i] = readTermFreq(in);
}
}

@Override public void writeTo(DataOutput out) throws IOException {
out.writeUTF(fieldName);
out.writeInt(termsFreqs.length);
for (TermFreq termFreq : termsFreqs) {
termFreq.writeTo(out);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.action.terms;

import org.elasticsearch.action.support.broadcast.BroadcastShardOperationRequest;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
* @author kimchy (Shay Banon)
*/
public class ShardTermsRequest extends BroadcastShardOperationRequest {

private String[] fields;

private String from;

private String to;

private boolean fromInclusive = true;

private boolean toInclusive = false;

private String prefix;

private String regexp;

private int size = 10;

private boolean convert = true;

private TermsRequest.SortType sortType;

private boolean exact = false;

ShardTermsRequest() {
}

public ShardTermsRequest(String index, int shardId, TermsRequest request) {
super(index, shardId);
this.fields = request.fields();
this.from = request.from();
this.to = request.to();
this.fromInclusive = request.fromInclusive();
this.toInclusive = request.toInclusive();
this.prefix = request.prefix();
this.regexp = request.regexp();
this.size = request.size();
this.convert = request.convert();
this.sortType = request.sortType();
this.exact = request.exact();
}

public String[] fields() {
return fields;
}

public String from() {
return from;
}

public String to() {
return to;
}

public boolean fromInclusive() {
return fromInclusive;
}

public boolean toInclusive() {
return toInclusive;
}

public String prefix() {
return prefix;
}

public String regexp() {
return regexp;
}

public int size() {
return size;
}

public boolean convert() {
return convert;
}

public TermsRequest.SortType sortType() {
return sortType;
}

public boolean exact() {
return this.exact;
}

@Override public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
super.readFrom(in);
fields = new String[in.readInt()];
for (int i = 0; i < fields.length; i++) {
fields[i] = in.readUTF();
}
if (in.readBoolean()) {
from = in.readUTF();
}
if (in.readBoolean()) {
to = in.readUTF();
}
fromInclusive = in.readBoolean();
toInclusive = in.readBoolean();
if (in.readBoolean()) {
prefix = in.readUTF();
}
if (in.readBoolean()) {
regexp = in.readUTF();
}
size = in.readInt();
convert = in.readBoolean();
sortType = TermsRequest.SortType.fromValue(in.readByte());
exact = in.readBoolean();
}

@Override public void writeTo(DataOutput out) throws IOException {
super.writeTo(out);
out.writeInt(fields.length);
for (String field : fields) {
out.writeUTF(field);
}
if (from == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeUTF(from);
}
if (to == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeUTF(to);
}
out.writeBoolean(fromInclusive);
out.writeBoolean(toInclusive);
if (prefix == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeUTF(prefix);
}
if (regexp == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeUTF(regexp);
}
out.writeInt(size);
out.writeBoolean(convert);
out.writeByte(sortType.value());
out.writeBoolean(exact);
}
}
Loading

0 comments on commit 5d78196

Please sign in to comment.