Skip to content

DATAES-317 - Introduce query logging in ElasticsearchTemplate. #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static org.elasticsearch.index.VersionType.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.springframework.data.elasticsearch.core.MappingBuilder.*;
import static org.springframework.util.CollectionUtils.isEmpty;
import static org.springframework.util.CollectionUtils.*;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -52,6 +52,7 @@
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequestBuilder;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
Expand Down Expand Up @@ -136,7 +137,8 @@
*/
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {

private static final Logger logger = LoggerFactory.getLogger(ElasticsearchTemplate.class);
private static final Logger QUERY_LOGGER = LoggerFactory.getLogger("org.springframework.data.elasticsearch.core.QUERY");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea, though I don't know if there are any conventions within Spring / Spring Data to look towards following for this type of logging? For example in the JPA project they have the following options for logging queries - http://www.baeldung.com/sql-logging-spring-boot

private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchTemplate.class);
private static final String FIELD_SCORE = "_score";

private Client client;
Expand Down Expand Up @@ -208,7 +210,7 @@ public <T> boolean putMapping(Class<T> clazz) {
return putMapping(clazz, mappings);
}
} else {
logger.info("mappingPath in @Mapping has to be defined. Building mappings using @Field");
LOGGER.info("mappingPath in @Mapping has to be defined. Building mappings using @Field");
}
}
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
Expand Down Expand Up @@ -339,7 +341,7 @@ public <T> List<String> queryForIds(SearchQuery query) {
if (query.getFilter() != null) {
request.setPostFilter(query.getFilter());
}
SearchResponse response = getSearchResponse(request.execute());
SearchResponse response = getSearchResponse(request);
return extractIds(response);
}

Expand All @@ -362,11 +364,8 @@ public <T> Page<T> queryForPage(CriteriaQuery criteriaQuery, Class<T> clazz) {

if (elasticsearchFilter != null)
searchRequestBuilder.setPostFilter(elasticsearchFilter);
if (logger.isDebugEnabled()) {
logger.debug("doSearch query:\n" + searchRequestBuilder.toString());
}

SearchResponse response = getSearchResponse(searchRequestBuilder.execute());
SearchResponse response = getSearchResponse(searchRequestBuilder);
return resultsMapper.mapResults(response, clazz, criteriaQuery.getPageable());
}

Expand All @@ -377,7 +376,7 @@ public <T> Page<T> queryForPage(StringQuery query, Class<T> clazz) {

@Override
public <T> Page<T> queryForPage(StringQuery query, Class<T> clazz, SearchResultMapper mapper) {
SearchResponse response = getSearchResponse(prepareSearch(query, clazz).setQuery(wrapperQuery(query.getSource())).execute());
SearchResponse response = getSearchResponse(prepareSearch(query, clazz).setQuery(wrapperQuery(query.getSource())));
return mapper.mapResults(response, clazz, query.getPageable());
}

Expand Down Expand Up @@ -793,7 +792,7 @@ private SearchResponse doScroll(SearchRequestBuilder requestBuilder, CriteriaQue
requestBuilder.setPostFilter(elasticsearchFilter);
}

return getSearchResponse(requestBuilder.execute());
return getSearchResponse(requestBuilder);
}

private SearchResponse doScroll(SearchRequestBuilder requestBuilder, SearchQuery searchQuery) {
Expand All @@ -805,7 +804,7 @@ private SearchResponse doScroll(SearchRequestBuilder requestBuilder, SearchQuery
requestBuilder.setPostFilter(searchQuery.getFilter());
}

return getSearchResponse(requestBuilder.setQuery(searchQuery.getQuery()).execute());
return getSearchResponse(requestBuilder.setQuery(searchQuery.getQuery()));
}

public <T> Page<T> startScroll(long scrollTimeInMillis, SearchQuery searchQuery, Class<T> clazz) {
Expand Down Expand Up @@ -931,7 +930,13 @@ private SearchResponse doSearch(SearchRequestBuilder searchRequest, SearchQuery
searchRequest.addAggregation(aggregatedFacet.getFacet());
}
}
return getSearchResponse(searchRequest.setQuery(searchQuery.getQuery()).execute());
return getSearchResponse(searchRequest.setQuery(searchQuery.getQuery()));
}

private SearchResponse getSearchResponse(SearchRequestBuilder requestBuilder) {
if (QUERY_LOGGER.isDebugEnabled())
QUERY_LOGGER.debug(requestBuilder.toString());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if Spring has any guidelines for this, but I found this old document recommending to include curly-braces around if statements - http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-142311.html

return getSearchResponse(requestBuilder.execute());
}

private SearchResponse getSearchResponse(ActionFuture<SearchResponse> response) {
Expand All @@ -951,7 +956,7 @@ private <T> boolean createIndexWithSettings(Class<T> clazz) {
return createIndex(getPersistentEntityFor(clazz).getIndexName(), settings);
}
} else {
logger.info("settingPath in @Setting has to be defined. Using default instead.");
LOGGER.info("settingPath in @Setting has to be defined. Using default instead.");
}
}
return createIndex(getPersistentEntityFor(clazz).getIndexName(), getDefaultSettings(getPersistentEntityFor(clazz)));
Expand Down Expand Up @@ -1254,14 +1259,14 @@ public static String readFileFromClasspath(String url) {
stringBuilder.append(line).append(lineSeparator);
}
} catch (Exception e) {
logger.debug(String.format("Failed to load file from url: %s: %s", url, e.getMessage()));
LOGGER.debug(String.format("Failed to load file from url: %s: %s", url, e.getMessage()));
return null;
} finally {
if (bufferedReader != null)
try {
bufferedReader.close();
} catch (IOException e) {
logger.debug(String.format("Unable to close buffered reader.. %s", e.getMessage()));
LOGGER.debug(String.format("Unable to close buffered reader.. %s", e.getMessage()));
}
}

Expand Down