Search in sources :

Example 56 with SimpleOrderedMap

use of org.apache.solr.common.util.SimpleOrderedMap in project lucene-solr by apache.

the class SpellCheckComponent method process.

@Override
@SuppressWarnings("unchecked")
public void process(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    if (!params.getBool(COMPONENT_NAME, false) || spellCheckers.isEmpty()) {
        return;
    }
    boolean shardRequest = "true".equals(params.get(ShardParams.IS_SHARD));
    String q = params.get(SPELLCHECK_Q);
    SolrSpellChecker spellChecker = getSpellChecker(params);
    Collection<Token> tokens = null;
    if (q != null) {
        //we have a spell check param, tokenize it with the query analyzer applicable for this spellchecker
        tokens = getTokens(q, spellChecker.getQueryAnalyzer());
    } else {
        q = rb.getQueryString();
        if (q == null) {
            q = params.get(CommonParams.Q);
        }
        tokens = queryConverter.convert(q);
    }
    if (tokens != null && tokens.isEmpty() == false) {
        if (spellChecker != null) {
            int count = params.getInt(SPELLCHECK_COUNT, 1);
            boolean onlyMorePopular = params.getBool(SPELLCHECK_ONLY_MORE_POPULAR, DEFAULT_ONLY_MORE_POPULAR);
            boolean extendedResults = params.getBool(SPELLCHECK_EXTENDED_RESULTS, false);
            boolean collate = params.getBool(SPELLCHECK_COLLATE, false);
            float accuracy = params.getFloat(SPELLCHECK_ACCURACY, Float.MIN_VALUE);
            int alternativeTermCount = params.getInt(SpellingParams.SPELLCHECK_ALTERNATIVE_TERM_COUNT, 0);
            //If specified, this can be a discrete # of results, or a percentage of fq results.
            Integer maxResultsForSuggest = maxResultsForSuggest(rb);
            ModifiableSolrParams customParams = new ModifiableSolrParams();
            for (String checkerName : getDictionaryNames(params)) {
                customParams.add(getCustomParams(checkerName, params));
            }
            Integer hitsInteger = (Integer) rb.rsp.getToLog().get("hits");
            long hits = 0;
            if (hitsInteger == null) {
                hits = rb.getNumberDocumentsFound();
            } else {
                hits = hitsInteger.longValue();
            }
            SpellingResult spellingResult = null;
            if (maxResultsForSuggest == null || hits <= maxResultsForSuggest) {
                SuggestMode suggestMode = SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX;
                if (onlyMorePopular) {
                    suggestMode = SuggestMode.SUGGEST_MORE_POPULAR;
                } else if (alternativeTermCount > 0) {
                    suggestMode = SuggestMode.SUGGEST_ALWAYS;
                }
                IndexReader reader = rb.req.getSearcher().getIndexReader();
                SpellingOptions options = new SpellingOptions(tokens, reader, count, alternativeTermCount, suggestMode, extendedResults, accuracy, customParams);
                spellingResult = spellChecker.getSuggestions(options);
            } else {
                spellingResult = new SpellingResult();
            }
            boolean isCorrectlySpelled = hits > (maxResultsForSuggest == null ? 0 : maxResultsForSuggest);
            NamedList response = new SimpleOrderedMap();
            NamedList suggestions = toNamedList(shardRequest, spellingResult, q, extendedResults);
            response.add("suggestions", suggestions);
            if (extendedResults) {
                response.add("correctlySpelled", isCorrectlySpelled);
            }
            if (collate) {
                addCollationsToResponse(params, spellingResult, rb, q, response, spellChecker.isSuggestionsMayOverlap());
            }
            if (shardRequest) {
                addOriginalTermsToResponse(response, tokens);
            }
            rb.rsp.add("spellcheck", response);
        } else {
            throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Specified dictionaries do not exist: " + getDictionaryNameAsSingleString(getDictionaryNames(params)));
        }
    }
}
Also used : ConjunctionSolrSpellChecker(org.apache.solr.spelling.ConjunctionSolrSpellChecker) SolrSpellChecker(org.apache.solr.spelling.SolrSpellChecker) NamedList(org.apache.solr.common.util.NamedList) Token(org.apache.lucene.analysis.Token) SpellingOptions(org.apache.solr.spelling.SpellingOptions) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SuggestMode(org.apache.lucene.search.spell.SuggestMode) SpellingResult(org.apache.solr.spelling.SpellingResult) IndexReader(org.apache.lucene.index.IndexReader) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrException(org.apache.solr.common.SolrException)

