org.springframework.data.elasticsearch.core.query.IndexQuery Java Examples

The following examples show how to use org.springframework.data.elasticsearch.core.query.IndexQuery. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NestedObjectTests.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void shouldIndexMultipleLevelNestedObject() {
    //given
    List<IndexQuery> indexQueries = createPerson();

    //when
    elasticsearchTemplate.putMapping(PersonMultipleLevelNested.class);
    elasticsearchTemplate.bulkIndex(indexQueries);
    elasticsearchTemplate.refresh(PersonMultipleLevelNested.class);

    //then
    GetQuery getQuery = new GetQuery();
    getQuery.setId("1");
    PersonMultipleLevelNested personIndexed =
        elasticsearchTemplate.queryForObject(getQuery, PersonMultipleLevelNested.class);
    assertThat(personIndexed, is(notNullValue()));
}
 
Example #2
Source File: NestedObjectTests.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void shouldSearchUsingNestedQueryOnMultipleLevelNestedObject() {
    //given
    List<IndexQuery> indexQueries = createPerson();

    //when
    elasticsearchTemplate.putMapping(PersonMultipleLevelNested.class);
    elasticsearchTemplate.bulkIndex(indexQueries);
    elasticsearchTemplate.refresh(PersonMultipleLevelNested.class);

    //then
    BoolQueryBuilder builder = boolQuery();
    builder.must(nestedQuery("girlFriends", termQuery("girlFriends.type", "temp"), ScoreMode.Total))
        .must(nestedQuery("girlFriends.cars", termQuery("girlFriends.cars.name", "Ford".toLowerCase()),
            ScoreMode.Total));

    SearchQuery searchQuery = new NativeSearchQueryBuilder()
        .withQuery(builder)
        .build();

    Page<PersonMultipleLevelNested> personIndexed =
        elasticsearchTemplate.queryForPage(searchQuery, PersonMultipleLevelNested.class);
    assertThat(personIndexed, is(notNullValue()));
    assertThat(personIndexed.getTotalElements(), is(1L));
    assertThat(personIndexed.getContent().get(0).getId(), is("1"));
}
 
Example #3
Source File: NestedObjectTests.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void shouldIndexMultipleLevelNestedObject() {
    //given
    List<IndexQuery> indexQueries = createPerson();

    //when
    elasticsearchTemplate.putMapping(PersonMultipleLevelNested.class);
    elasticsearchTemplate.bulkIndex(indexQueries);
    elasticsearchTemplate.refresh(PersonMultipleLevelNested.class);

    //then
    GetQuery getQuery = new GetQuery();
    getQuery.setId("1");
    PersonMultipleLevelNested personIndexed =
        elasticsearchTemplate.queryForObject(getQuery, PersonMultipleLevelNested.class);
    assertThat(personIndexed, is(notNullValue()));
}
 
Example #4
Source File: NestedObjectTests.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Test
public void shouldSearchUsingNestedQueryOnMultipleLevelNestedObject() {
    //given
    List<IndexQuery> indexQueries = createPerson();

    //when
    elasticsearchTemplate.putMapping(PersonMultipleLevelNested.class);
    elasticsearchTemplate.bulkIndex(indexQueries);
    elasticsearchTemplate.refresh(PersonMultipleLevelNested.class);

    //then
    BoolQueryBuilder builder = boolQuery();
    builder.must(nestedQuery("girlFriends", termQuery("girlFriends.type", "temp"), ScoreMode.Total))
        .must(nestedQuery("girlFriends.cars", termQuery("girlFriends.cars.name", "Ford".toLowerCase()),
            ScoreMode.Total));

    SearchQuery searchQuery = new NativeSearchQueryBuilder()
        .withQuery(builder)
        .build();

    Page<PersonMultipleLevelNested> personIndexed =
        elasticsearchTemplate.queryForPage(searchQuery, PersonMultipleLevelNested.class);
    assertThat(personIndexed, is(notNullValue()));
    assertThat(personIndexed.getTotalElements(), is(1L));
    assertThat(personIndexed.getContent().get(0).getId(), is("1"));
}
 
