Search in sources :

Example 1 with Config

use of org.apache.solr.core.Config in project lucene-solr by apache.

the class QueryElevationComponent method inform.

@Override
public void inform(SolrCore core) {
    IndexSchema schema = core.getLatestSchema();
    String a = initArgs.get(FIELD_TYPE);
    if (a != null) {
        FieldType ft = schema.getFieldTypes().get(a);
        if (ft == null) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown FieldType: '" + a + "' used in QueryElevationComponent");
        }
        analyzer = ft.getQueryAnalyzer();
    }
    SchemaField sf = schema.getUniqueKeyField();
    if (sf == null) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "QueryElevationComponent requires the schema to have a uniqueKeyField.");
    }
    idSchemaFT = sf.getType();
    idField = sf.getName();
    //register the EditorialMarkerFactory
    String excludeName = initArgs.get(QueryElevationParams.EXCLUDE_MARKER_FIELD_NAME, "excluded");
    if (excludeName == null || excludeName.equals("") == true) {
        excludeName = "excluded";
    }
    ExcludedMarkerFactory excludedMarkerFactory = new ExcludedMarkerFactory();
    core.addTransformerFactory(excludeName, excludedMarkerFactory);
    ElevatedMarkerFactory elevatedMarkerFactory = new ElevatedMarkerFactory();
    String markerName = initArgs.get(QueryElevationParams.EDITORIAL_MARKER_FIELD_NAME, "elevated");
    if (markerName == null || markerName.equals("") == true) {
        markerName = "elevated";
    }
    core.addTransformerFactory(markerName, elevatedMarkerFactory);
    forceElevation = initArgs.getBool(QueryElevationParams.FORCE_ELEVATION, forceElevation);
    try {
        synchronized (elevationCache) {
            elevationCache.clear();
            String f = initArgs.get(CONFIG_FILE);
            if (f == null) {
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "QueryElevationComponent must specify argument: '" + CONFIG_FILE + "' -- path to elevate.xml");
            }
            boolean exists = false;
            // check if using ZooKeeper
            ZkController zkController = core.getCoreContainer().getZkController();
            if (zkController != null) {
                // TODO : shouldn't have to keep reading the config name when it has been read before
                exists = zkController.configFileExists(zkController.getZkStateReader().readConfigName(core.getCoreDescriptor().getCloudDescriptor().getCollectionName()), f);
            } else {
                File fC = new File(core.getResourceLoader().getConfigDir(), f);
                File fD = new File(core.getDataDir(), f);
                if (fC.exists() == fD.exists()) {
                    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "QueryElevationComponent missing config file: '" + f + "\n" + "either: " + fC.getAbsolutePath() + " or " + fD.getAbsolutePath() + " must exist, but not both.");
                }
                if (fC.exists()) {
                    exists = true;
                    log.info("Loading QueryElevation from: " + fC.getAbsolutePath());
                    Config cfg = new Config(core.getResourceLoader(), f);
                    elevationCache.put(null, loadElevationMap(cfg));
                }
            }
            //in other words, we think this is in the data dir, not the conf dir
            if (!exists) {
                // preload the first data
                RefCounted<SolrIndexSearcher> searchHolder = null;
                try {
                    searchHolder = core.getNewestSearcher(false);
                    IndexReader reader = searchHolder.get().getIndexReader();
                    getElevationMap(reader, core);
                } finally {
                    if (searchHolder != null)
                        searchHolder.decref();
                }
            }
        }
    } catch (Exception ex) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error initializing QueryElevationComponent.", ex);
    }
}
Also used : ExcludedMarkerFactory(org.apache.solr.response.transform.ExcludedMarkerFactory) Config(org.apache.solr.core.Config) ElevatedMarkerFactory(org.apache.solr.response.transform.ElevatedMarkerFactory) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) XPathExpressionException(javax.xml.xpath.XPathExpressionException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) ZkController(org.apache.solr.cloud.ZkController) IndexReader(org.apache.lucene.index.IndexReader) IndexSchema(org.apache.solr.schema.IndexSchema) VersionedFile(org.apache.solr.util.VersionedFile) File(java.io.File) SolrException(org.apache.solr.common.SolrException)

Example 2 with Config

use of org.apache.solr.core.Config in project lucene-solr by apache.

the class IndexSchema method readSchema.

