Search in sources :

Example 46 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class CustomMustacheFactoryTests method testDefaultEncoder.

public void testDefaultEncoder() {
    final ScriptEngineService engine = new MustacheScriptEngineService();
    final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, PLAIN_TEXT_MIME_TYPE);
    Mustache script = (Mustache) engine.compile(null, "{\"field\": \"{{value}}\"}", params);
    CompiledScript compiled = new CompiledScript(INLINE, null, MustacheScriptEngineService.NAME, script);
    ExecutableScript executable = engine.executable(compiled, singletonMap("value", "a \"value\""));
    BytesReference result = (BytesReference) executable.run();
    assertThat(result.utf8ToString(), equalTo("{\"field\": \"a \"value\"\"}"));
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) BytesReference(org.elasticsearch.common.bytes.BytesReference) ExecutableScript(org.elasticsearch.script.ExecutableScript) Mustache(com.github.mustachejava.Mustache) ScriptEngineService(org.elasticsearch.script.ScriptEngineService)

Example 47 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class PercolateQueryBuilder method doRewrite.

@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) throws IOException {
    if (document != null) {
        return this;
    }
    GetRequest getRequest = new GetRequest(indexedDocumentIndex, indexedDocumentType, indexedDocumentId);
    getRequest.preference("_local");
    getRequest.routing(indexedDocumentRouting);
    getRequest.preference(indexedDocumentPreference);
    if (indexedDocumentVersion != null) {
        getRequest.version(indexedDocumentVersion);
    }
    GetResponse getResponse = queryShardContext.getClient().get(getRequest).actionGet();
    if (getResponse.isExists() == false) {
        throw new ResourceNotFoundException("indexed document [{}/{}/{}] couldn't be found", indexedDocumentIndex, indexedDocumentType, indexedDocumentId);
    }
    if (getResponse.isSourceEmpty()) {
        throw new IllegalArgumentException("indexed document [" + indexedDocumentIndex + "/" + indexedDocumentType + "/" + indexedDocumentId + "] source disabled");
    }
    final BytesReference source = getResponse.getSourceAsBytesRef();
    return new PercolateQueryBuilder(field, documentType, source, XContentFactory.xContentType(source));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) GetRequest(org.elasticsearch.action.get.GetRequest) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 48 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class PercolatorQuerySearchIT method testPercolatorQueryWithHighlighting.

public void testPercolatorQueryWithHighlighting() throws Exception {
    StringBuilder fieldMapping = new StringBuilder("type=text").append(",store=").append(randomBoolean());
    if (randomBoolean()) {
        fieldMapping.append(",term_vector=with_positions_offsets");
    } else if (randomBoolean()) {
        fieldMapping.append(",index_options=offsets");
    }
    createIndex("test", client().admin().indices().prepareCreate("test").addMapping("type", "field1", fieldMapping).addMapping("queries", "query", "type=percolator"));
    client().prepareIndex("test", "queries", "1").setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "brown fox")).endObject()).execute().actionGet();
    client().prepareIndex("test", "queries", "2").setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "lazy dog")).endObject()).execute().actionGet();
    client().prepareIndex("test", "queries", "3").setSource(jsonBuilder().startObject().field("query", termQuery("field1", "jumps")).endObject()).execute().actionGet();
    client().prepareIndex("test", "queries", "4").setSource(jsonBuilder().startObject().field("query", termQuery("field1", "dog")).endObject()).execute().actionGet();
    client().prepareIndex("test", "queries", "5").setSource(jsonBuilder().startObject().field("query", termQuery("field1", "fox")).endObject()).execute().actionGet();
    client().admin().indices().prepareRefresh().get();
    BytesReference document = jsonBuilder().startObject().field("field1", "The quick brown fox jumps over the lazy dog").endObject().bytes();
    SearchResponse searchResponse = client().prepareSearch().setQuery(new PercolateQueryBuilder("query", "type", document, XContentType.JSON)).highlighter(new HighlightBuilder().field("field1")).addSort("_uid", SortOrder.ASC).get();
    assertHitCount(searchResponse, 5);
    assertThat(searchResponse.getHits().getAt(0).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick <em>brown</em> <em>fox</em> jumps over the lazy dog"));
    assertThat(searchResponse.getHits().getAt(1).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox jumps over the <em>lazy</em> <em>dog</em>"));
    assertThat(searchResponse.getHits().getAt(2).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox <em>jumps</em> over the lazy dog"));
    assertThat(searchResponse.getHits().getAt(3).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox jumps over the lazy <em>dog</em>"));
    assertThat(searchResponse.getHits().getAt(4).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown <em>fox</em> jumps over the lazy dog"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse)

Example 49 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class ByteBufStreamInput method readBytesReference.

@Override
public BytesReference readBytesReference(int length) throws IOException {
    BytesReference ref = Netty4Utils.toBytesReference(buffer.slice(buffer.readerIndex(), length));
    buffer.skipBytes(length);
    return ref;
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference)

Example 50 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class Netty4MessageChannelHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    Transports.assertTransportThread();
    if (!(msg instanceof ByteBuf)) {
        ctx.fireChannelRead(msg);
        return;
    }
    final ByteBuf buffer = (ByteBuf) msg;
    final int remainingMessageSize = buffer.getInt(buffer.readerIndex() - TcpHeader.MESSAGE_LENGTH_SIZE);
    final int expectedReaderIndex = buffer.readerIndex() + remainingMessageSize;
    try {
        InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        // netty always copies a buffer, either in NioWorker in its read handler, where it copies to a fresh
        // buffer, or in the cumulative buffer, which is cleaned each time so it could be bigger than the actual size
        BytesReference reference = Netty4Utils.toBytesReference(buffer, remainingMessageSize);
        transport.messageReceived(reference, ctx.channel(), profileName, remoteAddress, remainingMessageSize);
    } finally {
        // Set the expected position of the buffer, no matter what happened
        buffer.readerIndex(expectedReaderIndex);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

BytesReference (org.elasticsearch.common.bytes.BytesReference)318 Matchers.containsString (org.hamcrest.Matchers.containsString)72 XContentParser (org.elasticsearch.common.xcontent.XContentParser)63 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)61 IOException (java.io.IOException)58 XContentType (org.elasticsearch.common.xcontent.XContentType)50 BytesArray (org.elasticsearch.common.bytes.BytesArray)47 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)37 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)30 Map (java.util.Map)26 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)26 Test (org.junit.Test)25 List (java.util.List)24 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)24 Version (org.elasticsearch.Version)22 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)20 ReleasableBytesReference (org.elasticsearch.common.bytes.ReleasableBytesReference)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)18 BytesRef (org.apache.lucene.util.BytesRef)18