Search in sources :

Example 11 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class TCPTransportTests method testCompressRequest.

public void testCompressRequest() throws IOException {
    final boolean compressed = randomBoolean();
    final AtomicBoolean called = new AtomicBoolean(false);
    Req request = new Req(randomRealisticUnicodeOfLengthBetween(10, 100));
    ThreadPool threadPool = new TestThreadPool(TCPTransportTests.class.getName());
    try {
        TcpTransport transport = new TcpTransport("test", Settings.builder().put("transport.tcp.compress", compressed).build(), threadPool, new BigArrays(Settings.EMPTY, null), null, null, null) {

            @Override
            protected InetSocketAddress getLocalAddress(Object o) {
                return null;
            }

            @Override
            protected Object bind(String name, InetSocketAddress address) throws IOException {
                return null;
            }

            @Override
            protected void closeChannels(List channel) throws IOException {
            }

            @Override
            protected void sendMessage(Object o, BytesReference reference, Runnable sendListener) throws IOException {
                StreamInput streamIn = reference.streamInput();
                streamIn.skip(TcpHeader.MARKER_BYTES_SIZE);
                int len = streamIn.readInt();
                long requestId = streamIn.readLong();
                assertEquals(42, requestId);
                byte status = streamIn.readByte();
                Version version = Version.fromId(streamIn.readInt());
                assertEquals(Version.CURRENT, version);
                assertEquals(compressed, TransportStatus.isCompress(status));
                called.compareAndSet(false, true);
                if (compressed) {
                    final int bytesConsumed = TcpHeader.HEADER_SIZE;
                    streamIn = CompressorFactory.compressor(reference.slice(bytesConsumed, reference.length() - bytesConsumed)).streamInput(streamIn);
                }
                threadPool.getThreadContext().readHeaders(streamIn);
                assertEquals("foobar", streamIn.readString());
                Req readReq = new Req("");
                readReq.readFrom(streamIn);
                assertEquals(request.value, readReq.value);
            }

            @Override
            protected NodeChannels connectToChannels(DiscoveryNode node, ConnectionProfile profile) throws IOException {
                return new NodeChannels(node, new Object[profile.getNumConnections()], profile);
            }

            @Override
            protected boolean isOpen(Object o) {
                return false;
            }

            @Override
            public long serverOpen() {
                return 0;
            }

            @Override
            public NodeChannels getConnection(DiscoveryNode node) {
                return new NodeChannels(node, new Object[MockTcpTransport.LIGHT_PROFILE.getNumConnections()], MockTcpTransport.LIGHT_PROFILE);
            }
        };
        DiscoveryNode node = new DiscoveryNode("foo", buildNewFakeTransportAddress(), Version.CURRENT);
        Transport.Connection connection = transport.getConnection(node);
        connection.sendRequest(42, "foobar", request, TransportRequestOptions.EMPTY);
        assertTrue(called.get());
    } finally {
        ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) InetSocketAddress(java.net.InetSocketAddress) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BigArrays(org.elasticsearch.common.util.BigArrays) Version(org.elasticsearch.Version) StreamInput(org.elasticsearch.common.io.stream.StreamInput) List(java.util.List)

Example 12 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class PercolateQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    // Call nowInMillis() so that this query becomes un-cacheable since we
    // can't be sure that it doesn't use now or scripts
    context.nowInMillis();
    if (indexedDocumentIndex != null || indexedDocumentType != null || indexedDocumentId != null) {
        throw new IllegalStateException("query builder must be rewritten first");
    }
    if (document == null) {
        throw new IllegalStateException("no document to percolate");
    }
    MapperService mapperService = context.getMapperService();
    DocumentMapperForType docMapperForType = mapperService.documentMapperWithAutoCreate(documentType);
    DocumentMapper docMapper = docMapperForType.getDocumentMapper();
    ParsedDocument doc = docMapper.parse(source(context.index().getName(), documentType, "_temp_id", document, documentXContentType));
    FieldNameAnalyzer fieldNameAnalyzer = (FieldNameAnalyzer) docMapper.mappers().indexAnalyzer();
    // Need to this custom impl because FieldNameAnalyzer is strict and the percolator sometimes isn't when
    // 'index.percolator.map_unmapped_fields_as_string' is enabled:
    Analyzer analyzer = new DelegatingAnalyzerWrapper(Analyzer.PER_FIELD_REUSE_STRATEGY) {

        @Override
        protected Analyzer getWrappedAnalyzer(String fieldName) {
            Analyzer analyzer = fieldNameAnalyzer.analyzers().get(fieldName);
            if (analyzer != null) {
                return analyzer;
            } else {
                return context.getIndexAnalyzers().getDefaultIndexAnalyzer();
            }
        }
    };
    final IndexSearcher docSearcher;
    if (doc.docs().size() > 1) {
        assert docMapper.hasNestedObjects();
        docSearcher = createMultiDocumentSearcher(analyzer, doc);
    } else {
        MemoryIndex memoryIndex = MemoryIndex.fromDocument(doc.rootDoc(), analyzer, true, false);
        docSearcher = memoryIndex.createSearcher();
        docSearcher.setQueryCache(null);
    }
    Version indexVersionCreated = context.getIndexSettings().getIndexVersionCreated();
    boolean mapUnmappedFieldsAsString = context.getIndexSettings().getValue(PercolatorFieldMapper.INDEX_MAP_UNMAPPED_FIELDS_AS_STRING_SETTING);
    // We have to make a copy of the QueryShardContext here so we can have a unfrozen version for parsing the legacy
    // percolator queries
    QueryShardContext percolateShardContext = new QueryShardContext(context);
    MappedFieldType fieldType = context.fieldMapper(field);
    if (fieldType == null) {
        throw new QueryShardException(context, "field [" + field + "] does not exist");
    }
    if (!(fieldType instanceof PercolatorFieldMapper.FieldType)) {
        throw new QueryShardException(context, "expected field [" + field + "] to be of type [percolator], but is of type [" + fieldType.typeName() + "]");
    }
    PercolatorFieldMapper.FieldType pft = (PercolatorFieldMapper.FieldType) fieldType;
    PercolateQuery.QueryStore queryStore = createStore(pft, percolateShardContext, mapUnmappedFieldsAsString);
    return pft.percolateQuery(documentType, queryStore, document, docSearcher);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) FieldNameAnalyzer(org.elasticsearch.index.analysis.FieldNameAnalyzer) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) Analyzer(org.apache.lucene.analysis.Analyzer) FieldNameAnalyzer(org.elasticsearch.index.analysis.FieldNameAnalyzer) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DelegatingAnalyzerWrapper(org.apache.lucene.analysis.DelegatingAnalyzerWrapper) Version(org.elasticsearch.Version) DocumentMapperForType(org.elasticsearch.index.mapper.DocumentMapperForType) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) QueryShardException(org.elasticsearch.index.query.QueryShardException) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 13 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class PercolatorFieldMapperTests method testEmptyName.

