Search in sources :

Example 36 with SolrException

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

the class CloudMLTQParser method parse.

public Query parse() {
    String id = localParams.get(QueryParsing.V);
    // Do a Real Time Get for the document
    SolrDocument doc = getDocument(id);
    if (doc == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error completing MLT request. Could not fetch " + "document with id [" + id + "]");
    }
    String[] qf = localParams.getParams("qf");
    Map<String, Float> boostFields = new HashMap<>();
    MoreLikeThis mlt = new MoreLikeThis(req.getSearcher().getIndexReader());
    mlt.setMinTermFreq(localParams.getInt("mintf", MoreLikeThis.DEFAULT_MIN_TERM_FREQ));
    mlt.setMinDocFreq(localParams.getInt("mindf", 0));
    mlt.setMinWordLen(localParams.getInt("minwl", MoreLikeThis.DEFAULT_MIN_WORD_LENGTH));
    mlt.setMaxWordLen(localParams.getInt("maxwl", MoreLikeThis.DEFAULT_MAX_WORD_LENGTH));
    mlt.setMaxQueryTerms(localParams.getInt("maxqt", MoreLikeThis.DEFAULT_MAX_QUERY_TERMS));
    mlt.setMaxNumTokensParsed(localParams.getInt("maxntp", MoreLikeThis.DEFAULT_MAX_NUM_TOKENS_PARSED));
    mlt.setMaxDocFreq(localParams.getInt("maxdf", MoreLikeThis.DEFAULT_MAX_DOC_FREQ));
    Boolean boost = localParams.getBool("boost", MoreLikeThis.DEFAULT_BOOST);
    mlt.setBoost(boost);
    mlt.setAnalyzer(req.getSchema().getIndexAnalyzer());
    Map<String, Collection<Object>> filteredDocument = new HashMap<>();
    String[] fieldNames;
    if (qf != null) {
        ArrayList<String> fields = new ArrayList();
        for (String fieldName : qf) {
            if (!StringUtils.isEmpty(fieldName)) {
                String[] strings = splitList.split(fieldName);
                for (String string : strings) {
                    if (!StringUtils.isEmpty(string)) {
                        fields.add(string);
                    }
                }
            }
        }
        // Parse field names and boosts from the fields
        boostFields = SolrPluginUtils.parseFieldBoosts(fields.toArray(new String[0]));
        fieldNames = boostFields.keySet().toArray(new String[0]);
    } else {
        ArrayList<String> fields = new ArrayList();
        for (String field : doc.getFieldNames()) {
            // Only use fields that are stored and have an explicit analyzer.
            // This makes sense as the query uses tf/idf/.. for query construction.
            // We might want to relook and change this in the future though.
            SchemaField f = req.getSchema().getFieldOrNull(field);
            if (f != null && f.stored() && f.getType().isExplicitAnalyzer()) {
                fields.add(field);
            }
        }
        fieldNames = fields.toArray(new String[0]);
    }
    if (fieldNames.length < 1) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "MoreLikeThis requires at least one similarity field: qf");
    }
    mlt.setFieldNames(fieldNames);
    for (String field : fieldNames) {
        Collection<Object> fieldValues = doc.getFieldValues(field);
        if (fieldValues != null) {
            Collection<Object> values = new ArrayList();
            for (Object val : fieldValues) {
                if (val instanceof IndexableField) {
                    values.add(((IndexableField) val).stringValue());
                } else {
                    values.add(val);
                }
            }
            filteredDocument.put(field, values);
        }
    }
    try {
        Query rawMLTQuery = mlt.like(filteredDocument);
        BooleanQuery boostedMLTQuery = (BooleanQuery) rawMLTQuery;
        if (boost && boostFields.size() > 0) {
            BooleanQuery.Builder newQ = new BooleanQuery.Builder();
            newQ.setMinimumNumberShouldMatch(boostedMLTQuery.getMinimumNumberShouldMatch());
            for (BooleanClause clause : boostedMLTQuery) {
                Query q = clause.getQuery();
                float originalBoost = 1f;
                if (q instanceof BoostQuery) {
                    BoostQuery bq = (BoostQuery) q;
                    q = bq.getQuery();
                    originalBoost = bq.getBoost();
                }
                Float fieldBoost = boostFields.get(((TermQuery) q).getTerm().field());
                q = ((fieldBoost != null) ? new BoostQuery(q, fieldBoost * originalBoost) : clause.getQuery());
                newQ.add(q, clause.getOccur());
            }
            boostedMLTQuery = newQ.build();
        }
        // exclude current document from results
        BooleanQuery.Builder realMLTQuery = new BooleanQuery.Builder();
        realMLTQuery.add(boostedMLTQuery, BooleanClause.Occur.MUST);
        realMLTQuery.add(createIdQuery(req.getSchema().getUniqueKeyField().getName(), id), BooleanClause.Occur.MUST_NOT);
        return realMLTQuery.build();
    } catch (IOException e) {
        e.printStackTrace();
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Bad Request");
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) HashMap(java.util.HashMap) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) ArrayList(java.util.ArrayList) MoreLikeThis(org.apache.lucene.queries.mlt.MoreLikeThis) BoostQuery(org.apache.lucene.search.BoostQuery) SolrDocument(org.apache.solr.common.SolrDocument) SolrException(org.apache.solr.common.SolrException) TermQuery(org.apache.lucene.search.TermQuery) IOException(java.io.IOException) SchemaField(org.apache.solr.schema.SchemaField) IndexableField(org.apache.lucene.index.IndexableField) BooleanClause(org.apache.lucene.search.BooleanClause) Collection(java.util.Collection)

