Search in sources :

Example 1 with SpellCheckResponse

use of org.apache.solr.client.solrj.response.SpellCheckResponse in project lucene-solr by apache.

the class SpellCheckComponent method collectShardSuggestions.

@SuppressWarnings("unchecked")
private void collectShardSuggestions(NamedList nl, SpellCheckMergeData mergeData) {
    SpellCheckResponse spellCheckResp = new SpellCheckResponse(nl);
    Iterable<Object> originalTermStrings = (Iterable<Object>) nl.get("originalTerms");
    if (originalTermStrings != null) {
        mergeData.originalTerms = new HashSet<>();
        for (Object originalTermObj : originalTermStrings) {
            mergeData.originalTerms.add(originalTermObj.toString());
        }
    }
    for (SpellCheckResponse.Suggestion suggestion : spellCheckResp.getSuggestions()) {
        mergeData.origVsSuggestion.put(suggestion.getToken(), suggestion);
        HashSet<String> suggested = mergeData.origVsSuggested.get(suggestion.getToken());
        if (suggested == null) {
            suggested = new HashSet<>();
            mergeData.origVsSuggested.put(suggestion.getToken(), suggested);
        }
        // sum up original frequency          
        int origFreq = 0;
        Integer o = mergeData.origVsFreq.get(suggestion.getToken());
        if (o != null)
            origFreq += o;
        origFreq += suggestion.getOriginalFrequency();
        mergeData.origVsFreq.put(suggestion.getToken(), origFreq);
        //# shards reporting
        Integer origShards = mergeData.origVsShards.get(suggestion.getToken());
        if (origShards == null) {
            mergeData.origVsShards.put(suggestion.getToken(), 1);
        } else {
            mergeData.origVsShards.put(suggestion.getToken(), ++origShards);
        }
        // find best suggestions
        for (int i = 0; i < suggestion.getNumFound(); i++) {
            String alternative = suggestion.getAlternatives().get(i);
            suggested.add(alternative);
            SuggestWord sug = mergeData.suggestedVsWord.get(alternative);
            if (sug == null) {
                sug = new SuggestWord();
                mergeData.suggestedVsWord.put(alternative, sug);
            }
            sug.string = alternative;
            // alternative frequency is present only for extendedResults=true
            if (suggestion.getAlternativeFrequencies() != null && suggestion.getAlternativeFrequencies().size() > 0) {
                Integer freq = suggestion.getAlternativeFrequencies().get(i);
                if (freq != null)
                    sug.freq += freq;
            }
        }
    }
}
Also used : SuggestWord(org.apache.lucene.search.spell.SuggestWord) SpellCheckResponse(org.apache.solr.client.solrj.response.SpellCheckResponse)

Example 2 with SpellCheckResponse

use of org.apache.solr.client.solrj.response.SpellCheckResponse in project jackrabbit-oak by apache.

the class SolrQueryIndex method getIterator.

