Skip to content

Commit

Permalink
HSEARCH-2253 Remove ID sort workaround using "_uid" in Elasticsearch
Browse files Browse the repository at this point in the history
This follows HSEARCH-2396 where we added an in-document indexed field
for the ID.
  • Loading branch information
yrodiere committed Nov 4, 2016
1 parent 156c803 commit cbe79dd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 22 deletions.
Expand Up @@ -291,12 +291,10 @@ private class IndexSearcher {

private final Map<String, Class<?>> entityTypesByName = new HashMap<>();
private final Set<String> indexNames = new HashSet<>();
private final String idFieldName;
private final String completeQueryAsString;

private IndexSearcher() {
JsonArray typeFilters = new JsonArray();
String queriedEntityIdFieldName = null;
Iterable<Class<?>> queriedEntityTypes = getQueriedEntityTypes();

for ( Class<?> queriedEntityType : queriedEntityTypes ) {
Expand All @@ -313,20 +311,13 @@ private IndexSearcher() {
);
}

// TODO HSEARCH-2253 the id field name is used to detect if we should sort using the internal Elasticsearch id field
// as it's currently the only field in which we store the id of the entity.
// Thus the id fields must be consistent accross all the entity types when querying multiple ones.
queriedEntityIdFieldName = binding.getDocumentBuilder().getIdentifierName();

ElasticsearchIndexManager esIndexManager = (ElasticsearchIndexManager) indexManager;
indexNames.add( esIndexManager.getActualIndexName() );
}

typeFilters.add( getEntityTypeFilter( queriedEntityType ) );
}

this.idFieldName = queriedEntityIdFieldName;

// Query filters; always a type filter, possibly a tenant id filter;
JsonObject filteredQuery = getFilteredQuery( jsonQuery.get( "query" ), typeFilters );

Expand Down Expand Up @@ -430,7 +421,7 @@ private Iterable<Class<?>> getQueriedEntityTypes() {
}
}

private Sort getSort(SortField sortField, String idFieldName) {
private Sort getSort(SortField sortField) {
if ( sortField instanceof DistanceSortField ) {
DistanceSortField distanceSortField = (DistanceSortField) sortField;
return new DistanceSort( distanceSortField.getField(),
Expand Down Expand Up @@ -459,17 +450,7 @@ else if ( sortField instanceof NativeSortField ) {
}
}
else {
if ( sortField.getField().equals( idFieldName ) ) {
// It is not possible to sort on the _id field as this field is not indexed by Elasticsearch and there
// is no way to make it indexed in recent Elasticsearch versions.
// Thus, we use _uid which is stored as type#id even if it is not very satisfactory when
// we query multiple types.
// It is not recommended to sort by the @DocumentId field anyway.
sortFieldName = "_uid";
}
else {
sortFieldName = sortField.getField();
}
sortFieldName = sortField.getField();
}
return new Sort( sortFieldName, sortField.getReverse() ? Sorting.DESC : Sorting.ASC );
}
Expand Down Expand Up @@ -565,7 +546,7 @@ private SearchResult search(boolean enableScrolling) {
if ( sort != null ) {
validateSortFields( extendedIntegrator, getQueriedEntityTypes() );
for ( SortField sortField : sort.getSort() ) {
builder.addSort( getSort( sortField, idFieldName ) );
builder.addSort( getSort( sortField ) );
}
}
Search search = builder.build();
Expand Down
Expand Up @@ -36,6 +36,7 @@
import org.hibernate.search.elasticsearch.test.model.Tower;
import org.hibernate.search.query.engine.spi.QueryDescriptor;
import org.hibernate.search.test.SearchTestBase;
import org.hibernate.search.testsupport.TestForIssue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -435,6 +436,7 @@ public void testScroll() throws Exception {
}

@Test
@TestForIssue(jiraKey = "HSEARCH-2253")
public void testSort() throws Exception {
Session s = openSession();
FullTextSession session = Search.getFullTextSession( s );
Expand Down

0 comments on commit cbe79dd

Please sign in to comment.