Example 57 with SimpleOrderedMap

use of org.apache.solr.common.util.SimpleOrderedMap in project lucene-solr by apache.

the class SpellCheckComponent method toNamedList.

protected NamedList toNamedList(boolean shardRequest, SpellingResult spellingResult, String origQuery, boolean extendedResults) {
    NamedList result = new NamedList();
    Map<Token, LinkedHashMap<String, Integer>> suggestions = spellingResult.getSuggestions();
    boolean hasFreqInfo = spellingResult.hasTokenFrequencyInfo();
    boolean hasSuggestions = false;
    boolean hasZeroFrequencyToken = false;
    for (Map.Entry<Token, LinkedHashMap<String, Integer>> entry : suggestions.entrySet()) {
        Token inputToken = entry.getKey();
        String tokenString = new String(inputToken.buffer(), 0, inputToken.length());
        Map<String, Integer> theSuggestions = new LinkedHashMap<>(entry.getValue());
        Iterator<String> sugIter = theSuggestions.keySet().iterator();
        while (sugIter.hasNext()) {
            String sug = sugIter.next();
            if (sug.equals(tokenString)) {
                sugIter.remove();
            }
        }
        if (theSuggestions.size() > 0) {
            hasSuggestions = true;
        }
        if (theSuggestions != null && (theSuggestions.size() > 0 || shardRequest)) {
            SimpleOrderedMap suggestionList = new SimpleOrderedMap();
            suggestionList.add("numFound", theSuggestions.size());
            suggestionList.add("startOffset", inputToken.startOffset());
            suggestionList.add("endOffset", inputToken.endOffset());
            // ]
            if (extendedResults && hasFreqInfo) {
                suggestionList.add("origFreq", spellingResult.getTokenFrequency(inputToken));
                ArrayList<SimpleOrderedMap> sugs = new ArrayList<>();
                suggestionList.add("suggestion", sugs);
                for (Map.Entry<String, Integer> suggEntry : theSuggestions.entrySet()) {
                    SimpleOrderedMap sugEntry = new SimpleOrderedMap();
                    sugEntry.add("word", suggEntry.getKey());
                    sugEntry.add("freq", suggEntry.getValue());
                    sugs.add(sugEntry);
                }
            } else {
                suggestionList.add("suggestion", theSuggestions.keySet());
            }
            if (hasFreqInfo) {
                Integer tokenFrequency = spellingResult.getTokenFrequency(inputToken);
                if (tokenFrequency == null || tokenFrequency == 0) {
                    hasZeroFrequencyToken = true;
                }
            }
            result.add(tokenString, suggestionList);
        }
    }
    return result;
}
Also used : NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) Token(org.apache.lucene.analysis.Token) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) LinkedHashMap(java.util.LinkedHashMap) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 58 with SimpleOrderedMap

use of org.apache.solr.common.util.SimpleOrderedMap in project lucene-solr by apache.

the class GroupedEndResultTransformer method transform.

/**
   * {@inheritDoc}
   */
