-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Terms API: Allow to get terms for one or more field. Closes #21.
- Loading branch information
Showing
56 changed files
with
2,320 additions
and
102 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
modules/elasticsearch/src/main/java/org/elasticsearch/action/terms/FieldTermsFreq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
180 changes: 180 additions & 0 deletions
180
modules/elasticsearch/src/main/java/org/elasticsearch/action/terms/ShardTermsRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.