Skip to content

DATAES-149 : add ES query Explain support #93

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
Show file tree
Hide file tree
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 @@ -25,6 +25,7 @@
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
Expand Down Expand Up @@ -85,6 +86,7 @@ public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pa
result = mapEntity(hit.getFields().values(), clazz);
}
setPersistentEntityId(result, hit.getId(), clazz);
mapExplanation(result, hit);
results.add(result);
}
}
Expand Down Expand Up @@ -167,4 +169,7 @@ private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
}
}
}

public <T> void mapExplanation(T result, SearchHit hit) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.suggest.SuggestRequest;
import org.elasticsearch.action.suggest.SuggestRequestBuilder;
import org.elasticsearch.action.suggest.SuggestResponse;
import org.elasticsearch.action.suggest.SuggestRequest;
import org.elasticsearch.action.suggest.SuggestRequestBuilder;
import org.elasticsearch.action.suggest.SuggestResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
Expand All @@ -63,7 +63,7 @@
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.QueryBuilder;
Expand All @@ -74,7 +74,7 @@
import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
Expand Down Expand Up @@ -779,6 +779,9 @@ private SearchRequestBuilder prepareSearch(Query query) {
if (query.getMinScore() > 0) {
searchRequestBuilder.setMinScore(query.getMinScore());
}

searchRequestBuilder.setExplain(query.isExplain());

return searchRequestBuilder;
}

Expand Down Expand Up @@ -981,14 +984,14 @@ public static String readFileFromClasspath(String url) {

return stringBuilder.toString();
}

public SuggestResponse suggest(SuggestBuilder.SuggestionBuilder<?> suggestion, String... indices) {
SuggestRequestBuilder suggestRequestBuilder = client.prepareSuggest(indices);
suggestRequestBuilder.addSuggestion(suggestion);
return suggestRequestBuilder.execute().actionGet();
}

public SuggestResponse suggest(SuggestBuilder.SuggestionBuilder<?> suggestion, Class clazz) {
return suggest(suggestion, retrieveIndexNameFromPersistentEntity(clazz));
}
public SuggestResponse suggest(SuggestBuilder.SuggestionBuilder<?> suggestion, String... indices) {
SuggestRequestBuilder suggestRequestBuilder = client.prepareSuggest(indices);
suggestRequestBuilder.addSuggestion(suggestion);
return suggestRequestBuilder.execute().actionGet();
}
public SuggestResponse suggest(SuggestBuilder.SuggestionBuilder<?> suggestion, Class clazz) {
return suggest(suggestion, retrieveIndexNameFromPersistentEntity(clazz));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ abstract class AbstractQuery implements Query {
protected List<String> types = new ArrayList<String>();
protected List<String> fields = new ArrayList<String>();
protected float minScore;
protected boolean explain;
protected Collection<String> ids;
protected String route;
protected SearchType searchType = SearchType.DFS_QUERY_THEN_FETCH;
Expand Down Expand Up @@ -81,6 +82,15 @@ public void addIndices(String... indices) {
addAll(this.indices, indices);
}

@Override
public boolean isExplain() {
return explain;
}

public void setExplain(boolean explain) {
this.explain = explain;
}

@Override
public void addTypes(String... types) {
addAll(this.types, types);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ public class NativeSearchQueryBuilder {
private String route;
private SearchType searchType;

public NativeSearchQueryBuilder withQuery(QueryBuilder queryBuilder) {
private boolean withExplanation;

public NativeSearchQueryBuilder withQuery(QueryBuilder queryBuilder) {
this.queryBuilder = queryBuilder;
return this;
}

public NativeSearchQueryBuilder withExplanation(boolean withExplanation) {
this.withExplanation = withExplanation;
return this;
}

public NativeSearchQueryBuilder withFilter(FilterBuilder filterBuilder) {
this.filterBuilder = filterBuilder;
return this;
Expand Down Expand Up @@ -166,6 +173,9 @@ public NativeSearchQuery build() {
nativeSearchQuery.setSearchType(searchType);
}

if (withExplanation)
nativeSearchQuery.setExplain(withExplanation);

return nativeSearchQuery;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,7 @@ public interface Query {
* @return
*/
SearchType getSearchType();


boolean isExplain();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.springframework.data.elasticsearch.core;

import org.elasticsearch.search.SearchHit;
import org.springframework.data.elasticsearch.entities.SampleExplanableEntity;

/**
* Created with IntelliJ IDEA.
* User: ghiron
* Date: 1/28/15
* Time: 4:35 PM
* To change this template use File | Settings | File Templates.
*/
public class ExplanationResultMapper extends DefaultResultMapper {
@Override
public <T> void mapExplanation(T result, SearchHit hit) {
if (result instanceof SampleExplanableEntity){
((SampleExplanableEntity)result).setExplanation(hit.getExplanation().getDescription());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.core.query;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.entities.SampleExplanableEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

/**
* @author Rizwan Idrees
* @author Mohsin Husen
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:elasticsearch-explanation-test.xml")
public class ExplanationQueryTests {


@Autowired
private ElasticsearchTemplate elasticsearchTemplate ;

@Before
public void before() {
elasticsearchTemplate.deleteIndex(SampleExplanableEntity.class);
elasticsearchTemplate.createIndex(SampleExplanableEntity.class);
elasticsearchTemplate.refresh(SampleExplanableEntity.class, true);
}

@Test
public void shouldPerformAndOperation() {

// given
String documentId = randomNumeric(5);
SampleExplanableEntity sampleEntity = new SampleExplanableEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some test message");
sampleEntity.setVersion(System.currentTimeMillis());

IndexQuery indexQuery = new IndexQuery();
indexQuery.setId(documentId);
indexQuery.setObject(sampleEntity);
elasticsearchTemplate.index(indexQuery);
elasticsearchTemplate.refresh(SampleExplanableEntity.class, true);

CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria("message").contains("test").and("message")
.contains("some"));
criteriaQuery.setExplain(true);

// when
SampleExplanableEntity sampleEntity1 = elasticsearchTemplate.queryForObject(criteriaQuery, SampleExplanableEntity.class);

// then
assertThat(sampleEntity1.getExplanation(), is(notNullValue()));
}

}
Loading