Example #5
Source File: ElasticsearchTransactionRepository.java    From servicecomb-pack with Apache License 2.0 5 votes vote down vote up
private IndexQuery convert(GlobalTransaction transaction) throws JsonProcessingException {
  IndexQuery indexQuery = new IndexQuery();
  indexQuery.setId(transaction.getGlobalTxId());
  indexQuery.setSource(mapper.writeValueAsString(transaction));
  indexQuery.setIndexName(INDEX_NAME);
  indexQuery.setType(INDEX_TYPE);
  return indexQuery;
}
 
Example #6
Source File: ArticleBuilder.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public IndexQuery buildIndex() {
    IndexQuery indexQuery = new IndexQuery();
    indexQuery.setId(result.getId());
    indexQuery.setObject(result);
    return indexQuery;
}
 
Example #7
Source File: ElasticsearchFacetTests.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
@Before
public void before() {
    elasticsearchTemplate.deleteIndex(Article.class);
    elasticsearchTemplate.createIndex(Article.class);
    elasticsearchTemplate.putMapping(Article.class);
    elasticsearchTemplate.refresh(Article.class);

    IndexQuery article1 = new ArticleBuilder("1").title("article four")
        .addAuthor(RIZWAN_IDREES)
        .addAuthor(ARTUR_KONCZAK)
        .addAuthor(MOHSIN_HUSEN)
        .addAuthor(JONATHAN_YAN)
        .score(10)
        .buildIndex();
    IndexQuery article2 = new ArticleBuilder("2").title("article three")
        .addAuthor(RIZWAN_IDREES)
        .addAuthor(ARTUR_KONCZAK)
        .addAuthor(MOHSIN_HUSEN)
        .addPublishedYear(YEAR_2000)
        .score(20)
        .buildIndex();
    IndexQuery article3 = new ArticleBuilder("3").title("article two")
        .addAuthor(RIZWAN_IDREES)
        .addAuthor(ARTUR_KONCZAK)
        .addPublishedYear(YEAR_2001)
        .addPublishedYear(YEAR_2000)
        .score(30)
        .buildIndex();
    IndexQuery article4 = new ArticleBuilder("4").title("article one")
        .addAuthor(RIZWAN_IDREES)
        .addPublishedYear(YEAR_2002)
        .addPublishedYear(YEAR_2001)
        .addPublishedYear(YEAR_2000)
        .score(40)
        .buildIndex();

    elasticsearchTemplate.index(article1);
    elasticsearchTemplate.index(article2);
    elasticsearchTemplate.index(article3);
    elasticsearchTemplate.index(article4);
    elasticsearchTemplate.refresh(Article.class);
}
 
Example #8
Source File: NestedObjectTests.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
@Test
public void shouldIndexInitialLevelNestedObject() {

    List<Car> cars = new ArrayList<Car>();

    Car saturn = new Car();
    saturn.setName("Saturn");
    saturn.setModel("SL");

    Car subaru = new Car();
    subaru.setName("Subaru");
    subaru.setModel("Imprezza");

    Car ford = new Car();
    ford.setName("Ford");
    ford.setModel("Focus");

    cars.add(saturn);
    cars.add(subaru);
    cars.add(ford);

    Person foo = new Person();
    foo.setName("Foo");
    foo.setId("1");
    foo.setCar(cars);

    Car car = new Car();
    car.setName("Saturn");
    car.setModel("Imprezza");

    Person bar = new Person();
    bar.setId("2");
    bar.setName("Bar");
    bar.setCar(Arrays.asList(car));

    List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
    IndexQuery indexQuery1 = new IndexQuery();
    indexQuery1.setId(foo.getId());
    indexQuery1.setObject(foo);

    IndexQuery indexQuery2 = new IndexQuery();
    indexQuery2.setId(bar.getId());
    indexQuery2.setObject(bar);

    indexQueries.add(indexQuery1);
    indexQueries.add(indexQuery2);

    elasticsearchTemplate.putMapping(Person.class);
    elasticsearchTemplate.bulkIndex(indexQueries);
    elasticsearchTemplate.refresh(Person.class);

    QueryBuilder builder = nestedQuery("car",
        boolQuery().must(termQuery("car.name", "saturn")).must(termQuery("car.model", "imprezza")),
        ScoreMode.Total);

    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
    List<Person> persons = elasticsearchTemplate.queryForList(searchQuery, Person.class);

    assertThat(persons.size(), is(1));
}
 
