Search in sources :

Example 26 with SearchQuery

use of org.hibernate.search.engine.search.query.SearchQuery in project hibernate-search by hibernate.

the class ElasticsearchExtensionIT method sort_nativeField_fromJson_string.

@Test
public void sort_nativeField_fromJson_string() {
    StubMappingScope scope = mainIndex.createScope();
    SearchQuery<DocumentReference> query = scope.query().where(f -> f.matchAll()).sort(f -> f.extension(ElasticsearchExtension.get()).fromJson("{'nativeField_sort1': 'asc'}").then().extension(ElasticsearchExtension.get()).fromJson("{'nativeField_sort2': 'asc'}").then().extension(ElasticsearchExtension.get()).fromJson("{'nativeField_sort3': 'asc'}").then().extension(ElasticsearchExtension.get()).fromJson("{'nativeField_sort4': {'order': 'asc', 'missing': '_last'}}").then().extension(ElasticsearchExtension.get()).fromJson("{'nativeField_sort5': {'order': 'asc', 'missing': '_first'}}")).toQuery();
    assertThatQuery(query).hasDocRefHitsExactOrder(mainIndex.typeName(), FIRST_ID, SECOND_ID, THIRD_ID, FOURTH_ID, EMPTY_ID, FIFTH_ID);
    query = scope.query().where(f -> f.matchAll()).sort(f -> f.extension(ElasticsearchExtension.get()).fromJson("{'nativeField_sort1': 'desc'}").then().extension(ElasticsearchExtension.get()).fromJson("{'nativeField_sort2': 'desc'}").then().extension(ElasticsearchExtension.get()).fromJson("{'nativeField_sort3': 'desc'}").then().extension(ElasticsearchExtension.get()).fromJson("{'nativeField_sort4': {'order': 'desc', 'missing': '_last'}}").then().extension(ElasticsearchExtension.get()).fromJson("{'nativeField_sort5': {'order': 'asc', 'missing': '_first'}}")).toQuery();
    assertThatQuery(query).hasDocRefHitsExactOrder(mainIndex.typeName(), FOURTH_ID, THIRD_ID, SECOND_ID, FIRST_ID, EMPTY_ID, FIFTH_ID);
}
Also used : SearchException(org.hibernate.search.util.common.SearchException) JsonObject(com.google.gson.JsonObject) Backend(org.hibernate.search.engine.backend.Backend) ElasticsearchIndexManager(org.hibernate.search.backend.elasticsearch.index.ElasticsearchIndexManager) IndexObjectFieldReference(org.hibernate.search.engine.backend.document.IndexObjectFieldReference) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SearchSetupHelper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper) ElasticsearchSearchQueryWhereStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQueryWhereStep) JSONException(org.json.JSONException) Gson(com.google.gson.Gson) Map(java.util.Map) IndexManager(org.hibernate.search.engine.backend.index.IndexManager) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) ElasticsearchSearchQueryOptionsStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQueryOptionsStep) SearchProjection(org.hibernate.search.engine.search.projection.SearchProjection) JsonHelper.assertJsonEquals(org.hibernate.search.util.impl.test.JsonHelper.assertJsonEquals) ElasticsearchSearchScrollResult(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchScrollResult) IndexSchemaElement(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement) IndexSchemaObjectField(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField) Collectors(java.util.stream.Collectors) ElasticsearchIndexMetadataTestUtils.defaultPrimaryName(org.hibernate.search.util.impl.integrationtest.backend.elasticsearch.ElasticsearchIndexMetadataTestUtils.defaultPrimaryName) IndexFieldReference(org.hibernate.search.engine.backend.document.IndexFieldReference) List(java.util.List) TestForIssue(org.hibernate.search.util.impl.test.annotation.TestForIssue) Aggregable(org.hibernate.search.engine.backend.types.Aggregable) Response(org.elasticsearch.client.Response) JSONCompareMode(org.skyscreamer.jsonassert.JSONCompareMode) SearchHitsAssert.assertThatHits(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchHitsAssert.assertThatHits) Optional(java.util.Optional) ElasticsearchBackend(org.hibernate.search.backend.elasticsearch.ElasticsearchBackend) Projectable(org.hibernate.search.engine.backend.types.Projectable) ElasticsearchSearchQuery(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchQuery) RestClient(org.elasticsearch.client.RestClient) SearchLoadingContext(org.hibernate.search.engine.search.loading.spi.SearchLoadingContext) SearchIntegration(org.hibernate.search.engine.common.spi.SearchIntegration) SearchPredicate(org.hibernate.search.engine.search.predicate.SearchPredicate) SearchSort(org.hibernate.search.engine.search.sort.SearchSort) SearchResultAssert.assertThatQuery(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery) ObjectStructure(org.hibernate.search.engine.backend.types.ObjectStructure) ArrayList(java.util.ArrayList) ElasticsearchSearchResult(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchResult) JsonElement(com.google.gson.JsonElement) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ElasticsearchSearchScroll(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchScroll) SimpleMappedIndex(org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex) ValueConvert(org.hibernate.search.engine.search.common.ValueConvert) JsonPrimitive(com.google.gson.JsonPrimitive) ElasticsearchSearchQuerySelectStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQuerySelectStep) DocumentElement(org.hibernate.search.engine.backend.document.DocumentElement) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) ValueWrapper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.ValueWrapper) Before(org.junit.Before) Sortable(org.hibernate.search.engine.backend.types.Sortable) AggregationKey(org.hibernate.search.engine.search.aggregation.AggregationKey) Test(org.junit.Test) Assertions.entry(org.assertj.core.api.Assertions.entry) SearchResultAssert.assertThatResult(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatResult) Request(org.elasticsearch.client.Request) ElasticsearchExtension(org.hibernate.search.backend.elasticsearch.ElasticsearchExtension) Rule(org.junit.Rule) StubLoadingOptionsStep(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubLoadingOptionsStep) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) HttpAsyncClient(org.apache.http.nio.client.HttpAsyncClient) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) Test(org.junit.Test)

