org.vertexium.elasticsearch.ElasticSearchParentChildGraphQuery Maven / Gradle / Ivy
package org.vertexium.elasticsearch;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.*;
import org.vertexium.Authorizations;
import org.vertexium.Graph;
import org.vertexium.PropertyDefinition;
import org.vertexium.VertexiumException;
import org.vertexium.elasticsearch.score.ScoringStrategy;
import org.vertexium.query.QueryParameters;
import org.vertexium.query.QueryStringQueryParameters;
import org.vertexium.query.SimilarToTextQueryParameters;
import java.util.List;
import java.util.Map;
public class ElasticSearchParentChildGraphQuery extends ElasticSearchGraphQueryBase {
protected ElasticSearchParentChildGraphQuery(TransportClient client, String[] indicesToQuery, Graph graph, String queryString, Map propertyDefinitions, ScoringStrategy scoringStrategy, Authorizations authorizations) {
super(client, indicesToQuery, graph, queryString, propertyDefinitions, scoringStrategy, false, authorizations);
}
protected ElasticSearchParentChildGraphQuery(TransportClient client, String[] indicesToQuery, Graph graph, String[] similarToFields, String similarToText, Map propertyDefinitions, ScoringStrategy scoringStrategy, Authorizations authorizations) {
super(client, indicesToQuery, graph, similarToFields, similarToText, propertyDefinitions, scoringStrategy, false, authorizations);
}
@Override
protected QueryBuilder createQuery(QueryParameters queryParameters, String elementType, List filters) {
FilterBuilder elementTypeFilter = createElementTypeFilter(elementType);
AndFilterBuilder andFilterBuilder = FilterBuilders.andFilter(
elementTypeFilter,
new AuthorizationFilterBuilder(getParameters().getAuthorizations().getAuthorizations())
);
AuthorizationFilterBuilder authorizationFilterBuilder = new AuthorizationFilterBuilder(getParameters().getAuthorizations().getAuthorizations());
QueryBuilder childQuery;
if (queryParameters instanceof QueryStringQueryParameters) {
childQuery = createQueryStringQuery((QueryStringQueryParameters) queryParameters, elementType, filters, authorizationFilterBuilder);
} else if (queryParameters instanceof SimilarToTextQueryParameters) {
childQuery = createSimilarToTextQuery((SimilarToTextQueryParameters) queryParameters, elementType, filters, authorizationFilterBuilder);
} else {
throw new VertexiumException("Query parameters not supported of type: " + queryParameters.getClass().getName());
}
return QueryBuilders.filteredQuery(childQuery, andFilterBuilder);
}
private QueryBuilder createSimilarToTextQuery(SimilarToTextQueryParameters queryParameters, String elementType, List filters, AuthorizationFilterBuilder authorizationFilterBuilder) {
BoolQueryBuilder boolChildQuery = QueryBuilders.boolQuery();
boolChildQuery.must(
new HasChildQueryBuilder(ElasticSearchParentChildSearchIndex.PROPERTY_TYPE,
QueryBuilders.filteredQuery(
super.createQuery(queryParameters, elementType, filters),
authorizationFilterBuilder
)
).scoreType("avg")
);
addFiltersToQuery(boolChildQuery, filters, authorizationFilterBuilder);
return boolChildQuery;
}
private QueryBuilder createQueryStringQuery(QueryStringQueryParameters queryParameters, String elementType, List filters, AuthorizationFilterBuilder authorizationFilterBuilder) {
String queryString = queryParameters.getQueryString();
if (((queryString == null || queryString.length() <= 0)) && (filters.size() <= 0)) {
return QueryBuilders.matchAllQuery();
}
BoolQueryBuilder boolChildQuery = QueryBuilders.boolQuery();
if (queryString != null && queryString.length() > 0) {
boolChildQuery.must(
new HasChildQueryBuilder(ElasticSearchParentChildSearchIndex.PROPERTY_TYPE,
QueryBuilders.filteredQuery(
super.createQuery(queryParameters, elementType, filters),
authorizationFilterBuilder
)
).scoreType("avg")
);
}
addFiltersToQuery(boolChildQuery, filters, authorizationFilterBuilder);
return boolChildQuery;
}
private void addFiltersToQuery(BoolQueryBuilder boolChildQuery, List filters, AuthorizationFilterBuilder authorizationFilterBuilder) {
for (FilterBuilder filterBuilder : filters) {
boolChildQuery.must(
new HasChildQueryBuilder(ElasticSearchParentChildSearchIndex.PROPERTY_TYPE,
QueryBuilders.filteredQuery(
QueryBuilders.matchAllQuery(),
FilterBuilders.andFilter(authorizationFilterBuilder, filterBuilder)
)
).scoreType("avg")
);
}
}
@Override
protected void addElementTypeFilter(List filters, String elementType) {
// don't add the element type filter here because the child docs don't have element type only the parent type does
}
@Override
protected SearchRequestBuilder getSearchRequestBuilder(List filters, QueryBuilder queryBuilder) {
return getClient()
.prepareSearch(getIndicesToQuery())
.setTypes(ElasticSearchSearchIndexBase.ELEMENT_TYPE)
.setQuery(queryBuilder)
.setFrom((int) getParameters().getSkip())
.setSize((int) getParameters().getLimit());
}
@Override
protected void addNotFilter(List filters, String key, Object value) {
filters.add(
FilterBuilders.andFilter(
FilterBuilders.existsFilter(key),
FilterBuilders.notFilter(FilterBuilders.inFilter(key, value))
)
);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy