Search in sources :

Example 16 with BytesArray

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;
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) IndexReader(org.apache.lucene.index.IndexReader) SuggestWord(org.apache.lucene.search.spell.SuggestWord) Text(org.elasticsearch.common.text.Text) DirectSpellChecker(org.apache.lucene.search.spell.DirectSpellChecker)

Example 17 with BytesArray

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);
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) RestRequest(org.elasticsearch.rest.RestRequest) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 18 with BytesArray

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"));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray)

Example 19 with BytesArray

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());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) GetField(org.elasticsearch.index.get.GetField) GetResultTests.mutateGetResult(org.elasticsearch.index.get.GetResultTests.mutateGetResult) GetResult(org.elasticsearch.index.get.GetResult) GetResultTests.randomGetResult(org.elasticsearch.index.get.GetResultTests.randomGetResult) GetResultTests.copyGetResult(org.elasticsearch.index.get.GetResultTests.copyGetResult)

Example 20 with BytesArray

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());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

BytesArray (org.elasticsearch.common.bytes.BytesArray)203 BytesReference (org.elasticsearch.common.bytes.BytesReference)36 Matchers.containsString (org.hamcrest.Matchers.containsString)31 IOException (java.io.IOException)29 StreamInput (org.elasticsearch.common.io.stream.StreamInput)24 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)24 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)21 HashMap (java.util.HashMap)17 BytesRef (org.apache.lucene.util.BytesRef)17 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)14 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)14 FakeRestRequest (org.elasticsearch.test.rest.FakeRestRequest)13 ArrayList (java.util.ArrayList)12 TopDocs (org.apache.lucene.search.TopDocs)12 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 Document (org.elasticsearch.index.mapper.ParseContext.Document)12 Index (org.elasticsearch.index.Index)11 Map (java.util.Map)10 IndexableField (org.apache.lucene.index.IndexableField)10 IndexService (org.elasticsearch.index.IndexService)10