Example 37 with SolrException

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

the class SimpleMLTQParser method parse.

public Query parse() {
    String defaultField = req.getSchema().getUniqueKeyField().getName();
    String uniqueValue = localParams.get(QueryParsing.V);
    String[] qf = localParams.getParams("qf");
    SolrIndexSearcher searcher = req.getSearcher();
    Query docIdQuery = createIdQuery(defaultField, uniqueValue);
    Map<String, Float> boostFields = new HashMap<>();
    try {
        TopDocs td = searcher.search(docIdQuery, 1);
        if (td.totalHits != 1)
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error completing MLT request. Could not fetch " + "document with id [" + uniqueValue + "]");
        ScoreDoc[] scoreDocs = td.scoreDocs;
        MoreLikeThis mlt = new MoreLikeThis(req.getSearcher().getIndexReader());
        mlt.setMinTermFreq(localParams.getInt("mintf", MoreLikeThis.DEFAULT_MIN_TERM_FREQ));
        mlt.setMinDocFreq(localParams.getInt("mindf", MoreLikeThis.DEFAULT_MIN_DOC_FREQ));
        mlt.setMinWordLen(localParams.getInt("minwl", MoreLikeThis.DEFAULT_MIN_WORD_LENGTH));
        mlt.setMaxWordLen(localParams.getInt("maxwl", MoreLikeThis.DEFAULT_MAX_WORD_LENGTH));
        mlt.setMaxQueryTerms(localParams.getInt("maxqt", MoreLikeThis.DEFAULT_MAX_QUERY_TERMS));
        mlt.setMaxNumTokensParsed(localParams.getInt("maxntp", MoreLikeThis.DEFAULT_MAX_NUM_TOKENS_PARSED));
        mlt.setMaxDocFreq(localParams.getInt("maxdf", MoreLikeThis.DEFAULT_MAX_DOC_FREQ));
        Boolean boost = localParams.getBool("boost", false);
        mlt.setBoost(boost);
        String[] fieldNames;
        if (qf != null) {
            ArrayList<String> fields = new ArrayList<>();
            for (String fieldName : qf) {
                if (!StringUtils.isEmpty(fieldName)) {
                    String[] strings = splitList.split(fieldName);
                    for (String string : strings) {
                        if (!StringUtils.isEmpty(string)) {
                            fields.add(string);
                        }
                    }
                }
            }
            // Parse field names and boosts from the fields
            boostFields = SolrPluginUtils.parseFieldBoosts(fields.toArray(new String[0]));
            fieldNames = boostFields.keySet().toArray(new String[0]);
        } else {
            Map<String, SchemaField> fieldDefinitions = req.getSearcher().getSchema().getFields();
            ArrayList<String> fields = new ArrayList();
            for (String fieldName : fieldDefinitions.keySet()) {
                if (fieldDefinitions.get(fieldName).indexed() && fieldDefinitions.get(fieldName).stored())
                    if (fieldDefinitions.get(fieldName).getType().getNumberType() == null)
                        fields.add(fieldName);
            }
            fieldNames = fields.toArray(new String[0]);
        }
        if (fieldNames.length < 1) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "MoreLikeThis requires at least one similarity field: qf");
        }
        mlt.setFieldNames(fieldNames);
        mlt.setAnalyzer(req.getSchema().getIndexAnalyzer());
        Query rawMLTQuery = mlt.like(scoreDocs[0].doc);
        BooleanQuery boostedMLTQuery = (BooleanQuery) rawMLTQuery;
        if (boost && boostFields.size() > 0) {
            BooleanQuery.Builder newQ = new BooleanQuery.Builder();
            newQ.setMinimumNumberShouldMatch(boostedMLTQuery.getMinimumNumberShouldMatch());
            for (BooleanClause clause : boostedMLTQuery) {
                Query q = clause.getQuery();
                float originalBoost = 1f;
                if (q instanceof BoostQuery) {
                    BoostQuery bq = (BoostQuery) q;
                    q = bq.getQuery();
                    originalBoost = bq.getBoost();
                }
                Float fieldBoost = boostFields.get(((TermQuery) q).getTerm().field());
                q = ((fieldBoost != null) ? new BoostQuery(q, fieldBoost * originalBoost) : clause.getQuery());
                newQ.add(q, clause.getOccur());
            }
            boostedMLTQuery = newQ.build();
        }
        // exclude current document from results
        BooleanQuery.Builder realMLTQuery = new BooleanQuery.Builder();
        realMLTQuery.add(boostedMLTQuery, BooleanClause.Occur.MUST);
        realMLTQuery.add(docIdQuery, BooleanClause.Occur.MUST_NOT);
        return realMLTQuery.build();
    } catch (IOException e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error completing MLT request" + e.getMessage());
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) HashMap(java.util.HashMap) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) ArrayList(java.util.ArrayList) MoreLikeThis(org.apache.lucene.queries.mlt.MoreLikeThis) BoostQuery(org.apache.lucene.search.BoostQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) SolrException(org.apache.solr.common.SolrException) TermQuery(org.apache.lucene.search.TermQuery) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) IOException(java.io.IOException) SchemaField(org.apache.solr.schema.SchemaField) BooleanClause(org.apache.lucene.search.BooleanClause)

