Package org.apache.jackrabbit.oak.plugins.index.solr.http

Source Code of org.apache.jackrabbit.oak.plugins.index.solr.http.RemoteSolrConfigurationProvider

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jackrabbit.oak.plugins.index.solr.http;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.plugins.index.solr.CommitPolicy;
import org.apache.jackrabbit.oak.plugins.index.solr.OakSolrConfiguration;
import org.apache.jackrabbit.oak.plugins.index.solr.OakSolrConfigurationProvider;
import org.apache.jackrabbit.oak.spi.query.Filter;
import org.osgi.service.component.ComponentContext;

/**
* An {@link OakSolrConfigurationProvider} for the remote Solr server
* <p/>
* In this {@link OakSolrConfiguration} the 'path' related fields are taken from
* OSGi configuration while the other configuration just does nothing triggering
* the default behavior that properties are indexed by name.
* Possible extensions of this class may trigger type based property indexing / search.
*/
@Component
@Service(OakSolrConfigurationProvider.class)
public class RemoteSolrConfigurationProvider implements OakSolrConfigurationProvider {

    private static final String DEFAULT_DESC_FIELD = "path_des";
    private static final String DEFAULT_CHILD_FIELD = "path_child";
    private static final String DEFAULT_PARENT_FIELD = "path_anc";
    private static final String DEFAULT_PATH_FIELD = "path_exact";
    private static final String DEFAULT_CORE_NAME = "oak";

    @Property(value = DEFAULT_DESC_FIELD)
    private static final String PATH_DESCENDANTS_FIELD = "path.desc.field";

    @Property(value = DEFAULT_CHILD_FIELD)
    private static final String PATH_CHILDREN_FIELD = "path.child.field";

    @Property(value = DEFAULT_PARENT_FIELD)
    private static final String PATH_PARENT_FIELD = "path.parent.field";

    @Property(value = DEFAULT_PATH_FIELD)
    private static final String PATH_EXACT_FIELD = "path.exact.field";

    @Property(value = DEFAULT_CORE_NAME)
    private static final String CORE_NAME = "core.name";


    private String pathChildrenFieldName;
    private String pathParentFieldName;
    private String pathDescendantsFieldName;
    private String pathExactFieldName;
    private String coreName;

    private OakSolrConfiguration oakSolrConfiguration;

    public RemoteSolrConfigurationProvider() {
        this.pathChildrenFieldName = DEFAULT_CHILD_FIELD;
        this.pathDescendantsFieldName = DEFAULT_DESC_FIELD;
        this.pathExactFieldName = DEFAULT_PATH_FIELD;
        this.pathParentFieldName = DEFAULT_PARENT_FIELD;
        this.coreName = DEFAULT_CORE_NAME;
    }

    public RemoteSolrConfigurationProvider(String pathChildrenFieldName, String pathParentFieldName,
                                           String pathDescendantsFieldName, String pathExactFieldName,
                                           String coreName) {
        this.pathChildrenFieldName = pathChildrenFieldName;
        this.pathParentFieldName = pathParentFieldName;
        this.pathDescendantsFieldName = pathDescendantsFieldName;
        this.pathExactFieldName = pathExactFieldName;
        this.coreName = coreName;
    }

    protected void activate(ComponentContext componentContext) throws Exception {
        pathChildrenFieldName = String.valueOf(componentContext.getProperties().get(PATH_CHILDREN_FIELD));
        pathParentFieldName = String.valueOf(componentContext.getProperties().get(PATH_PARENT_FIELD));
        pathExactFieldName = String.valueOf(componentContext.getProperties().get(PATH_EXACT_FIELD));
        pathDescendantsFieldName = String.valueOf(componentContext.getProperties().get(PATH_DESCENDANTS_FIELD));
        coreName = String.valueOf(componentContext.getProperties().get(CORE_NAME));
    }

    @Override
    public OakSolrConfiguration getConfiguration() {
        if (oakSolrConfiguration == null) {
            oakSolrConfiguration = new OakSolrConfiguration() {
                @Override
                public String getFieldNameFor(Type<?> propertyType) {
                    if (Type.BINARIES.equals(propertyType) || Type.BINARY.equals(propertyType)) {
                        // TODO : use Tika / SolrCell here
                        return propertyType.toString()+"_bin";
                    }
                    return null;
                }

                @Override
                public String getPathField() {
                    return pathExactFieldName;
                }

                @Override
                public String getFieldForPathRestriction(Filter.PathRestriction pathRestriction) {
                    String fieldName = null;
                    switch (pathRestriction) {
                        case ALL_CHILDREN: {
                            fieldName = pathDescendantsFieldName;
                            break;
                        }
                        case DIRECT_CHILDREN: {
                            fieldName = pathChildrenFieldName;
                            break;
                        }
                        case EXACT: {
                            fieldName = pathExactFieldName;
                            break;
                        }
                        case PARENT: {
                            fieldName = pathParentFieldName;
                            break;
                        }

                    }
                    return fieldName;
                }

                @Override
                public String getFieldForPropertyRestriction(Filter.PropertyRestriction propertyRestriction) {
                    return null;
                }

                @Override
                public CommitPolicy getCommitPolicy() {
                    return CommitPolicy.SOFT;
                }

                 @Override
                public String getCoreName() {
                    return coreName;
                }
            };
        }
        return oakSolrConfiguration;
    }
}
TOP

Related Classes of org.apache.jackrabbit.oak.plugins.index.solr.http.RemoteSolrConfigurationProvider

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.