public void testEmptyName() throws Exception {
    // after 5.x
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("").field("type", "percolator").endObject().endObject().endObject().endObject().string();
    DocumentMapperParser parser = mapperService.documentMapperParser();
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.parse("type1", new CompressedXContent(mapping)));
    assertThat(e.getMessage(), containsString("name cannot be empty string"));
    // before 5.x
    Version oldVersion = VersionUtils.randomVersionBetween(getRandom(), Version.V_2_0_0, Version.V_2_3_5);
    Settings oldIndexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, oldVersion).build();
    DocumentMapperParser parser2x = createIndex("test_old", oldIndexSettings).mapperService().documentMapperParser();
    DocumentMapper defaultMapper = parser2x.parse("type1", new CompressedXContent(mapping));
    assertEquals(mapping, defaultMapper.mappingSource().string());
}
Also used : Version(org.elasticsearch.Version) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) DocumentMapperParser(org.elasticsearch.index.mapper.DocumentMapperParser) Settings(org.elasticsearch.common.settings.Settings)

Example 14 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class RemoteScrollableHitSourceTests method sourceWithMockedClient.

private RemoteScrollableHitSource sourceWithMockedClient(boolean mockRemoteVersion, CloseableHttpAsyncClient httpClient) throws Exception {
    HttpAsyncClientBuilder clientBuilder = mock(HttpAsyncClientBuilder.class);
    when(clientBuilder.build()).thenReturn(httpClient);
    RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).setHttpClientConfigCallback(httpClientBuilder -> clientBuilder).build();
    TestRemoteScrollableHitSource hitSource = new TestRemoteScrollableHitSource(restClient) {

        @Override
        void lookupRemoteVersion(Consumer<Version> onVersion) {
            if (mockRemoteVersion) {
                onVersion.accept(Version.CURRENT);
            } else {
                super.lookupRemoteVersion(onVersion);
            }
        }
    };
    if (mockRemoteVersion) {
        hitSource.remoteVersion = Version.CURRENT;
    }
    return hitSource;
}
Also used : Response(org.elasticsearch.action.bulk.byscroll.ScrollableHitSource.Response) ByteSizeUnit(org.elasticsearch.common.unit.ByteSizeUnit) ScheduledFuture(java.util.concurrent.ScheduledFuture) URL(java.net.URL) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) StatusLine(org.apache.http.StatusLine) HeapBufferedAsyncResponseConsumer(org.elasticsearch.client.HeapBufferedAsyncResponseConsumer) Future(java.util.concurrent.Future) After(org.junit.After) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) Streams(org.elasticsearch.common.io.Streams) ThreadPool(org.elasticsearch.threadpool.ThreadPool) HttpAsyncRequestProducer(org.apache.http.nio.protocol.HttpAsyncRequestProducer) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) StandardCharsets(java.nio.charset.StandardCharsets) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Matchers.any(org.mockito.Matchers.any) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) Version(org.elasticsearch.Version) RestStatus(org.elasticsearch.rest.RestStatus) TimeValue.timeValueMinutes(org.elasticsearch.common.unit.TimeValue.timeValueMinutes) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) RestClient(org.elasticsearch.client.RestClient) ContentTooLongException(org.apache.http.ContentTooLongException) ParsingException(org.elasticsearch.common.ParsingException) BasicStatusLine(org.apache.http.message.BasicStatusLine) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SearchRequest(org.elasticsearch.action.search.SearchRequest) BackoffPolicy(org.elasticsearch.action.bulk.BackoffPolicy) BytesArray(org.elasticsearch.common.bytes.BytesArray) HttpAsyncResponseConsumer(org.apache.http.nio.protocol.HttpAsyncResponseConsumer) Answer(org.mockito.stubbing.Answer) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) TimeValue.timeValueMillis(org.elasticsearch.common.unit.TimeValue.timeValueMillis) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ESTestCase(org.elasticsearch.test.ESTestCase) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) FileSystemUtils(org.elasticsearch.common.io.FileSystemUtils) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Matchers.empty(org.hamcrest.Matchers.empty) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) FutureCallback(org.apache.http.concurrent.FutureCallback) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) InputStreamReader(java.io.InputStreamReader) Consumer(java.util.function.Consumer) ProtocolVersion(org.apache.http.ProtocolVersion) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) HttpResponse(org.apache.http.HttpResponse) InputStreamEntity(org.apache.http.entity.InputStreamEntity) HttpHost(org.apache.http.HttpHost) HeapBufferedAsyncResponseConsumer(org.elasticsearch.client.HeapBufferedAsyncResponseConsumer) HttpAsyncResponseConsumer(org.apache.http.nio.protocol.HttpAsyncResponseConsumer) Consumer(java.util.function.Consumer) HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder)

