-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic Search API integration test (#678)
Signed-off-by: Andriy Redko <[email protected]>
- Loading branch information
Showing
3 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
...ent/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchRequestIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.client.opensearch.integTest; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.arrayContaining; | ||
import static org.hamcrest.Matchers.hasSize; | ||
|
||
import java.io.IOException; | ||
import org.junit.Test; | ||
import org.opensearch.client.opensearch._types.FieldValue; | ||
import org.opensearch.client.opensearch._types.SortOrder; | ||
import org.opensearch.client.opensearch._types.mapping.Property; | ||
import org.opensearch.client.opensearch._types.query_dsl.Query; | ||
import org.opensearch.client.opensearch._types.query_dsl.TermQuery; | ||
import org.opensearch.client.opensearch.core.SearchRequest; | ||
import org.opensearch.client.opensearch.core.SearchResponse; | ||
import org.opensearch.client.opensearch.indices.SegmentSortOrder; | ||
|
||
public abstract class AbstractSearchRequestIT extends OpenSearchJavaClientTestCase { | ||
|
||
@Test | ||
public void shouldReturnSearcheResults() throws Exception { | ||
final String index = "searches_request"; | ||
assertThat( | ||
javaClient().indices() | ||
.create( | ||
b -> b.index(index) | ||
.mappings( | ||
m -> m.properties("name", Property.of(p -> p.keyword(v -> v.docValues(true)))) | ||
.properties("size", Property.of(p -> p.keyword(v -> v.docValues(true)))) | ||
) | ||
.settings(settings -> settings.sort(s -> s.field("name").order(SegmentSortOrder.Asc))) | ||
) | ||
.acknowledged(), | ||
equalTo(true) | ||
); | ||
|
||
createTestDocuments(index); | ||
javaClient().indices().refresh(); | ||
|
||
final Query query = Query.of( | ||
q -> q.bool( | ||
builder -> builder.filter(filter -> filter.term(TermQuery.of(term -> term.field("size").value(FieldValue.of("huge"))))) | ||
) | ||
); | ||
|
||
final SearchRequest request = SearchRequest.of( | ||
r -> r.index(index) | ||
.sort(s -> s.field(f -> f.field("name").order(SortOrder.Asc))) | ||
.fields(f -> f.field("name")) | ||
.query(query) | ||
.source(s -> s.fetch(true)) | ||
); | ||
|
||
final SearchResponse<ShopItem> response = javaClient().search(request, ShopItem.class); | ||
assertThat(response.hits().hits(), hasSize(2)); | ||
|
||
assertThat(response.hits().hits().get(0).fields().get("name").to(String[].class), arrayContaining("hummer")); | ||
assertThat(response.hits().hits().get(1).fields().get("name").to(String[].class), arrayContaining("jammer")); | ||
} | ||
|
||
private void createTestDocuments(String index) throws IOException { | ||
javaClient().create(_1 -> _1.index(index).id("1").document(createItem("hummer", "huge", "yes", 2))); | ||
javaClient().create(_1 -> _1.index(index).id("2").document(createItem("jammer", "huge", "yes", 1))); | ||
javaClient().create(_1 -> _1.index(index).id("3").document(createItem("hammer", "large", "yes", 3))); | ||
javaClient().create(_1 -> _1.index(index).id("4").document(createItem("drill", "large", "yes", 3))); | ||
javaClient().create(_1 -> _1.index(index).id("5").document(createItem("jack", "medium", "yes", 2))); | ||
javaClient().create(_1 -> _1.index(index).id("6").document(createItem("wrench", "medium", "no", 3))); | ||
javaClient().create(_1 -> _1.index(index).id("7").document(createItem("screws", "small", "no", 1))); | ||
javaClient().create(_1 -> _1.index(index).id("8").document(createItem("nuts", "small", "no", 2))); | ||
} | ||
|
||
private ShopItem createItem(String name, String size, String company, int quantity) { | ||
return new ShopItem(name, size, company, quantity); | ||
} | ||
|
||
public static class ShopItem { | ||
private String name; | ||
private String size; | ||
private String company; | ||
private int quantity; | ||
|
||
public ShopItem() {} | ||
|
||
public ShopItem(String name, String size, String company, int quantity) { | ||
this.name = name; | ||
this.size = size; | ||
this.company = company; | ||
this.quantity = quantity; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getSize() { | ||
return size; | ||
} | ||
|
||
public void setSize(String size) { | ||
this.size = size; | ||
} | ||
|
||
public String getCompany() { | ||
return company; | ||
} | ||
|
||
public void setCompany(String company) { | ||
this.company = company; | ||
} | ||
|
||
public int getQuantity() { | ||
return quantity; | ||
} | ||
|
||
public void setQuantity(int quantity) { | ||
this.quantity = quantity; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/SearchRequestIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.client.opensearch.integTest.httpclient5; | ||
|
||
import org.opensearch.client.opensearch.integTest.AbstractSearchRequestIT; | ||
|
||
public class SearchRequestIT extends AbstractSearchRequestIT implements HttpClient5TransportSupport {} |
24 changes: 24 additions & 0 deletions
24
.../src/test/java/org/opensearch/client/opensearch/integTest/restclient/SearchRequestIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.client.opensearch.integTest.restclient; | ||
|
||
import java.io.IOException; | ||
import org.apache.hc.core5.http.HttpHost; | ||
import org.opensearch.client.json.jackson.JacksonJsonpMapper; | ||
import org.opensearch.client.opensearch.integTest.AbstractSearchRequestIT; | ||
import org.opensearch.client.transport.OpenSearchTransport; | ||
import org.opensearch.client.transport.rest_client.RestClientTransport; | ||
import org.opensearch.common.settings.Settings; | ||
|
||
public class SearchRequestIT extends AbstractSearchRequestIT { | ||
@Override | ||
public OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) throws IOException { | ||
return new RestClientTransport(buildClient(settings, hosts), new JacksonJsonpMapper()); | ||
} | ||
} |