Example 27 with SearchQuery

use of org.hibernate.search.engine.search.query.SearchQuery in project hibernate-search by hibernate.

the class ElasticsearchExtensionIT method projection_jsonHit.

@Test
public void projection_jsonHit() {
    StubMappingScope scope = mainIndex.createScope();
    SearchQuery<JsonObject> query = scope.query().select(f -> f.extension(ElasticsearchExtension.get()).jsonHit()).where(f -> f.id().matching(FIRST_ID)).toQuery();
    List<JsonObject> result = query.fetchAll().hits();
    assertThat(result).hasSize(1);
    assertJsonEquals("{" + "'_id': '" + FIRST_ID + "'," + "'_index': '" + defaultPrimaryName(mainIndex.name()) + "'" + "}", result.get(0).toString(), JSONCompareMode.LENIENT);
}
Also used : SearchException(org.hibernate.search.util.common.SearchException) JsonObject(com.google.gson.JsonObject) Backend(org.hibernate.search.engine.backend.Backend) ElasticsearchIndexManager(org.hibernate.search.backend.elasticsearch.index.ElasticsearchIndexManager) IndexObjectFieldReference(org.hibernate.search.engine.backend.document.IndexObjectFieldReference) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SearchSetupHelper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper) ElasticsearchSearchQueryWhereStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQueryWhereStep) JSONException(org.json.JSONException) Gson(com.google.gson.Gson) Map(java.util.Map) IndexManager(org.hibernate.search.engine.backend.index.IndexManager) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) ElasticsearchSearchQueryOptionsStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQueryOptionsStep) SearchProjection(org.hibernate.search.engine.search.projection.SearchProjection) JsonHelper.assertJsonEquals(org.hibernate.search.util.impl.test.JsonHelper.assertJsonEquals) ElasticsearchSearchScrollResult(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchScrollResult) IndexSchemaElement(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement) IndexSchemaObjectField(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField) Collectors(java.util.stream.Collectors) ElasticsearchIndexMetadataTestUtils.defaultPrimaryName(org.hibernate.search.util.impl.integrationtest.backend.elasticsearch.ElasticsearchIndexMetadataTestUtils.defaultPrimaryName) IndexFieldReference(org.hibernate.search.engine.backend.document.IndexFieldReference) List(java.util.List) TestForIssue(org.hibernate.search.util.impl.test.annotation.TestForIssue) Aggregable(org.hibernate.search.engine.backend.types.Aggregable) Response(org.elasticsearch.client.Response) JSONCompareMode(org.skyscreamer.jsonassert.JSONCompareMode) SearchHitsAssert.assertThatHits(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchHitsAssert.assertThatHits) Optional(java.util.Optional) ElasticsearchBackend(org.hibernate.search.backend.elasticsearch.ElasticsearchBackend) Projectable(org.hibernate.search.engine.backend.types.Projectable) ElasticsearchSearchQuery(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchQuery) RestClient(org.elasticsearch.client.RestClient) SearchLoadingContext(org.hibernate.search.engine.search.loading.spi.SearchLoadingContext) SearchIntegration(org.hibernate.search.engine.common.spi.SearchIntegration) SearchPredicate(org.hibernate.search.engine.search.predicate.SearchPredicate) SearchSort(org.hibernate.search.engine.search.sort.SearchSort) SearchResultAssert.assertThatQuery(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery) ObjectStructure(org.hibernate.search.engine.backend.types.ObjectStructure) ArrayList(java.util.ArrayList) ElasticsearchSearchResult(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchResult) JsonElement(com.google.gson.JsonElement) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ElasticsearchSearchScroll(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchScroll) SimpleMappedIndex(org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex) ValueConvert(org.hibernate.search.engine.search.common.ValueConvert) JsonPrimitive(com.google.gson.JsonPrimitive) ElasticsearchSearchQuerySelectStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQuerySelectStep) DocumentElement(org.hibernate.search.engine.backend.document.DocumentElement) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) ValueWrapper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.ValueWrapper) Before(org.junit.Before) Sortable(org.hibernate.search.engine.backend.types.Sortable) AggregationKey(org.hibernate.search.engine.search.aggregation.AggregationKey) Test(org.junit.Test) Assertions.entry(org.assertj.core.api.Assertions.entry) SearchResultAssert.assertThatResult(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatResult) Request(org.elasticsearch.client.Request) ElasticsearchExtension(org.hibernate.search.backend.elasticsearch.ElasticsearchExtension) Rule(org.junit.Rule) StubLoadingOptionsStep(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubLoadingOptionsStep) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) HttpAsyncClient(org.apache.http.nio.client.HttpAsyncClient) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 28 with SearchQuery

