-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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"); | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchTemplate.class); | ||
private static final String FIELD_SCORE = "_score"; | ||
|
||
private Client client; | ||
|
@@ -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); | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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()); | ||
} | ||
|
||
|
@@ -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()); | ||
} | ||
|
||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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))); | ||
|
@@ -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())); | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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