Example 38 with SolrException

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

the class Permission method load.

static Permission load(Map m) {
    Permission p = new Permission();
    p.originalConfig = new LinkedHashMap<>(m);
    String name = (String) m.get(NAME);
    if (!m.containsKey("role"))
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "role not specified");
    p.role = readValueAsSet(m, "role");
    if (PermissionNameProvider.Name.get(name) != null) {
        p.wellknownName = PermissionNameProvider.Name.get(name);
        HashSet<String> disAllowed = new HashSet<>(knownKeys);
        //these are the only
        disAllowed.remove("role");
        //allowed keys for well-known permissions
        disAllowed.remove(NAME);
        //allowed keys for well-known permissions
        disAllowed.remove("collection");
        disAllowed.remove("index");
        for (String s : disAllowed) {
            if (m.containsKey(s))
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, s + " is not a valid key for the permission : " + name);
        }
    }
    p.name = name;
    p.path = readSetSmart(name, m, "path");
    p.collections = readSetSmart(name, m, "collection");
    p.method = readSetSmart(name, m, "method");
    Map<String, Object> paramRules = (Map<String, Object>) m.get("params");
    if (paramRules != null) {
        p.params = new LinkedHashMap<>();
        for (Map.Entry<String, Object> e : paramRules.entrySet()) {
            if (e.getValue() == null) {
                p.params.put(e.getKey(), (String[] val) -> val == null);
            } else {
                List<String> patternStrs = e.getValue() instanceof List ? (List) e.getValue() : singletonList(e.getValue().toString());
                List patterns = patternStrs.stream().map(it -> it.startsWith("REGEX:") ? Pattern.compile(String.valueOf(it.substring("REGEX:".length()))) : it).collect(Collectors.toList());
                p.params.put(e.getKey(), val -> {
                    if (val == null)
                        return false;
                    for (Object pattern : patterns) {
                        for (String s : val) {
                            if (pattern instanceof String) {
                                if (pattern.equals(s))
                                    return true;
                            } else if (pattern instanceof Pattern) {
                                if (((Pattern) pattern).matcher(s).find())
                                    return true;
                            }
                        }
                    }
                    return false;
                });
            }
        }
    }
    return p;
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Utils(org.apache.solr.common.util.Utils) Collection(java.util.Collection) Set(java.util.Set) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Collections.singletonList(java.util.Collections.singletonList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) SolrException(org.apache.solr.common.SolrException) NAME(org.apache.solr.common.params.CommonParams.NAME) List(java.util.List) Collections.singleton(java.util.Collections.singleton) Map(java.util.Map) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Pattern(java.util.regex.Pattern) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException) HashSet(java.util.HashSet)

Example 39 with SolrException

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

the class SchemaSimilarityFactory method getSimilarity.