use of org.hibernate.search.engine.search.query.SearchQuery in project hibernate-search by hibernate.

the class ElasticsearchExtensionIT method projection_explanation.

@Test
public void projection_explanation() {
    StubMappingScope scope = mainIndex.createScope();
    SearchQuery<JsonObject> query = scope.query().select(f -> f.extension(ElasticsearchExtension.get()).explanation()).where(f -> f.id().matching(FIRST_ID)).toQuery();
    List<JsonObject> result = query.fetchAll().hits();
    assertThat(result).hasSize(1);
    assertThat(result.get(0)).asString().contains("\"description\":").contains("\"details\":");
}
Also used : SearchException(org.hibernate.search.util.common.SearchException) JsonObject(com.google.gson.JsonObject) Backend(org.hibernate.search.engine.backend.Backend) ElasticsearchIndexManager(org.hibernate.search.backend.elasticsearch.index.ElasticsearchIndexManager) IndexObjectFieldReference(org.hibernate.search.engine.backend.document.IndexObjectFieldReference) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SearchSetupHelper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper) ElasticsearchSearchQueryWhereStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQueryWhereStep) JSONException(org.json.JSONException) Gson(com.google.gson.Gson) Map(java.util.Map) IndexManager(org.hibernate.search.engine.backend.index.IndexManager) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) ElasticsearchSearchQueryOptionsStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQueryOptionsStep) SearchProjection(org.hibernate.search.engine.search.projection.SearchProjection) JsonHelper.assertJsonEquals(org.hibernate.search.util.impl.test.JsonHelper.assertJsonEquals) ElasticsearchSearchScrollResult(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchScrollResult) IndexSchemaElement(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement) IndexSchemaObjectField(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField) Collectors(java.util.stream.Collectors) ElasticsearchIndexMetadataTestUtils.defaultPrimaryName(org.hibernate.search.util.impl.integrationtest.backend.elasticsearch.ElasticsearchIndexMetadataTestUtils.defaultPrimaryName) IndexFieldReference(org.hibernate.search.engine.backend.document.IndexFieldReference) List(java.util.List) TestForIssue(org.hibernate.search.util.impl.test.annotation.TestForIssue) Aggregable(org.hibernate.search.engine.backend.types.Aggregable) Response(org.elasticsearch.client.Response) JSONCompareMode(org.skyscreamer.jsonassert.JSONCompareMode) SearchHitsAssert.assertThatHits(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchHitsAssert.assertThatHits) Optional(java.util.Optional) ElasticsearchBackend(org.hibernate.search.backend.elasticsearch.ElasticsearchBackend) Projectable(org.hibernate.search.engine.backend.types.Projectable) ElasticsearchSearchQuery(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchQuery) RestClient(org.elasticsearch.client.RestClient) SearchLoadingContext(org.hibernate.search.engine.search.loading.spi.SearchLoadingContext) SearchIntegration(org.hibernate.search.engine.common.spi.SearchIntegration) SearchPredicate(org.hibernate.search.engine.search.predicate.SearchPredicate) SearchSort(org.hibernate.search.engine.search.sort.SearchSort) SearchResultAssert.assertThatQuery(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery) ObjectStructure(org.hibernate.search.engine.backend.types.ObjectStructure) ArrayList(java.util.ArrayList) ElasticsearchSearchResult(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchResult) JsonElement(com.google.gson.JsonElement) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ElasticsearchSearchScroll(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchScroll) SimpleMappedIndex(org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex) ValueConvert(org.hibernate.search.engine.search.common.ValueConvert) JsonPrimitive(com.google.gson.JsonPrimitive) ElasticsearchSearchQuerySelectStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQuerySelectStep) DocumentElement(org.hibernate.search.engine.backend.document.DocumentElement) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) ValueWrapper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.ValueWrapper) Before(org.junit.Before) Sortable(org.hibernate.search.engine.backend.types.Sortable) AggregationKey(org.hibernate.search.engine.search.aggregation.AggregationKey) Test(org.junit.Test) Assertions.entry(org.assertj.core.api.Assertions.entry) SearchResultAssert.assertThatResult(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatResult) Request(org.elasticsearch.client.Request) ElasticsearchExtension(org.hibernate.search.backend.elasticsearch.ElasticsearchExtension) Rule(org.junit.Rule) StubLoadingOptionsStep(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubLoadingOptionsStep) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) HttpAsyncClient(org.apache.http.nio.client.HttpAsyncClient) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 29 with SearchQuery