@Override
public void transform(Map<String, ?> result, ResponseBuilder rb, SolrDocumentSource solrDocumentSource) {
    NamedList<Object> commands = new SimpleOrderedMap<>();
    for (Map.Entry<String, ?> entry : result.entrySet()) {
        Object value = entry.getValue();
        if (TopGroups.class.isInstance(value)) {
            @SuppressWarnings("unchecked") TopGroups<BytesRef> topGroups = (TopGroups<BytesRef>) value;
            NamedList<Object> command = new SimpleOrderedMap<>();
            command.add("matches", rb.totalHitCount);
            Integer totalGroupCount = rb.mergedGroupCounts.get(entry.getKey());
            if (totalGroupCount != null) {
                command.add("ngroups", totalGroupCount);
            }
            List<NamedList> groups = new ArrayList<>();
            SchemaField groupField = searcher.getSchema().getField(entry.getKey());
            FieldType groupFieldType = groupField.getType();
            for (GroupDocs<BytesRef> group : topGroups.groups) {
                SimpleOrderedMap<Object> groupResult = new SimpleOrderedMap<>();
                if (group.groupValue != null) {
                    groupResult.add("groupValue", groupFieldType.toObject(groupField.createField(group.groupValue.utf8ToString())));
                } else {
                    groupResult.add("groupValue", null);
                }
                SolrDocumentList docList = new SolrDocumentList();
                docList.setNumFound(group.totalHits);
                if (!Float.isNaN(group.maxScore)) {
                    docList.setMaxScore(group.maxScore);
                }
                docList.setStart(rb.getGroupingSpec().getWithinGroupOffset());
                for (ScoreDoc scoreDoc : group.scoreDocs) {
                    docList.add(solrDocumentSource.retrieve(scoreDoc));
                }
                groupResult.add("doclist", docList);
                groups.add(groupResult);
            }
            command.add("groups", groups);
            commands.add(entry.getKey(), command);
        } else if (QueryCommandResult.class.isInstance(value)) {
            QueryCommandResult queryCommandResult = (QueryCommandResult) value;
            NamedList<Object> command = new SimpleOrderedMap<>();
            command.add("matches", queryCommandResult.getMatches());
            SolrDocumentList docList = new SolrDocumentList();
            docList.setNumFound(queryCommandResult.getTopDocs().totalHits);
            if (!Float.isNaN(queryCommandResult.getTopDocs().getMaxScore())) {
                docList.setMaxScore(queryCommandResult.getTopDocs().getMaxScore());
            }
            docList.setStart(rb.getGroupingSpec().getWithinGroupOffset());
            for (ScoreDoc scoreDoc : queryCommandResult.getTopDocs().scoreDocs) {
                docList.add(solrDocumentSource.retrieve(scoreDoc));
            }
            command.add("doclist", docList);
            commands.add(entry.getKey(), command);
        }
    }
    rb.rsp.add("grouped", commands);
}
Also used : NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) QueryCommandResult(org.apache.solr.search.grouping.distributed.command.QueryCommandResult) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) FieldType(org.apache.solr.schema.FieldType) ScoreDoc(org.apache.lucene.search.ScoreDoc) SchemaField(org.apache.solr.schema.SchemaField) TopGroups(org.apache.lucene.search.grouping.TopGroups) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef)

Example 59 with SimpleOrderedMap

use of org.apache.solr.common.util.SimpleOrderedMap in project jackrabbit-oak by apache.

the class SolrQueryIndex method putSuggestions.

