Search in sources :

Example 1 with SuggesterResponse

use of org.apache.solr.client.solrj.response.SuggesterResponse in project tutorials by eugenp.

the class ItemSearchServiceLiveTest method whenSearchingWithIncompleteKeyword_thenKeywordSuggestionsShouldBeReturned.

@Test
public void whenSearchingWithIncompleteKeyword_thenKeywordSuggestionsShouldBeReturned() throws Exception {
    itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f);
    itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f);
    itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "Home Appliances", 200f);
    itemSearchService.index("hm0004", "Brand2 Dishwasher", "Home washing equipments", 250f);
    SolrQuery query = new SolrQuery();
    query.setRequestHandler("/suggest");
    query.set("suggest", "true");
    query.set("suggest.build", "true");
    query.set("suggest.dictionary", "mySuggester");
    query.set("suggest.q", "Hom");
    QueryResponse response = solrClient.query(query);
    SuggesterResponse suggesterResponse = response.getSuggesterResponse();
    Map<String, List<String>> suggestedTerms = suggesterResponse.getSuggestedTerms();
    List<String> suggestions = suggestedTerms.get("mySuggester");
    assertEquals(2, suggestions.size());
    for (String term : suggestions) {
        if (!"Home Appliances".equals(term) && !"Home washing equipments".equals(term)) {
            fail("Unexpected suggestions");
        }
    }
}
Also used : QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SuggesterResponse(org.apache.solr.client.solrj.response.SuggesterResponse) List(java.util.List) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 2 with SuggesterResponse

use of org.apache.solr.client.solrj.response.SuggesterResponse in project ddf by codice.

the class GazetteerQueryOfflineSolr method getSuggestedNames.

@Override
public List<Suggestion> getSuggestedNames(String queryString, int maxResults) throws GeoEntryQueryException {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setRequestHandler(GAZETTEER_REQUEST_HANDLER);
    solrQuery.setParam(SUGGEST_Q_KEY, ClientUtils.escapeQueryChars(queryString));
    solrQuery.setParam(SUGGEST_DICT_KEY, SUGGEST_DICT);
    solrQuery.setParam(SUGGEST_COUNT_KEY, Integer.toString(Math.min(maxResults, MAX_RESULTS)));
    QueryResponse response;
    try {
        response = client.query(solrQuery);
    } catch (SolrServerException | IOException e) {
        throw new GeoEntryQueryException("Error while querying", e);
    }
    return Optional.ofNullable(response).map(QueryResponse::getSuggesterResponse).map(SuggesterResponse::getSuggestions).map(suggestionsPerDict -> suggestionsPerDict.get(SUGGEST_DICT)).orElse(Collections.emptyList()).stream().map(suggestion -> new SuggestionImpl(suggestion.getPayload(), suggestion.getTerm())).collect(Collectors.toList());
}
Also used : COLLECTION_NAME(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.COLLECTION_NAME) WKTReader(org.locationtech.jts.io.WKTReader) JtsGeometry(org.locationtech.spatial4j.shape.jts.JtsGeometry) SolrClient(org.codice.solr.client.solrj.SolrClient) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) SolrServerException(org.apache.solr.client.solrj.SolrServerException) WKTWriter(org.locationtech.jts.io.WKTWriter) Map(java.util.Map) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) COUNTRY_CODE(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.COUNTRY_CODE) ParseException(java.text.ParseException) NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) GAZETTEER_REQUEST_HANDLER(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.GAZETTEER_REQUEST_HANDLER) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) ImmutableMap(com.google.common.collect.ImmutableMap) Point(org.locationtech.jts.geom.Point) GeoCodingConstants(org.codice.ddf.spatial.geocoding.GeoCodingConstants) Shape(org.locationtech.spatial4j.shape.Shape) Collectors(java.util.stream.Collectors) Suggestion(org.codice.ddf.spatial.geocoding.Suggestion) Objects(java.util.Objects) List(java.util.List) InvalidShapeException(org.locationtech.spatial4j.exception.InvalidShapeException) FEATURE_CODE(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.FEATURE_CODE) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Optional(java.util.Optional) Geometry(org.locationtech.jts.geom.Geometry) LOCATION(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.LOCATION) ClientUtils(org.apache.solr.client.solrj.util.ClientUtils) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) SUGGEST_DICT(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_DICT) ValidationRule(org.locationtech.spatial4j.context.jts.ValidationRule) SolrClientFactory(org.codice.solr.factory.SolrClientFactory) SUGGEST_DICT_KEY(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_DICT_KEY) METHOD(org.apache.solr.client.solrj.SolrRequest.METHOD) SUGGEST_Q_KEY(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_Q_KEY) GeoEntryQueryable(org.codice.ddf.spatial.geocoding.GeoEntryQueryable) SuggesterResponse(org.apache.solr.client.solrj.response.SuggesterResponse) JtsSpatialContextFactory(org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory) Logger(org.slf4j.Logger) NAME(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.NAME) SORT_VALUE(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SORT_VALUE) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) IOException(java.io.IOException) SpatialContextFactory(org.locationtech.spatial4j.context.SpatialContextFactory) SolrDocument(org.apache.solr.common.SolrDocument) SUGGEST_COUNT_KEY(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.SUGGEST_COUNT_KEY) POPULATION(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.POPULATION) ID(ddf.catalog.solr.offlinegazetteer.GazetteerConstants.ID) Collections(java.util.Collections) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 3 with SuggesterResponse

