fixed most tests
This commit is contained in:
parent
95e4e7326d
commit
6f9e27dc59
@ -52,13 +52,13 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.locationtech.jts</groupId>
|
<groupId>org.locationtech.jts</groupId>
|
||||||
<artifactId>jts-core</artifactId>
|
<artifactId>jts-core</artifactId>
|
||||||
<version>1.16.1</version>
|
<version>1.15.0</version>
|
||||||
</dependency>
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
<dependency>
|
<groupId>xerces</groupId>
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
<artifactId>xercesImpl</artifactId>
|
||||||
<artifactId>log4j-core</artifactId>
|
</exclusion>
|
||||||
<version>${log4j.version}</version>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -77,12 +77,19 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<spring-data-elasticsearch.version>3.2.6.RELEASE</spring-data-elasticsearch.version>
|
<spring-data-elasticsearch.version>4.0.0.RC1</spring-data-elasticsearch.version>
|
||||||
<jna.version>4.5.2</jna.version>
|
<jna.version>4.5.2</jna.version>
|
||||||
<fastjson.version>1.2.47</fastjson.version>
|
<fastjson.version>1.2.47</fastjson.version>
|
||||||
<spatial4j.version>0.6</spatial4j.version>
|
<spatial4j.version>0.7</spatial4j.version>
|
||||||
<jts.version>1.13</jts.version>
|
<jts.version>1.13</jts.version>
|
||||||
<log4j.version>2.9.1</log4j.version>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>repository.spring.milestone</id>
|
||||||
|
<name>Spring Milestone Repository</name>
|
||||||
|
<url>https://repo.spring.io/milestone</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -8,9 +8,9 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||||||
import com.baeldung.spring.data.es.config.Config;
|
import com.baeldung.spring.data.es.config.Config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* This Manual test requires: * Elasticsearch instance running on localhost:9200.
|
||||||
* This Manual test requires: * Elasticsearch instance running on host
|
* The following docker command can be used:
|
||||||
*
|
* docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
|
@ -9,12 +9,15 @@ import java.util.Arrays;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
|
|
||||||
import org.elasticsearch.action.DocWriteResponse.Result;
|
import org.elasticsearch.action.DocWriteResponse.Result;
|
||||||
import org.elasticsearch.action.delete.DeleteRequest;
|
import org.elasticsearch.action.delete.DeleteRequest;
|
||||||
import org.elasticsearch.action.delete.DeleteResponse;
|
import org.elasticsearch.action.delete.DeleteResponse;
|
||||||
|
import org.elasticsearch.action.get.GetRequest;
|
||||||
|
import org.elasticsearch.action.get.GetResponse;
|
||||||
import org.elasticsearch.action.index.IndexRequest;
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
import org.elasticsearch.action.index.IndexResponse;
|
import org.elasticsearch.action.index.IndexResponse;
|
||||||
import org.elasticsearch.action.search.SearchRequest;
|
import org.elasticsearch.action.search.SearchRequest;
|
||||||
@ -34,10 +37,9 @@ import org.springframework.data.elasticsearch.client.ClientConfiguration;
|
|||||||
import org.springframework.data.elasticsearch.client.RestClients;
|
import org.springframework.data.elasticsearch.client.RestClients;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* This Manual test requires: * Elasticsearch instance running on localhost:9200.
|
||||||
* This Manual test requires: * Elasticsearch instance running on host * with
|
* The following docker command can be used:
|
||||||
* cluster name = elasticsearch
|
* docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ElasticSearchManualTest {
|
public class ElasticSearchManualTest {
|
||||||
private List<Person> listOfPersons = new ArrayList<>();
|
private List<Person> listOfPersons = new ArrayList<>();
|
||||||
@ -57,28 +59,35 @@ public class ElasticSearchManualTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception {
|
public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception {
|
||||||
String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}";
|
String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}";
|
||||||
IndexRequest request = new IndexRequest("people", "Doe");
|
IndexRequest request = new IndexRequest("people");
|
||||||
request.source(jsonObject, XContentType.JSON);
|
request.source(jsonObject, XContentType.JSON);
|
||||||
|
|
||||||
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
|
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
|
||||||
String index = response.getIndex();
|
String index = response.getIndex();
|
||||||
|
long version = response.getVersion();
|
||||||
|
|
||||||
assertEquals(Result.CREATED, response.getResult());
|
assertEquals(Result.CREATED, response.getResult());
|
||||||
assertEquals(index, "people");
|
assertEquals(1, version);
|
||||||
|
assertEquals("people", index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception {
|
public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception {
|
||||||
String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}";
|
String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}";
|
||||||
IndexRequest indexRequest = new IndexRequest("people", "Doe");
|
IndexRequest indexRequest = new IndexRequest("people");
|
||||||
indexRequest.source(jsonObject, XContentType.JSON);
|
indexRequest.source(jsonObject, XContentType.JSON);
|
||||||
|
|
||||||
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
|
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
|
||||||
String id = response.getId();
|
String id = response.getId();
|
||||||
|
|
||||||
|
GetRequest getRequest = new GetRequest("people");
|
||||||
|
getRequest.id(id);
|
||||||
|
|
||||||
|
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
|
||||||
|
System.out.println(getResponse.getSourceAsString());
|
||||||
|
|
||||||
DeleteRequest deleteRequest = new DeleteRequest("people");
|
DeleteRequest deleteRequest = new DeleteRequest("people");
|
||||||
deleteRequest.id(id);
|
deleteRequest.id(id);
|
||||||
deleteRequest.type("Doe");
|
|
||||||
|
|
||||||
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
|
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
@ -98,8 +107,8 @@ public class ElasticSearchManualTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSearchParameters_thenReturnResults() throws Exception {
|
public void givenSearchParameters_thenReturnResults() throws Exception {
|
||||||
SearchSourceBuilder builder = new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age").from(5).to(15))
|
SearchSourceBuilder builder =
|
||||||
.from(0).size(60).explain(true);
|
new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age").from(5).to(15));
|
||||||
|
|
||||||
SearchRequest searchRequest = new SearchRequest();
|
SearchRequest searchRequest = new SearchRequest();
|
||||||
searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH);
|
searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH);
|
||||||
@ -107,8 +116,7 @@ public class ElasticSearchManualTest {
|
|||||||
|
|
||||||
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
builder = new SearchSourceBuilder().postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette"))
|
builder = new SearchSourceBuilder().postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette"));
|
||||||
.from(0).size(60).explain(true);
|
|
||||||
|
|
||||||
searchRequest = new SearchRequest();
|
searchRequest = new SearchRequest();
|
||||||
searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH);
|
searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH);
|
||||||
@ -116,8 +124,7 @@ public class ElasticSearchManualTest {
|
|||||||
|
|
||||||
SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT);
|
SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
builder = new SearchSourceBuilder().postFilter(QueryBuilders.matchQuery("John", "Name*")).from(0).size(60)
|
builder = new SearchSourceBuilder().postFilter(QueryBuilders.matchQuery("John", "Name*"));
|
||||||
.explain(true);
|
|
||||||
searchRequest = new SearchRequest();
|
searchRequest = new SearchRequest();
|
||||||
searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH);
|
searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH);
|
||||||
searchRequest.source(builder);
|
searchRequest.source(builder);
|
||||||
@ -126,9 +133,15 @@ public class ElasticSearchManualTest {
|
|||||||
|
|
||||||
response2.getHits();
|
response2.getHits();
|
||||||
response3.getHits();
|
response3.getHits();
|
||||||
|
|
||||||
final List<Person> results = Arrays.stream(response.getHits().getHits())
|
final List<Person> results =
|
||||||
.map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)).collect(Collectors.toList());
|
Stream.of(response.getHits().getHits(),
|
||||||
|
response2.getHits().getHits(),
|
||||||
|
response3.getHits().getHits())
|
||||||
|
.flatMap(Arrays::stream)
|
||||||
|
.map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
results.forEach(System.out::println);
|
results.forEach(System.out::println);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,7 +150,7 @@ public class ElasticSearchManualTest {
|
|||||||
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("fullName", "Test")
|
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("fullName", "Test")
|
||||||
.field("salary", "11500").field("age", "10").endObject();
|
.field("salary", "11500").field("age", "10").endObject();
|
||||||
|
|
||||||
IndexRequest indexRequest = new IndexRequest("people", "Doe");
|
IndexRequest indexRequest = new IndexRequest("people");
|
||||||
indexRequest.source(builder);
|
indexRequest.source(builder);
|
||||||
|
|
||||||
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
|
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
|
||||||
|
@ -8,71 +8,84 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
|
import com.baeldung.spring.data.es.config.Config;
|
||||||
|
|
||||||
|
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
||||||
|
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
|
||||||
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
import org.elasticsearch.action.index.IndexResponse;
|
import org.elasticsearch.action.index.IndexResponse;
|
||||||
|
import org.elasticsearch.action.search.SearchRequest;
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.RequestOptions;
|
||||||
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
|
import org.elasticsearch.client.indices.CreateIndexRequest;
|
||||||
import org.elasticsearch.common.geo.GeoPoint;
|
import org.elasticsearch.common.geo.GeoPoint;
|
||||||
|
import org.elasticsearch.common.geo.ShapeRelation;
|
||||||
import org.elasticsearch.common.geo.builders.EnvelopeBuilder;
|
import org.elasticsearch.common.geo.builders.EnvelopeBuilder;
|
||||||
import org.elasticsearch.common.unit.DistanceUnit;
|
import org.elasticsearch.common.unit.DistanceUnit;
|
||||||
import org.elasticsearch.common.xcontent.XContentType;
|
import org.elasticsearch.common.xcontent.XContentType;
|
||||||
|
import org.elasticsearch.index.query.GeoShapeQueryBuilder;
|
||||||
import org.elasticsearch.index.query.QueryBuilder;
|
import org.elasticsearch.index.query.QueryBuilder;
|
||||||
import org.elasticsearch.index.query.QueryBuilders;
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
import org.elasticsearch.search.SearchHit;
|
import org.elasticsearch.search.SearchHit;
|
||||||
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.locationtech.jts.geom.Coordinate;
|
import org.locationtech.jts.geom.Coordinate;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import com.baeldung.spring.data.es.config.Config;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* This Manual test requires: * Elasticsearch instance running on localhost:9200.
|
||||||
* This Manual test requires: * Elasticsearch instance running on host * with
|
* The following docker command can be used:
|
||||||
* cluster name = elasticsearch * and further configurations
|
* docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
public class GeoQueriesManualTest {
|
public class GeoQueriesManualTest {
|
||||||
|
|
||||||
private static final String WONDERS_OF_WORLD = "wonders-of-world";
|
private static final String WONDERS_OF_WORLD = "wonders-of-world";
|
||||||
private static final String WONDERS = "Wonders";
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ElasticsearchTemplate elasticsearchTemplate;
|
private RestHighLevelClient client;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Client client;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() throws Exception {
|
||||||
String jsonObject = "{\"Wonders\":{\"properties\":{\"name\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"region\":{\"type\":\"geo_shape\",\"tree\":\"quadtree\",\"precision\":\"1m\"},\"location\":{\"type\":\"geo_point\"}}}}";
|
String jsonObject = "{\"properties\":{\"name\":{\"type\":\"text\",\"index\":false},\"region\":{\"type\":\"geo_shape\"},\"location\":{\"type\":\"geo_point\"}}}";
|
||||||
|
|
||||||
CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD);
|
CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD);
|
||||||
req.mapping(WONDERS, jsonObject, XContentType.JSON);
|
req.mapping(jsonObject, XContentType.JSON);
|
||||||
client.admin().indices().create(req).actionGet();
|
|
||||||
|
client.indices().create(req, RequestOptions.DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() throws IOException {
|
public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() throws IOException {
|
||||||
String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,30.2],[80.1, 25]]}}";
|
String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,30.2],[80.1, 25]]}}";
|
||||||
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON)
|
IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD);
|
||||||
.get();
|
indexRequest.source(jsonObject, XContentType.JSON);
|
||||||
|
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
String tajMahalId = response.getId();
|
String tajMahalId = response.getId();
|
||||||
client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get();
|
|
||||||
|
RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
|
||||||
|
client.indices().refresh(refreshRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
Coordinate topLeft = new Coordinate(74, 31.2);
|
Coordinate topLeft = new Coordinate(74, 31.2);
|
||||||
Coordinate bottomRight = new Coordinate(81.1, 24);
|
Coordinate bottomRight = new Coordinate(81.1, 24);
|
||||||
QueryBuilder qb = QueryBuilders.geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight));
|
|
||||||
|
GeoShapeQueryBuilder qb = QueryBuilders.geoShapeQuery("region",
|
||||||
|
new EnvelopeBuilder(topLeft, bottomRight).buildGeometry());
|
||||||
|
qb.relation(ShapeRelation.INTERSECTS);
|
||||||
|
|
||||||
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute()
|
SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
|
||||||
.actionGet();
|
SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD);
|
||||||
|
searchRequest.source(source);
|
||||||
|
|
||||||
|
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId)
|
List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
@ -81,62 +94,93 @@ public class GeoQueriesManualTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() {
|
public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() throws Exception {
|
||||||
String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}";
|
String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}";
|
||||||
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON)
|
|
||||||
.get();
|
IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD);
|
||||||
|
indexRequest.source(jsonObject, XContentType.JSON);
|
||||||
|
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
String pyramidsOfGizaId = response.getId();
|
String pyramidsOfGizaId = response.getId();
|
||||||
client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get();
|
|
||||||
|
RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
|
||||||
|
client.indices().refresh(refreshRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location").setCorners(31, 30, 28, 32);
|
QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location").setCorners(31, 30, 28, 32);
|
||||||
|
|
||||||
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute()
|
SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
|
||||||
.actionGet();
|
SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD);
|
||||||
|
searchRequest.source(source);
|
||||||
|
|
||||||
|
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId)
|
List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
assertTrue(ids.contains(pyramidsOfGizaId));
|
assertTrue(ids.contains(pyramidsOfGizaId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() {
|
public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() throws Exception {
|
||||||
String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}";
|
String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}";
|
||||||
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON)
|
|
||||||
.get();
|
IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD);
|
||||||
|
indexRequest.source(jsonObject, XContentType.JSON);
|
||||||
|
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
String lighthouseOfAlexandriaId = response.getId();
|
String lighthouseOfAlexandriaId = response.getId();
|
||||||
client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get();
|
|
||||||
|
|
||||||
QueryBuilder qb = QueryBuilders.geoDistanceQuery("location").point(29.976, 31.131).distance(10, DistanceUnit.MILES);
|
RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
|
||||||
|
client.indices().refresh(refreshRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
|
QueryBuilder qb =
|
||||||
|
QueryBuilders.geoDistanceQuery("location")
|
||||||
|
.point(29.976, 31.131)
|
||||||
|
.distance(10, DistanceUnit.MILES);
|
||||||
|
|
||||||
|
SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
|
||||||
|
SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD);
|
||||||
|
searchRequest.source(source);
|
||||||
|
|
||||||
|
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute()
|
|
||||||
.actionGet();
|
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId)
|
List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
assertTrue(ids.contains(lighthouseOfAlexandriaId));
|
assertTrue(ids.contains(lighthouseOfAlexandriaId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() {
|
public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() throws Exception {
|
||||||
String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}";
|
String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}";
|
||||||
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON)
|
|
||||||
.get();
|
|
||||||
String greatRannOfKutchid = response.getId();
|
|
||||||
client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get();
|
|
||||||
|
|
||||||
|
IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD);
|
||||||
|
indexRequest.source(jsonObject, XContentType.JSON);
|
||||||
|
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
|
String greatRannOfKutchid = response.getId();
|
||||||
|
|
||||||
|
RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
|
||||||
|
client.indices().refresh(refreshRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
List<GeoPoint> allPoints = new ArrayList<GeoPoint>();
|
List<GeoPoint> allPoints = new ArrayList<GeoPoint>();
|
||||||
allPoints.add(new GeoPoint(22.733, 68.859));
|
allPoints.add(new GeoPoint(22.733, 68.859));
|
||||||
allPoints.add(new GeoPoint(24.733, 68.859));
|
allPoints.add(new GeoPoint(24.733, 68.859));
|
||||||
allPoints.add(new GeoPoint(23, 70.859));
|
allPoints.add(new GeoPoint(23, 70.859));
|
||||||
QueryBuilder qb = QueryBuilders.geoPolygonQuery("location", allPoints);
|
QueryBuilder qb = QueryBuilders.geoPolygonQuery("location", allPoints);
|
||||||
|
|
||||||
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute()
|
SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
|
||||||
.actionGet();
|
SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD);
|
||||||
|
searchRequest.source(source);
|
||||||
|
|
||||||
|
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId)
|
List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
assertTrue(ids.contains(greatRannOfKutchid));
|
assertTrue(ids.contains(greatRannOfKutchid));
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void destroy() {
|
public void destroy() throws Exception {
|
||||||
elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD);
|
DeleteIndexRequest deleteIndex = new DeleteIndexRequest(WONDERS_OF_WORLD);
|
||||||
|
client.indices().delete(deleteIndex, RequestOptions.DEFAULT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,16 +22,17 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
||||||
|
import org.springframework.data.elasticsearch.core.SearchHits;
|
||||||
|
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||||
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
import org.springframework.data.elasticsearch.core.query.Query;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* This Manual test requires: * Elasticsearch instance running on localhost:9200.
|
||||||
* This Manual test requires: * Elasticsearch instance running on host * with
|
* The following docker command can be used:
|
||||||
* cluster name = elasticsearch
|
* docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
@ -48,8 +49,8 @@ public class ElasticSearchManualTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() {
|
public void before() {
|
||||||
elasticsearchTemplate.deleteIndex(Article.class);
|
elasticsearchTemplate.indexOps(Article.class).delete();
|
||||||
elasticsearchTemplate.createIndex(Article.class);
|
elasticsearchTemplate.indexOps(Article.class).create();
|
||||||
// don't call putMapping() to test the default mappings
|
// don't call putMapping() to test the default mappings
|
||||||
|
|
||||||
Article article = new Article("Spring Data Elasticsearch");
|
Article article = new Article("Spring Data Elasticsearch");
|
||||||
@ -86,7 +87,6 @@ public class ElasticSearchManualTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() {
|
public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() {
|
||||||
|
|
||||||
final Page<Article> articleByAuthorName = articleRepository.findByAuthorsName(johnSmith.getName(),
|
final Page<Article> articleByAuthorName = articleRepository.findByAuthorsName(johnSmith.getName(),
|
||||||
PageRequest.of(0, 10));
|
PageRequest.of(0, 10));
|
||||||
assertEquals(2L, articleByAuthorName.getTotalElements());
|
assertEquals(2L, articleByAuthorName.getTotalElements());
|
||||||
@ -115,22 +115,23 @@ public class ElasticSearchManualTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() {
|
public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() {
|
||||||
|
final Query searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*"))
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*"))
|
|
||||||
.build();
|
.build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
|
||||||
|
|
||||||
assertEquals(1, articles.size());
|
final SearchHits<Article> articles =
|
||||||
|
elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
|
||||||
|
assertEquals(1, articles.getTotalHits());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() {
|
public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")).build();
|
final Query searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
|
||||||
assertEquals(1, articles.size());
|
assertEquals(1, articles.getTotalHits());
|
||||||
|
|
||||||
final Article article = articles.get(0);
|
final Article article = articles.getSearchHit(0).getContent();
|
||||||
final String newTitle = "Getting started with Search Engines";
|
final String newTitle = "Getting started with Search Engines";
|
||||||
article.setTitle(newTitle);
|
article.setTitle(newTitle);
|
||||||
articleRepository.save(article);
|
articleRepository.save(article);
|
||||||
@ -140,25 +141,27 @@ public class ElasticSearchManualTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSavedDoc_whenDelete_thenRemovedFromIndex() {
|
public void givenSavedDoc_whenDelete_thenRemovedFromIndex() {
|
||||||
|
|
||||||
final String articleTitle = "Spring Data Elasticsearch";
|
final String articleTitle = "Spring Data Elasticsearch";
|
||||||
|
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
final Query searchQuery = new NativeSearchQueryBuilder()
|
||||||
.withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build();
|
.withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final SearchHits<Article> articles =
|
||||||
assertEquals(1, articles.size());
|
elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
|
||||||
|
assertEquals(1, articles.getTotalHits());
|
||||||
final long count = articleRepository.count();
|
final long count = articleRepository.count();
|
||||||
|
|
||||||
articleRepository.delete(articles.get(0));
|
articleRepository.delete(articles.getSearchHit(0).getContent());
|
||||||
|
|
||||||
assertEquals(count - 1, articleRepository.count());
|
assertEquals(count - 1, articleRepository.count());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
|
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
final Query searchQuery = new NativeSearchQueryBuilder()
|
||||||
.withQuery(matchQuery("title", "Search engines").operator(AND)).build();
|
.withQuery(matchQuery("title", "Search engines").operator(AND)).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final SearchHits<Article> articles =
|
||||||
assertEquals(1, articles.size());
|
elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
assertEquals(1, articles.getTotalHits());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package com.baeldung.spring.data.es;
|
|||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
import static java.util.stream.Collectors.toList;
|
import static java.util.stream.Collectors.toList;
|
||||||
import static org.elasticsearch.index.query.Operator.AND;
|
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
||||||
@ -26,6 +25,7 @@ import org.elasticsearch.client.RequestOptions;
|
|||||||
import org.elasticsearch.client.RestHighLevelClient;
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
import org.elasticsearch.common.unit.Fuzziness;
|
import org.elasticsearch.common.unit.Fuzziness;
|
||||||
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
|
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
|
||||||
|
import org.elasticsearch.index.query.Operator;
|
||||||
import org.elasticsearch.index.query.QueryBuilder;
|
import org.elasticsearch.index.query.QueryBuilder;
|
||||||
import org.elasticsearch.search.aggregations.Aggregation;
|
import org.elasticsearch.search.aggregations.Aggregation;
|
||||||
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
||||||
@ -39,16 +39,17 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
||||||
|
import org.springframework.data.elasticsearch.core.SearchHits;
|
||||||
|
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||||
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
||||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||||
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* This Manual test requires: * Elasticsearch instance running on localhost:9200.
|
||||||
* This Manual test requires: * Elasticsearch instance running on host * with
|
* The following docker command can be used:
|
||||||
* cluster name = elasticsearch
|
* docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = Config.class)
|
@ContextConfiguration(classes = Config.class)
|
||||||
@ -68,10 +69,10 @@ public class ElasticSearchQueryManualTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() {
|
public void before() {
|
||||||
elasticsearchTemplate.deleteIndex(Article.class);
|
elasticsearchTemplate.indexOps(Article.class).delete();
|
||||||
elasticsearchTemplate.createIndex(Article.class);
|
elasticsearchTemplate.indexOps(Article.class).create();
|
||||||
elasticsearchTemplate.putMapping(Article.class);
|
elasticsearchTemplate.indexOps(Article.class).createMapping();
|
||||||
elasticsearchTemplate.refresh(Article.class);
|
elasticsearchTemplate.indexOps(Article.class).refresh();;
|
||||||
|
|
||||||
Article article = new Article("Spring Data Elasticsearch");
|
Article article = new Article("Spring Data Elasticsearch");
|
||||||
article.setAuthors(asList(johnSmith, johnDoe));
|
article.setAuthors(asList(johnSmith, johnDoe));
|
||||||
@ -96,40 +97,55 @@ public class ElasticSearchQueryManualTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
|
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
.withQuery(matchQuery("title", "Search engines").operator(AND)).build();
|
.withQuery(matchQuery("title", "Search engines").operator(Operator.AND)).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final SearchHits<Article> articles =
|
||||||
assertEquals(1, articles.size());
|
elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
assertEquals(1, articles.getTotalHits());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() {
|
public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions"))
|
final NativeSearchQuery searchQuery =
|
||||||
|
new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions"))
|
||||||
.build();
|
.build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
|
||||||
assertEquals(1, articles.size());
|
final SearchHits<Article> articles =
|
||||||
assertEquals("Search engines", articles.get(0).getTitle());
|
elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
|
||||||
|
assertEquals(1, articles.getTotalHits());
|
||||||
|
assertEquals("Search engines", articles.getSearchHit(0).getContent().getTitle());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
|
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data"))
|
final NativeSearchQuery searchQuery =
|
||||||
|
new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data"))
|
||||||
.build();
|
.build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
|
||||||
assertEquals(3, articles.size());
|
final SearchHits<Article> articles =
|
||||||
|
elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
|
||||||
|
assertEquals(3, articles.getTotalHits());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() {
|
public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() {
|
||||||
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
.withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build();
|
.withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch"))
|
||||||
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
.build();
|
||||||
assertEquals(1, articles.size());
|
|
||||||
|
|
||||||
searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About"))
|
SearchHits<Article> articles =
|
||||||
.build();
|
elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
|
||||||
assertEquals(0, articles.size());
|
assertEquals(1, articles.getTotalHits());
|
||||||
|
|
||||||
|
searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchQuery("title.verbatim", "Second Article About"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
assertEquals(0, articles.getTotalHits());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -137,10 +153,10 @@ public class ElasticSearchQueryManualTest {
|
|||||||
final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")),
|
final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")),
|
||||||
ScoreMode.None);
|
ScoreMode.None);
|
||||||
|
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
|
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
|
||||||
assertEquals(2, articles.size());
|
assertEquals(2, articles.getTotalHits());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -148,7 +164,7 @@ public class ElasticSearchQueryManualTest {
|
|||||||
final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title");
|
final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title");
|
||||||
|
|
||||||
final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
|
final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
|
||||||
final SearchRequest searchRequest = new SearchRequest("blog").types("article").source(builder);
|
final SearchRequest searchRequest = new SearchRequest("blog").source(builder);
|
||||||
|
|
||||||
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
@ -167,7 +183,7 @@ public class ElasticSearchQueryManualTest {
|
|||||||
.order(BucketOrder.count(false));
|
.order(BucketOrder.count(false));
|
||||||
|
|
||||||
final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
|
final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
|
||||||
final SearchRequest searchRequest = new SearchRequest().indices("blog").types("article").source(builder);
|
final SearchRequest searchRequest = new SearchRequest().indices("blog").source(builder);
|
||||||
|
|
||||||
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
@ -181,32 +197,36 @@ public class ElasticSearchQueryManualTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() {
|
public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
.withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build();
|
.withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
|
||||||
assertEquals(1, articles.size());
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
|
||||||
|
assertEquals(1, articles.getTotalHits());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
|
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
.withQuery(
|
.withQuery(
|
||||||
matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE).prefixLength(3))
|
matchQuery("title", "spring date elasticserch").operator(Operator.AND).fuzziness(Fuzziness.ONE).prefixLength(3))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
assertEquals(1, articles.size());
|
|
||||||
|
assertEquals(1, articles.getTotalHits());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
|
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
.withQuery(
|
.withQuery(
|
||||||
multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS))
|
multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
assertEquals(2, articles.size());
|
|
||||||
|
assertEquals(2, articles.getTotalHits());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -215,9 +235,9 @@ public class ElasticSearchQueryManualTest {
|
|||||||
.must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None))
|
.must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None))
|
||||||
.filter(termQuery("tags", "elasticsearch"));
|
.filter(termQuery("tags", "elasticsearch"));
|
||||||
|
|
||||||
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
|
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
|
||||||
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||||
|
|
||||||
assertEquals(2, articles.size());
|
assertEquals(2, articles.getTotalHits());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user