use of org.hibernate.search.engine.search.query.SearchQuery in project hibernate-search by hibernate.

the class ElasticsearchExtensionIT method aggregation_nativeField_fromJson_jsonObject.

@Test
public void aggregation_nativeField_fromJson_jsonObject() {
    StubMappingScope scope = mainIndex.createScope();
    AggregationKey<JsonObject> documentCountPerValue = AggregationKey.of("documentCountPerValue");
    SearchQuery<DocumentReference> query = scope.query().extension(ElasticsearchExtension.get()).where(f -> f.matchAll()).aggregation(documentCountPerValue, f -> f.fromJson(gson.fromJson("{" + "'value_count' : {" + "'field' : 'nativeField_aggregation'" + " }" + "}", JsonObject.class))).toQuery();
    JsonObject aggregationResult = query.fetchAll().aggregation(documentCountPerValue);
    assertJsonEquals("{" + "'value': 3" + "}", aggregationResult.toString());
}
Also used : SearchException(org.hibernate.search.util.common.SearchException) JsonObject(com.google.gson.JsonObject) Backend(org.hibernate.search.engine.backend.Backend) ElasticsearchIndexManager(org.hibernate.search.backend.elasticsearch.index.ElasticsearchIndexManager) IndexObjectFieldReference(org.hibernate.search.engine.backend.document.IndexObjectFieldReference) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SearchSetupHelper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper) ElasticsearchSearchQueryWhereStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQueryWhereStep) JSONException(org.json.JSONException) Gson(com.google.gson.Gson) Map(java.util.Map) IndexManager(org.hibernate.search.engine.backend.index.IndexManager) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) ElasticsearchSearchQueryOptionsStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQueryOptionsStep) SearchProjection(org.hibernate.search.engine.search.projection.SearchProjection) JsonHelper.assertJsonEquals(org.hibernate.search.util.impl.test.JsonHelper.assertJsonEquals) ElasticsearchSearchScrollResult(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchScrollResult) IndexSchemaElement(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement) IndexSchemaObjectField(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField) Collectors(java.util.stream.Collectors) ElasticsearchIndexMetadataTestUtils.defaultPrimaryName(org.hibernate.search.util.impl.integrationtest.backend.elasticsearch.ElasticsearchIndexMetadataTestUtils.defaultPrimaryName) IndexFieldReference(org.hibernate.search.engine.backend.document.IndexFieldReference) List(java.util.List) TestForIssue(org.hibernate.search.util.impl.test.annotation.TestForIssue) Aggregable(org.hibernate.search.engine.backend.types.Aggregable) Response(org.elasticsearch.client.Response) JSONCompareMode(org.skyscreamer.jsonassert.JSONCompareMode) SearchHitsAssert.assertThatHits(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchHitsAssert.assertThatHits) Optional(java.util.Optional) ElasticsearchBackend(org.hibernate.search.backend.elasticsearch.ElasticsearchBackend) Projectable(org.hibernate.search.engine.backend.types.Projectable) ElasticsearchSearchQuery(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchQuery) RestClient(org.elasticsearch.client.RestClient) SearchLoadingContext(org.hibernate.search.engine.search.loading.spi.SearchLoadingContext) SearchIntegration(org.hibernate.search.engine.common.spi.SearchIntegration) SearchPredicate(org.hibernate.search.engine.search.predicate.SearchPredicate) SearchSort(org.hibernate.search.engine.search.sort.SearchSort) SearchResultAssert.assertThatQuery(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery) ObjectStructure(org.hibernate.search.engine.backend.types.ObjectStructure) ArrayList(java.util.ArrayList) ElasticsearchSearchResult(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchResult) JsonElement(com.google.gson.JsonElement) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ElasticsearchSearchScroll(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchScroll) SimpleMappedIndex(org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex) ValueConvert(org.hibernate.search.engine.search.common.ValueConvert) JsonPrimitive(com.google.gson.JsonPrimitive) ElasticsearchSearchQuerySelectStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQuerySelectStep) DocumentElement(org.hibernate.search.engine.backend.document.DocumentElement) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) ValueWrapper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.ValueWrapper) Before(org.junit.Before) Sortable(org.hibernate.search.engine.backend.types.Sortable) AggregationKey(org.hibernate.search.engine.search.aggregation.AggregationKey) Test(org.junit.Test) Assertions.entry(org.assertj.core.api.Assertions.entry) SearchResultAssert.assertThatResult(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatResult) Request(org.elasticsearch.client.Request) ElasticsearchExtension(org.hibernate.search.backend.elasticsearch.ElasticsearchExtension) Rule(org.junit.Rule) StubLoadingOptionsStep(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubLoadingOptionsStep) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) HttpAsyncClient(org.apache.http.nio.client.HttpAsyncClient) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) JsonObject(com.google.gson.JsonObject) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) Test(org.junit.Test)

Example 30 with SearchQuery

use of org.hibernate.search.engine.search.query.SearchQuery in project hibernate-search by hibernate.

the class ElasticsearchExtensionIT method query.

@Test
public void query() {
    StubMappingScope scope = mainIndex.createScope();
    SearchQuery<DocumentReference> genericQuery = scope.query().where(f -> f.matchAll()).toQuery();
    // Put the query and result into variables to check they have the right type
    ElasticsearchSearchQuery<DocumentReference> query = genericQuery.extension(ElasticsearchExtension.get());
    ElasticsearchSearchResult<DocumentReference> result = query.fetchAll();
    assertThatResult(result).fromQuery(query).hasDocRefHitsAnyOrder(mainIndex.typeName(), FIRST_ID, SECOND_ID, THIRD_ID, FOURTH_ID, FIFTH_ID, EMPTY_ID).hasTotalHitCount(6);
    // Unsupported extension
    assertThatThrownBy(() -> query.extension((SearchQuery<DocumentReference> original, SearchLoadingContext<?, ?> loadingContext) -> Optional.empty())).isInstanceOf(SearchException.class);
}
Also used : SearchException(org.hibernate.search.util.common.SearchException) JsonObject(com.google.gson.JsonObject) Backend(org.hibernate.search.engine.backend.Backend) ElasticsearchIndexManager(org.hibernate.search.backend.elasticsearch.index.ElasticsearchIndexManager) IndexObjectFieldReference(org.hibernate.search.engine.backend.document.IndexObjectFieldReference) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SearchSetupHelper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper) ElasticsearchSearchQueryWhereStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQueryWhereStep) JSONException(org.json.JSONException) Gson(com.google.gson.Gson) Map(java.util.Map) IndexManager(org.hibernate.search.engine.backend.index.IndexManager) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) ElasticsearchSearchQueryOptionsStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQueryOptionsStep) SearchProjection(org.hibernate.search.engine.search.projection.SearchProjection) JsonHelper.assertJsonEquals(org.hibernate.search.util.impl.test.JsonHelper.assertJsonEquals) ElasticsearchSearchScrollResult(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchScrollResult) IndexSchemaElement(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement) IndexSchemaObjectField(org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField) Collectors(java.util.stream.Collectors) ElasticsearchIndexMetadataTestUtils.defaultPrimaryName(org.hibernate.search.util.impl.integrationtest.backend.elasticsearch.ElasticsearchIndexMetadataTestUtils.defaultPrimaryName) IndexFieldReference(org.hibernate.search.engine.backend.document.IndexFieldReference) List(java.util.List) TestForIssue(org.hibernate.search.util.impl.test.annotation.TestForIssue) Aggregable(org.hibernate.search.engine.backend.types.Aggregable) Response(org.elasticsearch.client.Response) JSONCompareMode(org.skyscreamer.jsonassert.JSONCompareMode) SearchHitsAssert.assertThatHits(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchHitsAssert.assertThatHits) Optional(java.util.Optional) ElasticsearchBackend(org.hibernate.search.backend.elasticsearch.ElasticsearchBackend) Projectable(org.hibernate.search.engine.backend.types.Projectable) ElasticsearchSearchQuery(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchQuery) RestClient(org.elasticsearch.client.RestClient) SearchLoadingContext(org.hibernate.search.engine.search.loading.spi.SearchLoadingContext) SearchIntegration(org.hibernate.search.engine.common.spi.SearchIntegration) SearchPredicate(org.hibernate.search.engine.search.predicate.SearchPredicate) SearchSort(org.hibernate.search.engine.search.sort.SearchSort) SearchResultAssert.assertThatQuery(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery) ObjectStructure(org.hibernate.search.engine.backend.types.ObjectStructure) ArrayList(java.util.ArrayList) ElasticsearchSearchResult(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchResult) JsonElement(com.google.gson.JsonElement) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ElasticsearchSearchScroll(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchScroll) SimpleMappedIndex(org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex) ValueConvert(org.hibernate.search.engine.search.common.ValueConvert) JsonPrimitive(com.google.gson.JsonPrimitive) ElasticsearchSearchQuerySelectStep(org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQuerySelectStep) DocumentElement(org.hibernate.search.engine.backend.document.DocumentElement) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) ValueWrapper(org.hibernate.search.integrationtest.backend.tck.testsupport.util.ValueWrapper) Before(org.junit.Before) Sortable(org.hibernate.search.engine.backend.types.Sortable) AggregationKey(org.hibernate.search.engine.search.aggregation.AggregationKey) Test(org.junit.Test) Assertions.entry(org.assertj.core.api.Assertions.entry) SearchResultAssert.assertThatResult(org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatResult) Request(org.elasticsearch.client.Request) ElasticsearchExtension(org.hibernate.search.backend.elasticsearch.ElasticsearchExtension) Rule(org.junit.Rule) StubLoadingOptionsStep(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubLoadingOptionsStep) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) HttpAsyncClient(org.apache.http.nio.client.HttpAsyncClient) ElasticsearchSearchQuery(org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchQuery) SearchQuery(org.hibernate.search.engine.search.query.SearchQuery) StubMappingScope(org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope) DocumentReference(org.hibernate.search.engine.backend.common.DocumentReference) SearchLoadingContext(org.hibernate.search.engine.search.loading.spi.SearchLoadingContext) Test(org.junit.Test)

Aggregations

SearchQuery (org.hibernate.search.engine.search.query.SearchQuery)229 Test (org.junit.Test)222 Rule (org.junit.Rule)197 SearchSetupHelper (org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper)186 IndexSchemaElement (org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement)185 SimpleMappedIndex (org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex)185 StubMappingScope (org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope)179 Before (org.junit.Before)177 SearchResultAssert.assertThatQuery (org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery)164 List (java.util.List)158 IndexFieldReference (org.hibernate.search.engine.backend.document.IndexFieldReference)153 DocumentReference (org.hibernate.search.engine.backend.common.DocumentReference)151 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)135 TestForIssue (org.hibernate.search.util.impl.test.annotation.TestForIssue)129 Projectable (org.hibernate.search.engine.backend.types.Projectable)127 SearchException (org.hibernate.search.util.common.SearchException)119 DocumentElement (org.hibernate.search.engine.backend.document.DocumentElement)113 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)110 IndexObjectFieldReference (org.hibernate.search.engine.backend.document.IndexObjectFieldReference)102 IndexSchemaObjectField (org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField)102