use of org.apache.solr.client.solrj.response.SuggesterResponse in project ddf by codice.

the class SolrMetacardClientImpl method handleSuggestionResponse.

private void handleSuggestionResponse(QueryResponse solrResponse, Map<String, Serializable> responseProps) {
    SuggesterResponse suggesterResponse = solrResponse.getSuggesterResponse();
    if (suggesterResponse != null) {
        List<Map.Entry<String, String>> suggestionResults = suggesterResponse.getSuggestions().values().stream().flatMap(List::stream).map(suggestion -> new AbstractMap.SimpleImmutableEntry<>(suggestion.getPayload(), suggestion.getTerm())).collect(Collectors.toList());
        responseProps.put(SUGGESTION_RESULT_KEY, (Serializable) suggestionResults);
    }
}
Also used : ADDITIONAL_SORT_BYS(ddf.catalog.Constants.ADDITIONAL_SORT_BYS) Arrays(java.util.Arrays) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrClient(org.codice.solr.client.solrj.SolrClient) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) LoggerFactory(org.slf4j.LoggerFactory) MetacardCreationException(ddf.catalog.data.MetacardCreationException) TermFacetProperties(ddf.catalog.operation.TermFacetProperties) AttributeType(ddf.catalog.data.AttributeType) NumberUtils(org.apache.commons.lang.math.NumberUtils) BooleanUtils(org.apache.commons.lang.BooleanUtils) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) FIRST_CHAR_OF_SUFFIX(ddf.catalog.source.solr.DynamicSchemaResolver.FIRST_CHAR_OF_SUFFIX) ContentTypeImpl(ddf.catalog.data.impl.ContentTypeImpl) Map(java.util.Map) SUGGESTION_QUERY_KEY(ddf.catalog.Constants.SUGGESTION_QUERY_KEY) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) Collection(java.util.Collection) AbstractUpdateRequest(org.apache.solr.client.solrj.request.AbstractUpdateRequest) ResultImpl(ddf.catalog.data.impl.ResultImpl) Set(java.util.Set) PrivilegedAction(java.security.PrivilegedAction) Collation(org.apache.solr.client.solrj.response.SpellCheckResponse.Collation) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) MetacardType(ddf.catalog.data.MetacardType) Serializable(java.io.Serializable) List(java.util.List) Attribute(ddf.catalog.data.Attribute) SUGGEST_DICT(org.apache.solr.spelling.suggest.SuggesterParams.SUGGEST_DICT) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Optional(java.util.Optional) SUGGEST_BUILD(org.apache.solr.spelling.suggest.SuggesterParams.SUGGEST_BUILD) AccessController(java.security.AccessController) SolrInputDocument(org.apache.solr.common.SolrInputDocument) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) HashMap(java.util.HashMap) FilterAdapter(ddf.catalog.filter.FilterAdapter) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FacetAttributeResultImpl(ddf.catalog.operation.impl.FacetAttributeResultImpl) SortBy(org.opengis.filter.sort.SortBy) Lists(com.google.common.collect.Lists) METHOD(org.apache.solr.client.solrj.SolrRequest.METHOD) FacetAttributeResult(ddf.catalog.operation.FacetAttributeResult) Metacard(ddf.catalog.data.Metacard) CollectionUtils(org.apache.commons.collections.CollectionUtils) FacetField(org.apache.solr.client.solrj.response.FacetField) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) QueryRequest(ddf.catalog.operation.QueryRequest) SUGGEST_CONTEXT_FILTER_QUERY(org.apache.solr.spelling.suggest.SuggesterParams.SUGGEST_CONTEXT_FILTER_QUERY) Result(ddf.catalog.data.Result) SortOrder(org.opengis.filter.sort.SortOrder) SuggesterResponse(org.apache.solr.client.solrj.response.SuggesterResponse) ContentType(ddf.catalog.data.ContentType) Logger(org.slf4j.Logger) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) EXPERIMENTAL_FACET_PROPERTIES_KEY(ddf.catalog.Constants.EXPERIMENTAL_FACET_PROPERTIES_KEY) SUGGESTION_RESULT_KEY(ddf.catalog.Constants.SUGGESTION_RESULT_KEY) IOException(java.io.IOException) EXPERIMENTAL_FACET_RESULTS_KEY(ddf.catalog.Constants.EXPERIMENTAL_FACET_RESULTS_KEY) CommonParams(org.apache.solr.common.params.CommonParams) SUGGESTION_BUILD_KEY(ddf.catalog.Constants.SUGGESTION_BUILD_KEY) SolrDocument(org.apache.solr.common.SolrDocument) SourceResponse(ddf.catalog.operation.SourceResponse) AbstractMap(java.util.AbstractMap) SUGGESTION_DICT_KEY(ddf.catalog.Constants.SUGGESTION_DICT_KEY) PivotField(org.apache.solr.client.solrj.response.PivotField) Distance(ddf.measure.Distance) SUGGESTION_CONTEXT_KEY(ddf.catalog.Constants.SUGGESTION_CONTEXT_KEY) Collections(java.util.Collections) SUGGEST_Q(org.apache.solr.spelling.suggest.SuggesterParams.SUGGEST_Q) SuggesterResponse(org.apache.solr.client.solrj.response.SuggesterResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

IOException (java.io.IOException)2 List (java.util.List)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2 SuggesterResponse (org.apache.solr.client.solrj.response.SuggesterResponse)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 ADDITIONAL_SORT_BYS (ddf.catalog.Constants.ADDITIONAL_SORT_BYS)1 EXPERIMENTAL_FACET_PROPERTIES_KEY (ddf.catalog.Constants.EXPERIMENTAL_FACET_PROPERTIES_KEY)1 EXPERIMENTAL_FACET_RESULTS_KEY (ddf.catalog.Constants.EXPERIMENTAL_FACET_RESULTS_KEY)1 SUGGESTION_BUILD_KEY (ddf.catalog.Constants.SUGGESTION_BUILD_KEY)1 SUGGESTION_CONTEXT_KEY (ddf.catalog.Constants.SUGGESTION_CONTEXT_KEY)1 SUGGESTION_DICT_KEY (ddf.catalog.Constants.SUGGESTION_DICT_KEY)1 SUGGESTION_QUERY_KEY (ddf.catalog.Constants.SUGGESTION_QUERY_KEY)1 SUGGESTION_RESULT_KEY (ddf.catalog.Constants.SUGGESTION_RESULT_KEY)1 Attribute (ddf.catalog.data.Attribute)1 AttributeType (ddf.catalog.data.AttributeType)1 ContentType (ddf.catalog.data.ContentType)1 Metacard (ddf.catalog.data.Metacard)1