private void putSuggestions(Set<Map.Entry<String, Object>> suggestEntries, final Deque<SolrResultRow> queue, Filter filter, OakSolrConfiguration configuration, SolrServer solrServer) throws SolrServerException {
    Collection<SimpleOrderedMap<Object>> retrievedSuggestions = new HashSet<SimpleOrderedMap<Object>>();
    for (Map.Entry<String, Object> suggester : suggestEntries) {
        SimpleOrderedMap<Object> suggestionResponses = ((SimpleOrderedMap) suggester.getValue());
        for (Map.Entry<String, Object> suggestionResponse : suggestionResponses) {
            SimpleOrderedMap<Object> suggestionResults = ((SimpleOrderedMap) suggestionResponse.getValue());
            for (Map.Entry<String, Object> suggestionResult : suggestionResults) {
                if ("suggestions".equals(suggestionResult.getKey())) {
                    ArrayList<SimpleOrderedMap<Object>> suggestions = ((ArrayList<SimpleOrderedMap<Object>>) suggestionResult.getValue());
                    if (!suggestions.isEmpty()) {
                        for (SimpleOrderedMap<Object> suggestion : suggestions) {
                            retrievedSuggestions.add(suggestion);
                        }
                    }
                }
            }
        }
    }
    // ACL filter suggestions
    for (SimpleOrderedMap<Object> suggestion : retrievedSuggestions) {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setParam("q", String.valueOf(suggestion.get("term")));
        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(suggestion.get("term").toString(), Double.parseDouble(suggestion.get("weight").toString())));
                    break;
                }
            }
        }
    }
}
Also used : SolrDocumentList(org.apache.solr.common.SolrDocumentList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) HashSet(java.util.HashSet)

Example 60 with SimpleOrderedMap

use of org.apache.solr.common.util.SimpleOrderedMap in project lucene-solr by apache.

the class SpellCheckCollatorTest method testCollationWithRangeQuery.

@Test
public void testCollationWithRangeQuery() throws Exception {
    SolrCore core = h.getCore();
    SearchComponent speller = core.getSearchComponent("spellcheck");
    assertTrue("speller is null and it shouldn't be", speller != null);
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add(SpellCheckComponent.COMPONENT_NAME, "true");
    params.add(SpellingParams.SPELLCHECK_BUILD, "true");
    params.add(SpellingParams.SPELLCHECK_COUNT, "10");
    params.add(SpellingParams.SPELLCHECK_COLLATE, "true");
    params.add(SpellingParams.SPELLCHECK_ALTERNATIVE_TERM_COUNT, "10");
    params.add(CommonParams.Q, "id:[1 TO 10] AND lowerfilt:lovw");
    {
        SolrRequestHandler handler = core.getRequestHandler("spellCheckCompRH");
        SolrQueryResponse rsp = new SolrQueryResponse();
        rsp.addResponseHeader(new SimpleOrderedMap());
        SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
        handler.handleRequest(req, rsp);
        req.close();
        NamedList values = rsp.getValues();
        NamedList spellCheck = (NamedList) values.get("spellcheck");
        NamedList collationHolder = (NamedList) spellCheck.get("collations");
        List<String> collations = collationHolder.getAll("collation");
        assertTrue(collations.size() == 1);
        String collation = collations.iterator().next();
        System.out.println(collation);
        assertTrue("Incorrect collation: " + collation, "id:[1 TO 10] AND lowerfilt:love".equals(collation));
    }
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrCore(org.apache.solr.core.SolrCore) NamedList(org.apache.solr.common.util.NamedList) SearchComponent(org.apache.solr.handler.component.SearchComponent) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrRequestHandler(org.apache.solr.request.SolrRequestHandler) Test(org.junit.Test)

Aggregations

SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)131 NamedList (org.apache.solr.common.util.NamedList)69 ArrayList (java.util.ArrayList)36 SolrException (org.apache.solr.common.SolrException)32 Map (java.util.Map)27 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)22 IOException (java.io.IOException)19 List (java.util.List)19 HashMap (java.util.HashMap)18 SolrParams (org.apache.solr.common.params.SolrParams)16 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)16 SchemaField (org.apache.solr.schema.SchemaField)16 FieldType (org.apache.solr.schema.FieldType)14 BytesRef (org.apache.lucene.util.BytesRef)13 Test (org.junit.Test)13 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)12 SolrDocumentList (org.apache.solr.common.SolrDocumentList)11 HashSet (java.util.HashSet)10 SolrCore (org.apache.solr.core.SolrCore)10 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)9