0

I am writing a query; in Kibana it's easy

GET populationstreamassignment/_search
{
    "query": {
    "match": {
      "healthyChildrenIndicator": true
    }
  }, 
  "_source": "memberId"
}

What I want to do is get a list of all the memberId's for 'healthy children'. But I want to translate this to java syntax.

import java.util.List;
import java.util.UUID;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 Spring Data Elasticsearch repository for the {@link PopulationStreamAssignment} entity.
 */
public interface PopulationStreamAssignmentSearchRepository extends ElasticsearchRepository<PopulationStreamAssignment, Long> {

    @Query("{\"match\": {\"?0\": \"?1\"}}")
    List<UUID> getMemberIdsByPopulationStream(String popStream, Boolean criteria);

}

This query has a few problems. Here is where I have questions..

  1. How can I specify "_source" so that I only return the memberId field?
  2. memberId is a UUID, can I have it directly return memberId's as a List of Values?

3 Answers 3

1

There is currently a pull request being worked on that will allow to add source includes and source excludes to repository methods that are annotated with the @Query annotation, so this will be available in the next version.

Besides that, it is possible to set the source filter values to any provided implementation of the Query interface (StringQuery, CriteriaQuery, NativeSearchQuery). You'd need to create a repository fragment (see https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#repositories.custom-implementations) to add a function that builds and uses one of these queries if you wish to integrate it in a repository.

The entity you use to read the data would need to have a property that matches the returned values like

@Document(indexName="populationstreamassignment")
public class Returned {
  @Id
  private String id;
  @Field(type= FieldType.text)
  private String memberId
  // getter and setter
}
Sign up to request clarification or add additional context in comments.

3 Comments

That custom repository implementation is a life saver. Thanks.
@SandeepKhantwal Can you help with some working example of custom repo
@Shruti - answer posted in this thread with code example
0

As mentioned in this GitHub issue, you can not directly filter source using annotation and you need to use NativeSearchQueryBuilder for same.

You can check this SO answer written by Val for NativeSearch query builder.

Comments

0

@Shruti - pls note the CustomRepoImpl will be automatically searched by spring given the package is in the search path of spring boot. The name of the Impl should be (interface-name)Impl

@Repository
public interface NormalRepo extends ElasticsearchRepository<User, String>, CustomRepo {

  Optional<Instant> findByName(String name);
}

public interface CustomRepo {
  Optional<Instant> findByName(String name);
}

public class CustomRepoImpl implements CustomRepo {
  Optional<Instant> findByName(String name) {
    // your implementation
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.