protected void readSchema(InputSource is) {
    try {
        // pass the config resource loader to avoid building an empty one for no reason:
        // in the current case though, the stream is valid so we wont load the resource by name
        Config schemaConf = new Config(loader, SCHEMA, is, SLASH + SCHEMA + SLASH);
        Document document = schemaConf.getDocument();
        final XPath xpath = schemaConf.getXPath();
        String expression = stepsToPath(SCHEMA, AT + NAME);
        Node nd = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
        String coreName = getCoreName("null");
        StringBuilder sb = new StringBuilder();
        // Another case where the initialization from the test harness is different than the "real world"
        sb.append("[");
        sb.append(coreName);
        sb.append("] ");
        if (nd == null) {
            sb.append("schema has no name!");
            log.warn(sb.toString());
        } else {
            name = nd.getNodeValue();
            sb.append("Schema ");
            sb.append(NAME);
            sb.append("=");
            sb.append(name);
            log.info(sb.toString());
        }
        //                      /schema/@version
        expression = stepsToPath(SCHEMA, AT + VERSION);
        version = schemaConf.getFloat(expression, 1.0f);
        // load the Field Types
        final FieldTypePluginLoader typeLoader = new FieldTypePluginLoader(this, fieldTypes, schemaAware);
        expression = getFieldTypeXPathExpressions();
        NodeList nodes = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);
        typeLoader.load(loader, nodes);
        // load the fields
        Map<String, Boolean> explicitRequiredProp = loadFields(document, xpath);
        //   /schema/similarity
        expression = stepsToPath(SCHEMA, SIMILARITY);
        Node node = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
        similarityFactory = readSimilarity(loader, node);
        if (similarityFactory == null) {
            final boolean modernSim = getDefaultLuceneMatchVersion().onOrAfter(Version.LUCENE_6_0_0);
            final Class simClass = modernSim ? SchemaSimilarityFactory.class : ClassicSimilarityFactory.class;
            // use the loader to ensure proper SolrCoreAware handling
            similarityFactory = loader.newInstance(simClass.getName(), SimilarityFactory.class);
            similarityFactory.init(new ModifiableSolrParams());
        } else {
            isExplicitSimilarity = true;
        }
        if (!(similarityFactory instanceof SolrCoreAware)) {
            // then we are responsible for erroring if a field type is trying to specify a sim.
            for (FieldType ft : fieldTypes.values()) {
                if (null != ft.getSimilarity()) {
                    String msg = "FieldType '" + ft.getTypeName() + "' is configured with a similarity, but the global similarity does not support it: " + similarityFactory.getClass();
                    log.error(msg);
                    throw new SolrException(ErrorCode.SERVER_ERROR, msg);
                }
            }
        }
        //                      /schema/defaultSearchField/text()
        expression = stepsToPath(SCHEMA, "defaultSearchField", TEXT_FUNCTION);
        node = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
        if (node != null) {
            throw new SolrException(ErrorCode.SERVER_ERROR, "Setting defaultSearchField in schema not supported since Solr 7");
        }
        //                      /schema/solrQueryParser/@defaultOperator
        expression = stepsToPath(SCHEMA, "solrQueryParser", AT + "defaultOperator");
        node = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
        if (node != null) {
            throw new SolrException(ErrorCode.SERVER_ERROR, "Setting default operator in schema (solrQueryParser/@defaultOperator) not supported");
        }
        //                      /schema/uniqueKey/text()
        expression = stepsToPath(SCHEMA, UNIQUE_KEY, TEXT_FUNCTION);
        node = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
        if (node == null) {
            log.warn("no " + UNIQUE_KEY + " specified in schema.");
        } else {
            uniqueKeyField = getIndexedField(node.getNodeValue().trim());
            if (null != uniqueKeyField.getDefaultValue()) {
                String msg = UNIQUE_KEY + " field (" + uniqueKeyFieldName + ") can not be configured with a default value (" + uniqueKeyField.getDefaultValue() + ")";
                log.error(msg);
                throw new SolrException(ErrorCode.SERVER_ERROR, msg);
            }
            if (!uniqueKeyField.stored()) {
                log.warn(UNIQUE_KEY + " is not stored - distributed search and MoreLikeThis will not work");
            }
            if (uniqueKeyField.multiValued()) {
                String msg = UNIQUE_KEY + " field (" + uniqueKeyFieldName + ") can not be configured to be multivalued";
                log.error(msg);
                throw new SolrException(ErrorCode.SERVER_ERROR, msg);
            }
            uniqueKeyFieldName = uniqueKeyField.getName();
            uniqueKeyFieldType = uniqueKeyField.getType();
            // Unless the uniqueKeyField is marked 'required=false' then make sure it exists
            if (Boolean.FALSE != explicitRequiredProp.get(uniqueKeyFieldName)) {
                uniqueKeyField.required = true;
                requiredFields.add(uniqueKeyField);
            }
        }
        /////////////// parse out copyField commands ///////////////
        // Map<String,ArrayList<SchemaField>> cfields = new HashMap<String,ArrayList<SchemaField>>();
        // expression = "/schema/copyField";
        dynamicCopyFields = new DynamicCopy[] {};
        loadCopyFields(document, xpath);
        postReadInform();
    } catch (SolrException e) {
        throw new SolrException(ErrorCode.getErrorCode(e.code()), "Can't load schema " + loader.resourceLocation(resourceName) + ": " + e.getMessage(), e);
    } catch (Exception e) {
        // unexpected exception...
        throw new SolrException(ErrorCode.SERVER_ERROR, "Can't load schema " + loader.resourceLocation(resourceName) + ": " + e.getMessage(), e);
    }
    // create the field analyzers
    refreshAnalyzers();
    log.info("Loaded schema {}/{} with uniqueid field {}", name, version, uniqueKeyFieldName);
}
Also used : XPath(javax.xml.xpath.XPath) SolrConfig(org.apache.solr.core.SolrConfig) Config(org.apache.solr.core.Config) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) SchemaSimilarityFactory(org.apache.solr.search.similarities.SchemaSimilarityFactory) ClassicSimilarityFactory(org.apache.solr.search.similarities.ClassicSimilarityFactory) Document(org.w3c.dom.Document) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) XPathExpressionException(javax.xml.xpath.XPathExpressionException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) SolrCoreAware(org.apache.solr.util.plugin.SolrCoreAware) SolrException(org.apache.solr.common.SolrException)

Example 3 with Config

use of org.apache.solr.core.Config in project lucene-solr by apache.

the class QueryElevationComponent method getElevationMap.

//get the elevation map from the data dir
Map<String, ElevationObj> getElevationMap(IndexReader reader, SolrCore core) throws Exception {
    synchronized (elevationCache) {
        Map<String, ElevationObj> map = elevationCache.get(null);
        if (map != null)
            return map;
        map = elevationCache.get(reader);
        if (map == null) {
            String f = initArgs.get(CONFIG_FILE);
            if (f == null) {
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "QueryElevationComponent must specify argument: " + CONFIG_FILE);
            }
            log.info("Loading QueryElevation from data dir: " + f);
            Config cfg;
            ZkController zkController = core.getCoreContainer().getZkController();
            if (zkController != null) {
                cfg = new Config(core.getResourceLoader(), f, null, null);
            } else {
                InputStream is = VersionedFile.getLatestFile(core.getDataDir(), f);
                cfg = new Config(core.getResourceLoader(), f, new InputSource(is), null);
            }
            map = loadElevationMap(cfg);
            elevationCache.put(reader, map);
        }
        return map;
    }
}
Also used : InputSource(org.xml.sax.InputSource) Config(org.apache.solr.core.Config) ZkController(org.apache.solr.cloud.ZkController) InputStream(java.io.InputStream) SolrException(org.apache.solr.common.SolrException)

Aggregations

SolrException (org.apache.solr.common.SolrException)3 Config (org.apache.solr.core.Config)3 IOException (java.io.IOException)2 XPathExpressionException (javax.xml.xpath.XPathExpressionException)2 ZkController (org.apache.solr.cloud.ZkController)2 File (java.io.File)1 InputStream (java.io.InputStream)1 XPath (javax.xml.xpath.XPath)1 IndexReader (org.apache.lucene.index.IndexReader)1 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)1 SolrConfig (org.apache.solr.core.SolrConfig)1 ElevatedMarkerFactory (org.apache.solr.response.transform.ElevatedMarkerFactory)1 ExcludedMarkerFactory (org.apache.solr.response.transform.ExcludedMarkerFactory)1 FieldType (org.apache.solr.schema.FieldType)1 IndexSchema (org.apache.solr.schema.IndexSchema)1 SchemaField (org.apache.solr.schema.SchemaField)1 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)1 ClassicSimilarityFactory (org.apache.solr.search.similarities.ClassicSimilarityFactory)1 SchemaSimilarityFactory (org.apache.solr.search.similarities.SchemaSimilarityFactory)1 VersionedFile (org.apache.solr.util.VersionedFile)1