Example 15 with Version

use of org.elasticsearch.Version in project elasticsearch by elastic.

the class RemoteRequestBuildersTests method testInitialSearchParamsSort.

public void testInitialSearchParamsSort() {
    SearchRequest searchRequest = new SearchRequest().source(new SearchSourceBuilder());
    // Test sort:_doc for versions that support it.
    Version remoteVersion = Version.fromId(between(Version.V_2_1_0_ID, Version.CURRENT.id));
    searchRequest.source().sort("_doc");
    assertThat(initialSearchParams(searchRequest, remoteVersion), hasEntry("sort", "_doc:asc"));
    // Test search_type scan for versions that don't support sort:_doc.
    remoteVersion = Version.fromId(between(0, Version.V_2_1_0_ID - 1));
    assertThat(initialSearchParams(searchRequest, remoteVersion), hasEntry("search_type", "scan"));
    // Test sorting by some field. Version doesn't matter.
    remoteVersion = Version.fromId(between(0, Version.CURRENT.id));
    searchRequest.source().sorts().clear();
    searchRequest.source().sort("foo");
    assertThat(initialSearchParams(searchRequest, remoteVersion), hasEntry("sort", "foo:asc"));
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Version(org.elasticsearch.Version) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Aggregations

Version (org.elasticsearch.Version)124 Settings (org.elasticsearch.common.settings.Settings)51 StreamInput (org.elasticsearch.common.io.stream.StreamInput)21 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)20 HashMap (java.util.HashMap)17 ArrayList (java.util.ArrayList)16 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)16 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)14 IOException (java.io.IOException)13 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 List (java.util.List)11 Map (java.util.Map)11 ClusterState (org.elasticsearch.cluster.ClusterState)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ClusterName (org.elasticsearch.cluster.ClusterName)9 TimeValue (org.elasticsearch.common.unit.TimeValue)9 VersionUtils.randomVersion (org.elasticsearch.test.VersionUtils.randomVersion)9 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)8 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)8 GeoPoint (org.elasticsearch.common.geo.GeoPoint)8