use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class TermSuggester method innerExecute.
@Override
public TermSuggestion innerExecute(String name, TermSuggestionContext suggestion, IndexSearcher searcher, CharsRefBuilder spare) throws IOException {
DirectSpellChecker directSpellChecker = suggestion.getDirectSpellCheckerSettings().createDirectSpellChecker();
final IndexReader indexReader = searcher.getIndexReader();
TermSuggestion response = new TermSuggestion(name, suggestion.getSize(), suggestion.getDirectSpellCheckerSettings().sort());
List<Token> tokens = queryTerms(suggestion, spare);
for (Token token : tokens) {
// TODO: Extend DirectSpellChecker in 4.1, to get the raw suggested words as BytesRef
SuggestWord[] suggestedWords = directSpellChecker.suggestSimilar(token.term, suggestion.getShardSize(), indexReader, suggestion.getDirectSpellCheckerSettings().suggestMode());
Text key = new Text(new BytesArray(token.term.bytes()));
TermSuggestion.Entry resultEntry = new TermSuggestion.Entry(key, token.startOffset, token.endOffset - token.startOffset);
for (SuggestWord suggestWord : suggestedWords) {
Text word = new Text(suggestWord.string);
resultEntry.addOption(new TermSuggestion.Entry.Option(word, suggestWord.freq, suggestWord.score));
}
response.addTerm(resultEntry);
}
return response;
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class MultiSearchRequestTests method parseMultiSearchRequest.
private MultiSearchRequest parseMultiSearchRequest(String sample) throws IOException {
byte[] data = StreamsUtils.copyToBytesFromClasspath(sample);
RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(data), XContentType.JSON).build();
return RestMultiSearchAction.parseRequest(restRequest, true);
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class FieldStatsRequestTests method testFieldsParsing.
public void testFieldsParsing() throws Exception {
BytesArray data = new BytesArray(StreamsUtils.copyToBytesFromClasspath("/org/elasticsearch/action/fieldstats/" + "fieldstats-index-constraints-request.json"));
FieldStatsRequest request = new FieldStatsRequest();
request.source(createParser(JsonXContent.jsonXContent, data));
assertThat(request.getFields().length, equalTo(5));
assertThat(request.getFields()[0], equalTo("field1"));
assertThat(request.getFields()[1], equalTo("field2"));
assertThat(request.getFields()[2], equalTo("field3"));
assertThat(request.getFields()[3], equalTo("field4"));
assertThat(request.getFields()[4], equalTo("field5"));
assertThat(request.getIndexConstraints().length, equalTo(8));
assertThat(request.getIndexConstraints()[0].getField(), equalTo("field2"));
assertThat(request.getIndexConstraints()[0].getValue(), equalTo("9"));
assertThat(request.getIndexConstraints()[0].getProperty(), equalTo(MAX));
assertThat(request.getIndexConstraints()[0].getComparison(), equalTo(GTE));
assertThat(request.getIndexConstraints()[1].getField(), equalTo("field3"));
assertThat(request.getIndexConstraints()[1].getValue(), equalTo("5"));
assertThat(request.getIndexConstraints()[1].getProperty(), equalTo(MIN));
assertThat(request.getIndexConstraints()[1].getComparison(), equalTo(GT));
assertThat(request.getIndexConstraints()[2].getField(), equalTo("field4"));
assertThat(request.getIndexConstraints()[2].getValue(), equalTo("a"));
assertThat(request.getIndexConstraints()[2].getProperty(), equalTo(MIN));
assertThat(request.getIndexConstraints()[2].getComparison(), equalTo(GTE));
assertThat(request.getIndexConstraints()[3].getField(), equalTo("field4"));
assertThat(request.getIndexConstraints()[3].getValue(), equalTo("g"));
assertThat(request.getIndexConstraints()[3].getProperty(), equalTo(MAX));
assertThat(request.getIndexConstraints()[3].getComparison(), equalTo(LTE));
assertThat(request.getIndexConstraints()[4].getField(), equalTo("field5"));
assertThat(request.getIndexConstraints()[4].getValue(), equalTo("2"));
assertThat(request.getIndexConstraints()[4].getProperty(), equalTo(MIN));
assertThat(request.getIndexConstraints()[4].getComparison(), equalTo(GT));
assertThat(request.getIndexConstraints()[5].getField(), equalTo("field5"));
assertThat(request.getIndexConstraints()[5].getValue(), equalTo("9"));
assertThat(request.getIndexConstraints()[5].getProperty(), equalTo(MAX));
assertThat(request.getIndexConstraints()[5].getComparison(), equalTo(LT));
assertThat(request.getIndexConstraints()[6].getField(), equalTo("field1"));
assertThat(request.getIndexConstraints()[6].getValue(), equalTo("2014-01-01"));
assertThat(request.getIndexConstraints()[6].getProperty(), equalTo(MIN));
assertThat(request.getIndexConstraints()[6].getComparison(), equalTo(GTE));
assertThat(request.getIndexConstraints()[6].getOptionalFormat(), equalTo("date_optional_time"));
assertThat(request.getIndexConstraints()[7].getField(), equalTo("field1"));
assertThat(request.getIndexConstraints()[7].getValue(), equalTo("2015-01-01"));
assertThat(request.getIndexConstraints()[7].getProperty(), equalTo(MAX));
assertThat(request.getIndexConstraints()[7].getComparison(), equalTo(LT));
assertThat(request.getIndexConstraints()[7].getOptionalFormat(), equalTo("date_optional_time"));
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class GetResponseTests method testToString.
public void testToString() {
GetResponse getResponse = new GetResponse(new GetResult("index", "type", "id", 1, true, new BytesArray("{ \"field1\" : " + "\"value1\", \"field2\":\"value2\"}"), Collections.singletonMap("field1", new GetField("field1", Collections.singletonList("value1")))));
assertEquals("{\"_index\":\"index\",\"_type\":\"type\",\"_id\":\"id\",\"_version\":1,\"found\":true,\"_source\":{ \"field1\" " + ": \"value1\", \"field2\":\"value2\"},\"fields\":{\"field1\":[\"value1\"]}}", getResponse.toString());
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class IndexRequestTests method testIndexRequestXContentSerialization.
public void testIndexRequestXContentSerialization() throws IOException {
IndexRequest indexRequest = new IndexRequest("foo", "bar", "1");
indexRequest.source("{}", XContentType.JSON);
assertEquals(XContentType.JSON, indexRequest.getContentType());
BytesStreamOutput out = new BytesStreamOutput();
indexRequest.writeTo(out);
StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes);
IndexRequest serialized = new IndexRequest();
serialized.readFrom(in);
assertEquals(XContentType.JSON, serialized.getContentType());
assertEquals(new BytesArray("{}"), serialized.source());
}
Aggregations