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\"\"}"));
}
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));
}
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"));
}
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;
}
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);
}
}
Aggregations