private AbstractIterator<SolrResultRow> getIterator(final Filter filter, final IndexPlan plan, final String parent, final int parentDepth, final OakSolrConfiguration configuration, final SolrClient solrServer, final LMSEstimator estimator) {
    return new AbstractIterator<SolrResultRow>() {

        public Collection<FacetField> facetFields = new LinkedList<FacetField>();

        private final Set<String> seenPaths = Sets.newHashSet();

        private final Deque<SolrResultRow> queue = Queues.newArrayDeque();

        private int offset = 0;

        private boolean noDocs = false;

        private long numFound = 0;

        @Override
        protected SolrResultRow computeNext() {
            if (!queue.isEmpty() || loadDocs()) {
                return queue.remove();
            }
            return endOfData();
        }

        private SolrResultRow convertToRow(SolrDocument doc) {
            String path = String.valueOf(doc.getFieldValue(configuration.getPathField()));
            if ("".equals(path)) {
                path = "/";
            }
            if (!parent.isEmpty()) {
                path = getAncestorPath(path, parentDepth);
                // avoid duplicate entries
                if (seenPaths.contains(path)) {
                    return null;
                }
                seenPaths.add(path);
            }
            float score = 0f;
            Object scoreObj = doc.get("score");
            if (scoreObj != null) {
                score = (Float) scoreObj;
            }
            return new SolrResultRow(path, score, doc, facetFields);
        }

        /**
         * Loads the Solr documents in batches
         * @return true if any document is loaded
         */
        private boolean loadDocs() {
            if (noDocs) {
                return false;
            }
            try {
                if (log.isDebugEnabled()) {
                    log.debug("converting filter {}", filter);
                }
                SolrQuery query = FilterQueryParser.getQuery(filter, plan, configuration);
                if (numFound > 0) {
                    long rows = configuration.getRows();
                    long maxQueries = numFound / 2;
                    if (maxQueries > configuration.getRows()) {
                        // adjust the rows to avoid making more than 3 Solr requests for this particular query
                        rows = maxQueries;
                        query.setParam("rows", String.valueOf(rows));
                    }
                    long newOffset = configuration.getRows() + offset * rows;
                    if (newOffset >= numFound) {
                        return false;
                    }
                    query.setParam("start", String.valueOf(newOffset));
                    offset++;
                }
                if (log.isDebugEnabled()) {
                    log.debug("sending query {}", query);
                }
                QueryResponse queryResponse = solrServer.query(query);
                if (log.isDebugEnabled()) {
                    log.debug("getting response {}", queryResponse.getHeader());
                }
                SolrDocumentList docs = queryResponse.getResults();
                if (docs != null) {
                    numFound = docs.getNumFound();
                    estimator.update(filter, docs);
                    Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
                    for (SolrDocument doc : docs) {
                        // handle highlight
                        if (highlighting != null) {
                            Object pathObject = doc.getFieldValue(configuration.getPathField());
                            if (pathObject != null && highlighting.get(String.valueOf(pathObject)) != null) {
                                Map<String, List<String>> value = highlighting.get(String.valueOf(pathObject));
                                for (Map.Entry<String, List<String>> entry : value.entrySet()) {
                                    // all highlighted values end up in 'rep:excerpt', regardless of field match
                                    for (String v : entry.getValue()) {
                                        doc.addField(QueryConstants.REP_EXCERPT, v);
                                    }
                                }
                            }
                        }
                        SolrResultRow row = convertToRow(doc);
                        if (row != null) {
                            queue.add(row);
                        }
                    }
                }
                // get facets
                List<FacetField> returnedFieldFacet = queryResponse.getFacetFields();
                if (returnedFieldFacet != null) {
                    facetFields.addAll(returnedFieldFacet);
                }
                // filter facets on doc paths
                if (!facetFields.isEmpty() && docs != null) {
                    for (SolrDocument doc : docs) {
                        String path = String.valueOf(doc.getFieldValue(configuration.getPathField()));
                        // if facet path doesn't exist for the calling user, filter the facet for this doc
                        for (FacetField ff : facetFields) {
                            if (!filter.isAccessible(path + "/" + ff.getName())) {
                                filterFacet(doc, ff);
                            }
                        }
                    }
                }
                // handle spellcheck
                SpellCheckResponse spellCheckResponse = queryResponse.getSpellCheckResponse();
                if (spellCheckResponse != null && spellCheckResponse.getSuggestions() != null && spellCheckResponse.getSuggestions().size() > 0) {
                    putSpellChecks(spellCheckResponse, queue, filter, configuration, solrServer);
                    noDocs = true;
                }
                // handle suggest
                NamedList<Object> response = queryResponse.getResponse();
                Map suggest = (Map) response.get("suggest");
                if (suggest != null) {
                    Set<Map.Entry<String, Object>> suggestEntries = suggest.entrySet();
                    if (!suggestEntries.isEmpty()) {
                        putSuggestions(suggestEntries, queue, filter, configuration, solrServer);
                        noDocs = true;
                    }
                }
            } catch (Exception e) {
                if (log.isWarnEnabled()) {
                    log.warn("query via {} failed.", solrServer, e);
                }
            }
            return !queue.isEmpty();
        }
    };
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) FacetField(org.apache.solr.client.solrj.response.FacetField) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SpellCheckResponse(org.apache.solr.client.solrj.response.SpellCheckResponse) SolrDocument(org.apache.solr.common.SolrDocument) SolrDocumentList(org.apache.solr.common.SolrDocumentList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) NamedList(org.apache.solr.common.util.NamedList) AbstractIterator(com.google.common.collect.AbstractIterator) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Deque(java.util.Deque) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Collection(java.util.Collection) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap)

Example 3 with SpellCheckResponse

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

the class ItemSearchServiceLiveTest method whenSearchingWithKeywordWithMistake_thenSpellingSuggestionsShouldBeReturned.

