Search in sources :

Example 1 with SourceFetchContext

use of org.elasticsearch.mapping.SourceFetchContext in project alien4cloud by alien4cloud.

the class ESGenericSearchDAO method findByIdsWithContext.

@Override
public <T> List<T> findByIdsWithContext(Class<T> clazz, String fetchContext, String... ids) {
    // get the fetch context for the given type and apply it to the search
    List<String> includes = new ArrayList<String>();
    List<String> excludes = new ArrayList<String>();
    SourceFetchContext sourceFetchContext = getMappingBuilder().getFetchSource(clazz.getName(), fetchContext);
    if (sourceFetchContext != null) {
        includes.addAll(sourceFetchContext.getIncludes());
        excludes.addAll(sourceFetchContext.getExcludes());
    } else {
        getLog().warn("Unable to find fetch context <" + fetchContext + "> for class <" + clazz.getName() + ">. It will be ignored.");
    }
    String[] inc = includes.isEmpty() ? null : includes.toArray(new String[includes.size()]);
    String[] exc = excludes.isEmpty() ? null : excludes.toArray(new String[excludes.size()]);
    // TODO: correctly manage "from" and "size"
    SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(getIndexForType(clazz)).setQuery(QueryBuilders.idsQuery(MappingBuilder.indexTypeFromClass(clazz)).ids(ids)).setFetchSource(inc, exc).setSize(20);
    SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
    return toGetListOfData(searchResponse, clazz);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) ArrayList(java.util.ArrayList) SourceFetchContext(org.elasticsearch.mapping.SourceFetchContext) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

ArrayList (java.util.ArrayList)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 SourceFetchContext (org.elasticsearch.mapping.SourceFetchContext)1