Example #9
Source File: NestedObjectTests.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
private List<IndexQuery> createPerson() {

        PersonMultipleLevelNested person1 = new PersonMultipleLevelNested();

        person1.setId("1");
        person1.setName("name");

        Car saturn = new Car();
        saturn.setName("Saturn");
        saturn.setModel("SL");

        Car subaru = new Car();
        subaru.setName("Subaru");
        subaru.setModel("Imprezza");

        Car car = new Car();
        car.setName("Saturn");
        car.setModel("Imprezza");

        Car ford = new Car();
        ford.setName("Ford");
        ford.setModel("Focus");

        GirlFriend permanent = new GirlFriend();
        permanent.setName("permanent");
        permanent.setType("permanent");
        permanent.setCars(Arrays.asList(saturn, subaru));

        GirlFriend temp = new GirlFriend();
        temp.setName("temp");
        temp.setType("temp");
        temp.setCars(Arrays.asList(car, ford));

        person1.setGirlFriends(Arrays.asList(permanent, temp));

        IndexQuery indexQuery1 = new IndexQuery();
        indexQuery1.setId(person1.getId());
        indexQuery1.setObject(person1);

        PersonMultipleLevelNested person2 = new PersonMultipleLevelNested();

        person2.setId("2");
        person2.setName("name");

        person2.setGirlFriends(Arrays.asList(permanent));

        IndexQuery indexQuery2 = new IndexQuery();
        indexQuery2.setId(person2.getId());
        indexQuery2.setObject(person2);

        List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
        indexQueries.add(indexQuery1);
        indexQueries.add(indexQuery2);

        return indexQueries;
    }
 
Example #10
Source File: ArticleBuilder.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public IndexQuery buildIndex() {
    IndexQuery indexQuery = new IndexQuery();
    indexQuery.setId(result.getId());
    indexQuery.setObject(result);
    return indexQuery;
}
 
Example #11
Source File: ElasticsearchFacetTests.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
@Before
public void before() {
    elasticsearchTemplate.deleteIndex(Article.class);
    elasticsearchTemplate.createIndex(Article.class);
    elasticsearchTemplate.putMapping(Article.class);
    elasticsearchTemplate.refresh(Article.class);

    IndexQuery article1 = new ArticleBuilder("1").title("article four")
        .addAuthor(RIZWAN_IDREES)
        .addAuthor(ARTUR_KONCZAK)
        .addAuthor(MOHSIN_HUSEN)
        .addAuthor(JONATHAN_YAN)
        .score(10)
        .buildIndex();
    IndexQuery article2 = new ArticleBuilder("2").title("article three")
        .addAuthor(RIZWAN_IDREES)
        .addAuthor(ARTUR_KONCZAK)
        .addAuthor(MOHSIN_HUSEN)
        .addPublishedYear(YEAR_2000)
        .score(20)
        .buildIndex();
    IndexQuery article3 = new ArticleBuilder("3").title("article two")
        .addAuthor(RIZWAN_IDREES)
        .addAuthor(ARTUR_KONCZAK)
        .addPublishedYear(YEAR_2001)
        .addPublishedYear(YEAR_2000)
        .score(30)
        .buildIndex();
    IndexQuery article4 = new ArticleBuilder("4").title("article one")
        .addAuthor(RIZWAN_IDREES)
        .addPublishedYear(YEAR_2002)
        .addPublishedYear(YEAR_2001)
        .addPublishedYear(YEAR_2000)
        .score(40)
        .buildIndex();

    elasticsearchTemplate.index(article1);
    elasticsearchTemplate.index(article2);
    elasticsearchTemplate.index(article3);
    elasticsearchTemplate.index(article4);
    elasticsearchTemplate.refresh(Article.class);
}
 