@Test
public void whenSearchingWithKeywordWithMistake_thenSpellingSuggestionsShouldBeReturned() 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", "Washing equipments", 250f);
    SolrQuery query = new SolrQuery();
    query.setQuery("hme");
    query.set("spellcheck", "on");
    QueryResponse response = solrClient.query(query);
    SpellCheckResponse spellCheckResponse = response.getSpellCheckResponse();
    assertEquals(false, spellCheckResponse.isCorrectlySpelled());
    Suggestion suggestion = spellCheckResponse.getSuggestions().get(0);
    assertEquals("hme", suggestion.getToken());
    List<String> alternatives = suggestion.getAlternatives();
    String alternative = alternatives.get(0);
    assertEquals("home", alternative);
}
Also used : Suggestion(org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SpellCheckResponse(org.apache.solr.client.solrj.response.SpellCheckResponse) Test(org.junit.Test)

Example 4 with SpellCheckResponse

use of org.apache.solr.client.solrj.response.SpellCheckResponse in project nextprot-api by calipho-sib.

the class SolrServiceImpl method buildSearchResult.

private SearchResult buildSearchResult(SolrQuery query, String indexName, String url, QueryResponse response) {
    SearchResult results = new SearchResult(indexName, url);
    SolrDocumentList docs = response.getResults();
    Logger.debug("Response doc size:" + docs.size());
    List<Map<String, Object>> res = new ArrayList<>();
    Map<String, Object> item = null;
    for (SolrDocument doc : docs) {
        item = new HashMap<>();
        for (Entry<String, Object> e : doc.entrySet()) item.put(e.getKey(), e.getValue());
        res.add(item);
    }
    results.addAllResults(res);
    if (query.getStart() != null)
        results.setStart(query.getStart());
    results.setRows(query.getRows());
    results.setElapsedTime(response.getElapsedTime());
    results.setFound(docs.getNumFound());
    if (docs.getMaxScore() != null)
        results.setScore(docs.getMaxScore());
    // Facets
    List<FacetField> facetFields = response.getFacetFields();
    Logger.debug("Response facet fields:" + facetFields.size());
    if (facetFields != null) {
        Facet facet = null;
        for (FacetField ff : facetFields) {
            facet = new Facet(ff.getName());
            Logger.debug("Response facet field:" + ff.getName() + " count:" + ff.getValueCount());
            for (Count c : ff.getValues()) facet.addFacetField(c.getName(), c.getCount());
            results.addSearchResultFacet(facet);
        }
    }
    // Spellcheck
    SpellCheckResponse spellcheckResponse = response.getSpellCheckResponse();
    if (spellcheckResponse != null) {
        Spellcheck spellcheckResult = new Spellcheck();
        List<Suggestion> suggestions = spellcheckResponse.getSuggestions();
        List<Collation> collations = spellcheckResponse.getCollatedResults();
        if (collations != null) {
            for (Collation c : collations) spellcheckResult.addCollation(c.getCollationQueryString(), c.getNumberOfHits());
        }
        if (suggestions != null)
            for (Suggestion s : suggestions) spellcheckResult.addSuggestions(s.getToken(), s.getAlternatives());
        results.setSpellCheck(spellcheckResult);
    }
    return results;
}
Also used : Spellcheck(org.nextprot.api.solr.SearchResult.Spellcheck) FacetField(org.apache.solr.client.solrj.response.FacetField) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Count(org.apache.solr.client.solrj.response.FacetField.Count) Collation(org.apache.solr.client.solrj.response.SpellCheckResponse.Collation) SpellCheckResponse(org.apache.solr.client.solrj.response.SpellCheckResponse) Suggestion(org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion) SolrDocument(org.apache.solr.common.SolrDocument) Facet(org.nextprot.api.solr.SearchResult.Facet)

Example 5 with SpellCheckResponse

use of org.apache.solr.client.solrj.response.SpellCheckResponse in project jackrabbit-oak by apache.

the class SolrQueryIndex method putSpellChecks.

private void putSpellChecks(SpellCheckResponse spellCheckResponse, final Deque<SolrResultRow> queue, Filter filter, OakSolrConfiguration configuration, SolrServer solrServer) throws SolrServerException {
    List<SpellCheckResponse.Suggestion> suggestions = spellCheckResponse.getSuggestions();
    Collection<String> alternatives = new ArrayList<String>(suggestions.size());
    for (SpellCheckResponse.Suggestion suggestion : suggestions) {
        alternatives.addAll(suggestion.getAlternatives());
    }
    // ACL filter spellcheck results
    for (String alternative : alternatives) {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setParam("q", alternative);
        solrQuery.setParam("df", configuration.getCatchAllField());
        solrQuery.setParam("q.op", "AND");
        solrQuery.setParam("rows", "100");
        QueryResponse suggestQueryResponse = solrServer.query(solrQuery);
        SolrDocumentList results = suggestQueryResponse.getResults();
        if (results != null && results.getNumFound() > 0) {
            for (SolrDocument doc : results) {
                if (filter.isAccessible(String.valueOf(doc.getFieldValue(configuration.getPathField())))) {
                    queue.add(new SolrResultRow(alternative));
                    break;
                }
            }
        }
    }
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ArrayList(java.util.ArrayList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SpellCheckResponse(org.apache.solr.client.solrj.response.SpellCheckResponse)

Aggregations

SpellCheckResponse (org.apache.solr.client.solrj.response.SpellCheckResponse)9 ArrayList (java.util.ArrayList)6 SolrQuery (org.apache.solr.client.solrj.SolrQuery)6 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)6 SolrDocument (org.apache.solr.common.SolrDocument)6 SolrDocumentList (org.apache.solr.common.SolrDocumentList)6 FacetField (org.apache.solr.client.solrj.response.FacetField)4 List (java.util.List)3 Map (java.util.Map)3 SolrServerException (org.apache.solr.client.solrj.SolrServerException)3 AbstractIterator (com.google.common.collect.AbstractIterator)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Deque (java.util.Deque)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Set (java.util.Set)2 WeakHashMap (java.util.WeakHashMap)2 Collation (org.apache.solr.client.solrj.response.SpellCheckResponse.Collation)2 Suggestion (org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion)2