Skip to content

Commit 658660e

Browse files
retaKarthik Subramanian
authored andcommitted
Add basic Search API integration test (#678)
Signed-off-by: Andriy Redko <andriy.redko@aiven.io> Signed-off-by: Karthik Subramanian <ksubramanian@scholastic.com>
1 parent 2b7ea08 commit 658660e

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.client.opensearch.integTest;
10+
11+
import static org.hamcrest.CoreMatchers.equalTo;
12+
import static org.hamcrest.MatcherAssert.assertThat;
13+
import static org.hamcrest.Matchers.arrayContaining;
14+
import static org.hamcrest.Matchers.hasSize;
15+
16+
import java.io.IOException;
17+
import org.junit.Test;
18+
import org.opensearch.client.opensearch._types.FieldValue;
19+
import org.opensearch.client.opensearch._types.SortOrder;
20+
import org.opensearch.client.opensearch._types.mapping.Property;
21+
import org.opensearch.client.opensearch._types.query_dsl.Query;
22+
import org.opensearch.client.opensearch._types.query_dsl.TermQuery;
23+
import org.opensearch.client.opensearch.core.SearchRequest;
24+
import org.opensearch.client.opensearch.core.SearchResponse;
25+
import org.opensearch.client.opensearch.indices.SegmentSortOrder;
26+
27+
public abstract class AbstractSearchRequestIT extends OpenSearchJavaClientTestCase {
28+
29+
@Test
30+
public void shouldReturnSearcheResults() throws Exception {
31+
final String index = "searches_request";
32+
assertThat(
33+
javaClient().indices()
34+
.create(
35+
b -> b.index(index)
36+
.mappings(
37+
m -> m.properties("name", Property.of(p -> p.keyword(v -> v.docValues(true))))
38+
.properties("size", Property.of(p -> p.keyword(v -> v.docValues(true))))
39+
)
40+
.settings(settings -> settings.sort(s -> s.field("name").order(SegmentSortOrder.Asc)))
41+
)
42+
.acknowledged(),
43+
equalTo(true)
44+
);
45+
46+
createTestDocuments(index);
47+
javaClient().indices().refresh();
48+
49+
final Query query = Query.of(
50+
q -> q.bool(
51+
builder -> builder.filter(filter -> filter.term(TermQuery.of(term -> term.field("size").value(FieldValue.of("huge")))))
52+
)
53+
);
54+
55+
final SearchRequest request = SearchRequest.of(
56+
r -> r.index(index)
57+
.sort(s -> s.field(f -> f.field("name").order(SortOrder.Asc)))
58+
.fields(f -> f.field("name"))
59+
.query(query)
60+
.source(s -> s.fetch(true))
61+
);
62+
63+
final SearchResponse<ShopItem> response = javaClient().search(request, ShopItem.class);
64+
assertThat(response.hits().hits(), hasSize(2));
65+
66+
assertThat(response.hits().hits().get(0).fields().get("name").to(String[].class), arrayContaining("hummer"));
67+
assertThat(response.hits().hits().get(1).fields().get("name").to(String[].class), arrayContaining("jammer"));
68+
}
69+
70+
private void createTestDocuments(String index) throws IOException {
71+
javaClient().create(_1 -> _1.index(index).id("1").document(createItem("hummer", "huge", "yes", 2)));
72+
javaClient().create(_1 -> _1.index(index).id("2").document(createItem("jammer", "huge", "yes", 1)));
73+
javaClient().create(_1 -> _1.index(index).id("3").document(createItem("hammer", "large", "yes", 3)));
74+
javaClient().create(_1 -> _1.index(index).id("4").document(createItem("drill", "large", "yes", 3)));
75+
javaClient().create(_1 -> _1.index(index).id("5").document(createItem("jack", "medium", "yes", 2)));
76+
javaClient().create(_1 -> _1.index(index).id("6").document(createItem("wrench", "medium", "no", 3)));
77+
javaClient().create(_1 -> _1.index(index).id("7").document(createItem("screws", "small", "no", 1)));
78+
javaClient().create(_1 -> _1.index(index).id("8").document(createItem("nuts", "small", "no", 2)));
79+
}
80+
81+
private ShopItem createItem(String name, String size, String company, int quantity) {
82+
return new ShopItem(name, size, company, quantity);
83+
}
84+
85+
public static class ShopItem {
86+
private String name;
87+
private String size;
88+
private String company;
89+
private int quantity;
90+
91+
public ShopItem() {}
92+
93+
public ShopItem(String name, String size, String company, int quantity) {
94+
this.name = name;
95+
this.size = size;
96+
this.company = company;
97+
this.quantity = quantity;
98+
}
99+
100+
public String getName() {
101+
return name;
102+
}
103+
104+
public void setName(String name) {
105+
this.name = name;
106+
}
107+
108+
public String getSize() {
109+
return size;
110+
}
111+
112+
public void setSize(String size) {
113+
this.size = size;
114+
}
115+
116+
public String getCompany() {
117+
return company;
118+
}
119+
120+
public void setCompany(String company) {
121+
this.company = company;
122+
}
123+
124+
public int getQuantity() {
125+
return quantity;
126+
}
127+
128+
public void setQuantity(int quantity) {
129+
this.quantity = quantity;
130+
}
131+
}
132+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.client.opensearch.integTest.httpclient5;
10+
11+
import org.opensearch.client.opensearch.integTest.AbstractSearchRequestIT;
12+
13+
public class SearchRequestIT extends AbstractSearchRequestIT implements HttpClient5TransportSupport {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.client.opensearch.integTest.restclient;
10+
11+
import java.io.IOException;
12+
import org.apache.hc.core5.http.HttpHost;
13+
import org.opensearch.client.json.jackson.JacksonJsonpMapper;
14+
import org.opensearch.client.opensearch.integTest.AbstractSearchRequestIT;
15+
import org.opensearch.client.transport.OpenSearchTransport;
16+
import org.opensearch.client.transport.rest_client.RestClientTransport;
17+
import org.opensearch.common.settings.Settings;
18+
19+
public class SearchRequestIT extends AbstractSearchRequestIT {
20+
@Override
21+
public OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) throws IOException {
22+
return new RestClientTransport(buildClient(settings, hosts), new JacksonJsonpMapper());
23+
}
24+
}

0 commit comments

Comments
 (0)