Example #12
Source File: NestedObjectTests.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
@Test
public void shouldIndexInitialLevelNestedObject() {

    List<Car> cars = new ArrayList<Car>();

    Car saturn = new Car();
    saturn.setName("Saturn");
    saturn.setModel("SL");

    Car subaru = new Car();
    subaru.setName("Subaru");
    subaru.setModel("Imprezza");

    Car ford = new Car();
    ford.setName("Ford");
    ford.setModel("Focus");

    cars.add(saturn);
    cars.add(subaru);
    cars.add(ford);

    Person foo = new Person();
    foo.setName("Foo");
    foo.setId("1");
    foo.setCar(cars);

    Car car = new Car();
    car.setName("Saturn");
    car.setModel("Imprezza");

    Person bar = new Person();
    bar.setId("2");
    bar.setName("Bar");
    bar.setCar(Arrays.asList(car));

    List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
    IndexQuery indexQuery1 = new IndexQuery();
    indexQuery1.setId(foo.getId());
    indexQuery1.setObject(foo);

    IndexQuery indexQuery2 = new IndexQuery();
    indexQuery2.setId(bar.getId());
    indexQuery2.setObject(bar);

    indexQueries.add(indexQuery1);
    indexQueries.add(indexQuery2);

    elasticsearchTemplate.putMapping(Person.class);
    elasticsearchTemplate.bulkIndex(indexQueries);
    elasticsearchTemplate.refresh(Person.class);

    QueryBuilder builder = nestedQuery("car",
        boolQuery().must(termQuery("car.name", "saturn")).must(termQuery("car.model", "imprezza")),
        ScoreMode.Total);

    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
    List<Person> persons = elasticsearchTemplate.queryForList(searchQuery, Person.class);

    assertThat(persons.size(), is(1));
}
 
Example #13
Source File: NestedObjectTests.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
private List<IndexQuery> createPerson() {

        PersonMultipleLevelNested person1 = new PersonMultipleLevelNested();

        person1.setId("1");
        person1.setName("name");

        Car saturn = new Car();
        saturn.setName("Saturn");
        saturn.setModel("SL");

        Car subaru = new Car();
        subaru.setName("Subaru");
        subaru.setModel("Imprezza");

        Car car = new Car();
        car.setName("Saturn");
        car.setModel("Imprezza");

        Car ford = new Car();
        ford.setName("Ford");
        ford.setModel("Focus");

        GirlFriend permanent = new GirlFriend();
        permanent.setName("permanent");
        permanent.setType("permanent");
        permanent.setCars(Arrays.asList(saturn, subaru));

        GirlFriend temp = new GirlFriend();
        temp.setName("temp");
        temp.setType("temp");
        temp.setCars(Arrays.asList(car, ford));

        person1.setGirlFriends(Arrays.asList(permanent, temp));

        IndexQuery indexQuery1 = new IndexQuery();
        indexQuery1.setId(person1.getId());
        indexQuery1.setObject(person1);

        PersonMultipleLevelNested person2 = new PersonMultipleLevelNested();

        person2.setId("2");
        person2.setName("name");

        person2.setGirlFriends(Arrays.asList(permanent));

        IndexQuery indexQuery2 = new IndexQuery();
        indexQuery2.setId(person2.getId());
        indexQuery2.setObject(person2);

        List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
        indexQueries.add(indexQuery1);
        indexQueries.add(indexQuery2);

        return indexQueries;
    }