DATAES-171 - added support for missing query keywords
This commit is contained in:
@@ -18,12 +18,21 @@ package org.springframework.data.elasticsearch.entities;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.*;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
|
||||
/**
|
||||
* @author Mohsin Husen
|
||||
* @author Artur Konczak
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
@Document(indexName = "test-product-index", type = "test-product-type", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class Product {
|
||||
|
||||
@@ -42,6 +51,7 @@ public class Product {
|
||||
|
||||
private Float weight;
|
||||
|
||||
@Field(type = FieldType.Float)
|
||||
private Float price;
|
||||
|
||||
private Integer popularity;
|
||||
@@ -52,93 +62,6 @@ public class Product {
|
||||
|
||||
private Date lastModified;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<String> getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(List<String> title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public List<String> getCategories() {
|
||||
return categories;
|
||||
}
|
||||
|
||||
public void setCategories(List<String> categories) {
|
||||
this.categories = categories;
|
||||
}
|
||||
|
||||
public Float getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(Float weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public Float getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Float price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Integer getPopularity() {
|
||||
return popularity;
|
||||
}
|
||||
|
||||
public void setPopularity(Integer popularity) {
|
||||
this.popularity = popularity;
|
||||
}
|
||||
|
||||
public boolean isAvailable() {
|
||||
return available;
|
||||
}
|
||||
|
||||
public void setAvailable(boolean available) {
|
||||
this.available = available;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
public Date getLastModified() {
|
||||
return lastModified;
|
||||
}
|
||||
|
||||
public void setLastModified(Date lastModified) {
|
||||
this.lastModified = lastModified;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
@@ -166,12 +89,4 @@ public class Product {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ public class CustomMethodRepositoryTests {
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setId(documentId);
|
||||
sampleEntity.setType("test");
|
||||
sampleEntity.setRate(10);
|
||||
sampleEntity.setRate(9);
|
||||
sampleEntity.setMessage("some message");
|
||||
repository.save(sampleEntity);
|
||||
|
||||
@@ -753,7 +753,7 @@ public class CustomMethodRepositoryTests {
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setId(documentId);
|
||||
sampleEntity.setType("test");
|
||||
sampleEntity.setRate(10);
|
||||
sampleEntity.setRate(9);
|
||||
sampleEntity.setMessage("some message");
|
||||
repository.save(sampleEntity);
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.springframework.data.elasticsearch.repositories.query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.elasticsearch.entities.Product;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
/**
|
||||
* Created by akonczak on 04/09/15.
|
||||
*/
|
||||
public interface ProductRepository extends PagingAndSortingRepository<Product, String> {
|
||||
|
||||
public List<Product> findByNameAndText(String name, String text);
|
||||
|
||||
public List<Product> findByNameAndPrice(String name, Float price);
|
||||
|
||||
public List<Product> findByNameOrText(String name, String text);
|
||||
|
||||
public List<Product> findByNameOrPrice(String name, Float price);
|
||||
|
||||
|
||||
public List<Product> findByAvailableTrue();
|
||||
|
||||
public List<Product> findByAvailableFalse();
|
||||
|
||||
public List<Product> findByPriceIn(List<Float> floats);
|
||||
|
||||
public List<Product> findByPriceNotIn(List<Float> floats);
|
||||
|
||||
public List<Product> findByPriceNot(float v);
|
||||
|
||||
public List<Product> findByPriceBetween(float v, float v1);
|
||||
|
||||
public List<Product> findByPriceLessThan(float v);
|
||||
|
||||
public List<Product> findByPriceLessThanEqual(float v);
|
||||
|
||||
public List<Product> findByPriceGreaterThan(float v);
|
||||
|
||||
public List<Product> findByPriceGreaterThanEqual(float v);
|
||||
|
||||
public List<Product> findByIdNotIn(List<String> strings);
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package org.springframework.data.elasticsearch.repository.support;
|
||||
|
||||
import static org.hamcrest.core.Is.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.entities.Product;
|
||||
import org.springframework.data.elasticsearch.repositories.query.ProductRepository;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Artur Konczak
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:/repository-query-support.xml")
|
||||
public class QueryKeywordsTest {
|
||||
|
||||
@Autowired
|
||||
private ProductRepository repository;
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
elasticsearchTemplate.deleteIndex(Product.class);
|
||||
elasticsearchTemplate.createIndex(Product.class);
|
||||
elasticsearchTemplate.putMapping(Product.class);
|
||||
elasticsearchTemplate.refresh(Product.class, true);
|
||||
|
||||
repository.save(Arrays.asList(
|
||||
Product.builder().id("1").name("Sugar").text("Cane sugar").price(1.0f).available(false).build()
|
||||
, Product.builder().id("2").name("Sugar").text("Cane sugar").price(1.2f).available(true).build()
|
||||
, Product.builder().id("3").name("Sugar").text("Beet sugar").price(1.1f).available(true).build()
|
||||
, Product.builder().id("4").name("Salt").text("Rock salt").price(1.9f).available(true).build()
|
||||
, Product.builder().id("5").name("Salt").text("Sea salt").price(2.1f).available(false).build()));
|
||||
|
||||
elasticsearchTemplate.refresh(Product.class, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSupportAND() {
|
||||
//given
|
||||
|
||||
//when
|
||||
|
||||
//then
|
||||
assertThat(repository.findByNameAndText("Sugar", "Cane sugar").size(), is(2));
|
||||
assertThat(repository.findByNameAndPrice("Sugar", 1.1f).size(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSupportOR() {
|
||||
//given
|
||||
|
||||
//when
|
||||
|
||||
//then
|
||||
assertThat(repository.findByNameOrPrice("Sugar", 1.9f).size(), is(4));
|
||||
assertThat(repository.findByNameOrText("Salt", "Beet sugar").size(), is(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSupportTrueAndFalse() {
|
||||
//given
|
||||
|
||||
//when
|
||||
|
||||
//then
|
||||
assertThat(repository.findByAvailableTrue().size(), is(3));
|
||||
assertThat(repository.findByAvailableFalse().size(), is(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSupportInAndNotInAndNot() {
|
||||
//given
|
||||
|
||||
//when
|
||||
|
||||
//then
|
||||
assertThat(repository.findByPriceIn(Arrays.asList(1.2f, 1.1f)).size(), is(2));
|
||||
assertThat(repository.findByPriceNotIn(Arrays.asList(1.2f, 1.1f)).size(), is(3));
|
||||
assertThat(repository.findByPriceNot(1.2f).size(), is(4));
|
||||
}
|
||||
|
||||
/*
|
||||
DATAES-171
|
||||
*/
|
||||
@Test
|
||||
public void shouldWorkWithNotIn() {
|
||||
//given
|
||||
|
||||
//when
|
||||
|
||||
//then
|
||||
assertThat(repository.findByIdNotIn(Arrays.asList("2", "3")).size(), is(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSupportBetween() {
|
||||
//given
|
||||
|
||||
//when
|
||||
|
||||
//then
|
||||
assertThat(repository.findByPriceBetween(1.0f, 2.0f).size(), is(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSupportLessThanAndGreaterThan() {
|
||||
//given
|
||||
|
||||
//when
|
||||
|
||||
//then
|
||||
assertThat(repository.findByPriceLessThan(1.1f).size(), is(1));
|
||||
assertThat(repository.findByPriceLessThanEqual(1.1f).size(), is(2));
|
||||
|
||||
assertThat(repository.findByPriceGreaterThan(1.9f).size(), is(1));
|
||||
assertThat(repository.findByPriceGreaterThanEqual(1.9f).size(), is(2));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user