@Override
public Similarity getSimilarity() {
    if (null == core) {
        throw new IllegalStateException("SchemaSimilarityFactory can not be used until SolrCoreAware.inform has been called");
    }
    if (null == similarity) {
        // Need to instantiate lazily, can't do this in inform(SolrCore) because of chicken/egg
        // circular initialization hell with core.getLatestSchema() to lookup defaultSimFromFieldType
        Similarity defaultSim = null;
        if (null == defaultSimFromFieldType) {
            // nothing configured, choose a sensible implicit default...
            defaultSim = this.core.getSolrConfig().luceneMatchVersion.onOrAfter(Version.LUCENE_6_0_0) ? new BM25Similarity() : new ClassicSimilarity();
        } else {
            FieldType defSimFT = core.getLatestSchema().getFieldTypeByName(defaultSimFromFieldType);
            if (null == defSimFT) {
                throw new SolrException(ErrorCode.SERVER_ERROR, "SchemaSimilarityFactory configured with " + INIT_OPT + "='" + defaultSimFromFieldType + "' but that <fieldType> does not exist");
            }
            defaultSim = defSimFT.getSimilarity();
            if (null == defaultSim) {
                throw new SolrException(ErrorCode.SERVER_ERROR, "SchemaSimilarityFactory configured with " + INIT_OPT + "='" + defaultSimFromFieldType + "' but that <fieldType> does not define a <similarity>");
            }
        }
        similarity = new SchemaSimilarity(defaultSim);
    }
    return similarity;
}
Also used : ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) Similarity(org.apache.lucene.search.similarities.Similarity) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) SolrException(org.apache.solr.common.SolrException) FieldType(org.apache.solr.schema.FieldType)

Example 40 with SolrException

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

the class ClusteringComponent method inform.

@SuppressWarnings("unchecked")
@Override
public void inform(SolrCore core) {
    if (initParams != null) {
        log.info("Initializing Clustering Engines");
        // Our target list of engines, split into search-results and document clustering.
        SolrResourceLoader loader = core.getResourceLoader();
        for (Map.Entry<String, Object> entry : initParams) {
            if ("engine".equals(entry.getKey())) {
                NamedList<Object> engineInitParams = (NamedList<Object>) entry.getValue();
                Boolean optional = engineInitParams.getBooleanArg("optional");
                optional = (optional == null ? Boolean.FALSE : optional);
                String engineClassName = StringUtils.defaultIfBlank((String) engineInitParams.get("classname"), CarrotClusteringEngine.class.getName());
                // Instantiate the clustering engine and split to appropriate map. 
                final ClusteringEngine engine = loader.newInstance(engineClassName, ClusteringEngine.class);
                final String name = StringUtils.defaultIfBlank(engine.init(engineInitParams, core), "");
                if (!engine.isAvailable()) {
                    if (optional) {
                        log.info("Optional clustering engine not available: " + name);
                    } else {
                        throw new SolrException(ErrorCode.SERVER_ERROR, "A required clustering engine failed to initialize, check the logs: " + name);
                    }
                }
                final ClusteringEngine previousEntry;
                if (engine instanceof SearchClusteringEngine) {
                    previousEntry = searchClusteringEngines.put(name, (SearchClusteringEngine) engine);
                } else if (engine instanceof DocumentClusteringEngine) {
                    previousEntry = documentClusteringEngines.put(name, (DocumentClusteringEngine) engine);
                } else {
                    log.warn("Unknown type of a clustering engine for class: " + engineClassName);
                    continue;
                }
                if (previousEntry != null) {
                    log.warn("Duplicate clustering engine component named '" + name + "'.");
                }
            }
        }
        // Set up the default engine key for both types of engines.
        setupDefaultEngine("search results clustering", searchClusteringEngines);
        setupDefaultEngine("document clustering", documentClusteringEngines);
        log.info("Finished Initializing Clustering Engines");
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) CarrotClusteringEngine(org.apache.solr.handler.clustering.carrot2.CarrotClusteringEngine) CarrotClusteringEngine(org.apache.solr.handler.clustering.carrot2.CarrotClusteringEngine) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException)

Aggregations

SolrException (org.apache.solr.common.SolrException)617 IOException (java.io.IOException)172 ArrayList (java.util.ArrayList)100 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)80 NamedList (org.apache.solr.common.util.NamedList)79 HashMap (java.util.HashMap)75 Map (java.util.Map)70 SolrParams (org.apache.solr.common.params.SolrParams)64 KeeperException (org.apache.zookeeper.KeeperException)60 Test (org.junit.Test)55 Replica (org.apache.solr.common.cloud.Replica)48 Slice (org.apache.solr.common.cloud.Slice)45 DocCollection (org.apache.solr.common.cloud.DocCollection)41 SolrInputDocument (org.apache.solr.common.SolrInputDocument)39 SchemaField (org.apache.solr.schema.SchemaField)39 List (java.util.List)38 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)38 SolrServerException (org.apache.solr.client.solrj.SolrServerException)37 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)34 SolrCore (org.apache.solr.core.SolrCore)33