|
| 1 | +package com.liorkn.elasticsearch; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonParser; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import org.apache.http.HttpHost; |
| 6 | +import org.apache.http.entity.ContentType; |
| 7 | +import org.apache.http.entity.StringEntity; |
| 8 | +import org.apache.http.nio.entity.NStringEntity; |
| 9 | +import org.apache.http.util.EntityUtils; |
| 10 | +import org.elasticsearch.client.Response; |
| 11 | +import org.elasticsearch.client.RestClient; |
| 12 | +import org.junit.AfterClass; |
| 13 | +import org.junit.Assert; |
| 14 | +import org.junit.BeforeClass; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +/** |
| 23 | + * Created by Lior Knaany on 4/7/18. |
| 24 | + */ |
| 25 | +public class PluginTest { |
| 26 | + |
| 27 | + private static EmbeddedElasticsearchServer esServer; |
| 28 | + private static RestClient esClient; |
| 29 | + |
| 30 | + @BeforeClass |
| 31 | + public static void init() throws Exception { |
| 32 | + esServer = new EmbeddedElasticsearchServer(); |
| 33 | + esClient = RestClient.builder(new HttpHost("localhost", esServer.getPort(), "http")).build(); |
| 34 | + |
| 35 | + // delete test index if exists |
| 36 | + try { |
| 37 | + esClient.performRequest("DELETE", "/test", Collections.emptyMap()); |
| 38 | + } catch (Exception e) {} |
| 39 | + |
| 40 | + // create test index |
| 41 | + String mappingJson = "{\n" + |
| 42 | + " \"mappings\": {\n" + |
| 43 | + " \"type\": {\n" + |
| 44 | + " \"properties\": {\n" + |
| 45 | + " \"embedding_vector\": {\n" + |
| 46 | + " \"doc_values\": true,\n" + |
| 47 | + " \"type\": \"binary\"\n" + |
| 48 | + " },\n" + |
| 49 | + " \"job_id\": {\n" + |
| 50 | + " \"type\": \"long\"\n" + |
| 51 | + " },\n" + |
| 52 | + " \"vector\": {\n" + |
| 53 | + " \"type\": \"float\"\n" + |
| 54 | + " }\n" + |
| 55 | + " }\n" + |
| 56 | + " }\n" + |
| 57 | + " }\n" + |
| 58 | + "}"; |
| 59 | + esClient.performRequest("PUT", "/test", Collections.emptyMap(), new NStringEntity(mappingJson, ContentType.APPLICATION_JSON)); |
| 60 | + } |
| 61 | + |
| 62 | + public static final ObjectMapper mapper = new ObjectMapper(); |
| 63 | + static { |
| 64 | + mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); |
| 65 | + mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + @Test |
| 70 | + public void test() throws Exception { |
| 71 | + final Map<String, String> params = new HashMap<>(); |
| 72 | + params.put("refresh", "true"); |
| 73 | + final ObjectMapper mapper = new ObjectMapper(); |
| 74 | + final TestObject[] objs = {new TestObject(1, new double[] {0.0, 0.5, 1.0}), |
| 75 | + new TestObject(2, new double[] {0.2, 0.6, 0.99})}; |
| 76 | + |
| 77 | + for (int i = 0; i < objs.length; i++) { |
| 78 | + final TestObject t = objs[i]; |
| 79 | + final String json = mapper.writeValueAsString(t); |
| 80 | + System.out.println(json); |
| 81 | + final Response put = esClient.performRequest("PUT", "/test/type/" + t.jobId, params, new StringEntity(json, ContentType.APPLICATION_JSON)); |
| 82 | + System.out.println(put); |
| 83 | + System.out.println(EntityUtils.toString(put.getEntity())); |
| 84 | + final int statusCode = put.getStatusLine().getStatusCode(); |
| 85 | + Assert.assertTrue(statusCode == 200 || statusCode == 201); |
| 86 | + } |
| 87 | + |
| 88 | + // Test cosine score function |
| 89 | + String body = "{" + |
| 90 | + " \"query\": {" + |
| 91 | + " \"function_score\": {" + |
| 92 | + " \"boost_mode\": \"replace\"," + |
| 93 | + " \"script_score\": {" + |
| 94 | + " \"script\": {" + |
| 95 | + " \"inline\": \"binary_vector_score\"," + |
| 96 | + " \"lang\": \"knn\"," + |
| 97 | + " \"params\": {" + |
| 98 | + " \"cosine\": false," + |
| 99 | + " \"field\": \"embedding_vector\"," + |
| 100 | + " \"vector\": [" + |
| 101 | + " 0.1, 0.2, 0.3" + |
| 102 | + " ]" + |
| 103 | + " }" + |
| 104 | + " }" + |
| 105 | + " }" + |
| 106 | + " }" + |
| 107 | + " }," + |
| 108 | + " \"size\": 100" + |
| 109 | + "}"; |
| 110 | + final Response res = esClient.performRequest("POST", "/test/_search", Collections.emptyMap(), new NStringEntity(body, ContentType.APPLICATION_JSON)); |
| 111 | + System.out.println(res); |
| 112 | + final String resBody = EntityUtils.toString(res.getEntity()); |
| 113 | + System.out.println(resBody); |
| 114 | + Assert.assertEquals("search should return status code 200", 200, res.getStatusLine().getStatusCode()); |
| 115 | + Assert.assertTrue(String.format("There should be %d documents in the search response", objs.length), resBody.contains("\"hits\":{\"total\":" + objs.length)); |
| 116 | + } |
| 117 | + |
| 118 | + @AfterClass |
| 119 | + public static void shutdown() { |
| 120 | + try { |
| 121 | + esClient.close(); |
| 122 | + esServer.shutdown(); |
| 123 | + } catch (IOException e) { |
| 124 | + e.printStackTrace(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments