com.hp.autonomy.searchcomponents.idol.search.QueryResponseParserImpl Maven / Gradle / Ivy
/*
* Copyright 2015 Hewlett-Packard Development Company, L.P.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
*/
package com.hp.autonomy.searchcomponents.idol.search;
import com.autonomy.aci.client.services.AciErrorException;
import com.autonomy.aci.client.util.AciParameters;
import com.hp.autonomy.aci.content.database.Databases;
import com.hp.autonomy.searchcomponents.core.search.AutoCorrectException;
import com.hp.autonomy.searchcomponents.idol.databases.IdolDatabasesRequestBuilder;
import com.hp.autonomy.searchcomponents.idol.databases.IdolDatabasesService;
import com.hp.autonomy.searchcomponents.idol.search.fields.FieldsParser;
import com.hp.autonomy.types.idol.responses.Database;
import com.hp.autonomy.types.idol.responses.Hit;
import com.hp.autonomy.types.idol.responses.QueryResponseData;
import com.hp.autonomy.types.requests.Documents;
import com.hp.autonomy.types.requests.Spelling;
import com.hp.autonomy.types.requests.Warnings;
import com.hp.autonomy.types.requests.idol.actions.query.params.QueryParams;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Pattern;
import static com.hp.autonomy.searchcomponents.idol.search.QueryResponseParser.QUERY_RESPONSE_PARSER_BEAN_NAME;
/**
* Default implementation of {@link QueryResponseParser}
*/
@SuppressWarnings("WeakerAccess")
@Component(QUERY_RESPONSE_PARSER_BEAN_NAME)
class QueryResponseParserImpl implements QueryResponseParser {
static final String MISSING_DATABASE_WARNING = "At least one of the databases provided in the query does not exist";
private static final Pattern SPELLING_SEPARATOR_PATTERN = Pattern.compile(", ");
private final FieldsParser fieldsParser;
private final IdolDatabasesService databasesService;
private final ObjectFactory databasesRequestBuilderFactory;
@Autowired
QueryResponseParserImpl(final FieldsParser fieldsParser,
final IdolDatabasesService databasesService,
final ObjectFactory databasesRequestBuilderFactory) {
this.fieldsParser = fieldsParser;
this.databasesService = databasesService;
this.databasesRequestBuilderFactory = databasesRequestBuilderFactory;
}
@Override
public Documents parseQueryResults(final IdolSearchRequest searchRequest, final AciParameters aciParameters, final QueryResponseData responseData, final Function queryExecutor) {
final List hits = responseData.getHits();
final Warnings warnings = parseWarnings(searchRequest, aciParameters, responseData);
final String spellingQuery = responseData.getSpellingquery();
// If IDOL has a spelling suggestion, retry query for auto correct
final Documents documents;
if (spellingQuery != null) {
documents = rerunQueryWithAdjustedSpelling(aciParameters, responseData, spellingQuery, warnings, queryExecutor);
} else {
final List results = parseQueryHits(hits);
documents = new Documents<>(results, responseData.getTotalhits(), responseData.getExpandedQuery(), null, null, warnings);
}
return documents;
}
protected Warnings parseWarnings(final IdolSearchRequest searchRequest, final AciParameters aciParameters, final QueryResponseData responseData) {
Warnings warnings = null;
for (final String warning : responseData.getWarning()) {
if (MISSING_DATABASE_WARNING.equals(warning.trim())) {
final Set updatedDatabases = databasesService.getDatabases(databasesRequestBuilderFactory.getObject().build());
final List oldQueryRestrictionDatabases = searchRequest.getQueryRestrictions().getDatabases();
final Set badDatabases = new LinkedHashSet<>(oldQueryRestrictionDatabases);
for (final Database database : updatedDatabases) {
final String databaseName = database.getName();
badDatabases.remove(databaseName);
}
warnings = new Warnings(badDatabases);
final Set newQueryRestrictionDatabases = new LinkedHashSet<>(oldQueryRestrictionDatabases);
newQueryRestrictionDatabases.removeAll(badDatabases);
aciParameters.add(QueryParams.DatabaseMatch.name(), new Databases(newQueryRestrictionDatabases));
}
}
return warnings;
}
protected Documents rerunQueryWithAdjustedSpelling(final AciParameters aciParameters, final QueryResponseData responseData, final String spellingQuery, final Warnings warnings, final Function queryExecutor) {
final String originalQuery = aciParameters.get(QueryParams.Text.name());
aciParameters.put(QueryParams.Text.name(), spellingQuery);
final Spelling spelling = new Spelling(Arrays.asList(SPELLING_SEPARATOR_PATTERN.split(responseData.getSpelling())), spellingQuery, originalQuery);
try {
final QueryResponseData correctedResponseData = queryExecutor.apply(aciParameters);
final List correctedResults = parseQueryHits(correctedResponseData.getHits());
return new Documents<>(correctedResults, correctedResponseData.getTotalhits(), null, null, spelling, warnings);
} catch (final AciErrorException e) {
throw new AutoCorrectException(e.getMessage(), e, spelling);
}
}
@Override
public List parseQueryHits(final Collection hits) {
final List results = new ArrayList<>(hits.size());
for (final Hit hit : hits) {
final IdolSearchResult.IdolSearchResultBuilder searchResultBuilder = new IdolSearchResult.IdolSearchResultBuilder()
.reference(hit.getReference())
.index(hit.getDatabase())
.title(hit.getTitle())
.summary(hit.getSummary())
.date(hit.getDate() != null ? new DateTime(hit.getDatestring().toInstant().toEpochMilli(), DateTimeZone.UTC) : null)
.weight(hit.getWeight())
.promotionName(hit.getPromotionname());
fieldsParser.parseDocumentFields(hit, searchResultBuilder);
results.add(searchResultBuilder.build());
}
return results;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy