Skip to content

Commit

Permalink
add searchRequestToJsonTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamir-Schwarz committed Oct 29, 2023
1 parent 202a1fd commit 24e3f29
Showing 1 changed file with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@
import jakarta.json.JsonValue;
import jakarta.json.stream.JsonParser;
import java.io.StringReader;

import org.junit.Assert;
import org.junit.Test;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpUtils;
import org.opensearch.client.json.jsonb.JsonbJsonpMapper;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch._types.query_dsl.TermQuery;
import org.opensearch.client.opensearch.core.IndexRequest;
import org.opensearch.client.opensearch.core.SearchRequest;
import org.opensearch.client.opensearch.experiments.api.query2.BoolQuery;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class JsonDataTest extends Assert {
Expand Down Expand Up @@ -114,7 +119,7 @@ public String getName() {
}

@Test
public void testToJson() {
public void indexRequestToJsonTest() {
String expectedJson = "{\"aga\":42,\"name\":\"koko\"}";
Person person = new Person(42, "koko");
IndexRequest<Person> indexRequest = new IndexRequest.Builder<Person>()
Expand All @@ -129,4 +134,27 @@ public void testToJson() {
assertEquals(expectedJson, json);
}

@Test
public void searchRequestToJsonTest() {
String expectedJson = "{\"query\":{\"term\":{\"key-field\":{\"value\":\"value-filed\"}}}}";

SearchRequest searchRequest = new SearchRequest
.Builder()
.index("my-index")
.query(new TermQuery
.Builder()
.field("key-field")
.value(FieldValue.of("value-filed"))
.build()
._toQuery()
)
.build();

String json = JsonpUtils.toJson(searchRequest);
System.out.println(json);

assertEquals(expectedJson, json);
}


}

0 comments on commit 24e3f29

Please sign in to comment.