Search in sources :

Example 76 with FieldType

use of org.apache.solr.schema.FieldType in project lucene-solr by apache.

the class BlendedInfixLookupFactory method create.

@Override
public Lookup create(NamedList params, SolrCore core) {
    // mandatory parameter
    Object fieldTypeName = params.get(QUERY_ANALYZER);
    if (fieldTypeName == null) {
        throw new IllegalArgumentException("Error in configuration: " + QUERY_ANALYZER + " parameter is mandatory");
    }
    FieldType ft = core.getLatestSchema().getFieldTypeByName(fieldTypeName.toString());
    if (ft == null) {
        throw new IllegalArgumentException("Error in configuration: " + fieldTypeName.toString() + " is not defined in the schema");
    }
    Analyzer indexAnalyzer = ft.getIndexAnalyzer();
    Analyzer queryAnalyzer = ft.getQueryAnalyzer();
    // optional parameters
    String indexPath = params.get(INDEX_PATH) != null ? params.get(INDEX_PATH).toString() : DEFAULT_INDEX_PATH;
    if (new File(indexPath).isAbsolute() == false) {
        indexPath = core.getDataDir() + File.separator + indexPath;
    }
    int minPrefixChars = params.get(MIN_PREFIX_CHARS) != null ? Integer.parseInt(params.get(MIN_PREFIX_CHARS).toString()) : AnalyzingInfixSuggester.DEFAULT_MIN_PREFIX_CHARS;
    boolean allTermsRequired = params.get(ALL_TERMS_REQUIRED) != null ? Boolean.getBoolean(params.get(ALL_TERMS_REQUIRED).toString()) : AnalyzingInfixSuggester.DEFAULT_ALL_TERMS_REQUIRED;
    boolean highlight = params.get(HIGHLIGHT) != null ? Boolean.getBoolean(params.get(HIGHLIGHT).toString()) : AnalyzingInfixSuggester.DEFAULT_HIGHLIGHT;
    BlenderType blenderType = getBlenderType(params.get(BLENDER_TYPE));
    int numFactor = params.get(NUM_FACTOR) != null ? Integer.parseInt(params.get(NUM_FACTOR).toString()) : BlendedInfixSuggester.DEFAULT_NUM_FACTOR;
    Double exponent = params.get(EXPONENT) == null ? null : Double.valueOf(params.get(EXPONENT).toString());
    try {
        return new BlendedInfixSuggester(FSDirectory.open(new File(indexPath).toPath()), indexAnalyzer, queryAnalyzer, minPrefixChars, blenderType, numFactor, exponent, true, allTermsRequired, highlight) {

            @Override
            public List<LookupResult> lookup(CharSequence key, Set<BytesRef> contexts, int num, boolean allTermsRequired, boolean doHighlight) throws IOException {
                List<LookupResult> res = super.lookup(key, contexts, num, allTermsRequired, doHighlight);
                if (doHighlight) {
                    List<LookupResult> res2 = new ArrayList<>();
                    for (LookupResult hit : res) {
                        res2.add(new LookupResult(hit.highlightKey.toString(), hit.highlightKey, hit.value, hit.payload, hit.contexts));
                    }
                    res = res2;
                }
                return res;
            }
        };
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Analyzer(org.apache.lucene.analysis.Analyzer) FieldType(org.apache.solr.schema.FieldType) BlenderType(org.apache.lucene.search.suggest.analyzing.BlendedInfixSuggester.BlenderType) BlendedInfixSuggester(org.apache.lucene.search.suggest.analyzing.BlendedInfixSuggester) File(java.io.File)

Example 77 with FieldType

use of org.apache.solr.schema.FieldType in project lucene-solr by apache.

the class FuzzyLookupFactory method create.

@Override
public Lookup create(NamedList params, SolrCore core) {
    // mandatory parameter
    Object fieldTypeName = params.get(AnalyzingLookupFactory.QUERY_ANALYZER);
    if (fieldTypeName == null) {
        throw new IllegalArgumentException("Error in configuration: " + AnalyzingLookupFactory.QUERY_ANALYZER + " parameter is mandatory");
    }
    // retrieve index and query analyzers for the field
    FieldType ft = core.getLatestSchema().getFieldTypeByName(fieldTypeName.toString());
    if (ft == null) {
        throw new IllegalArgumentException("Error in configuration: " + fieldTypeName.toString() + " is not defined in the schema");
    }
    Analyzer indexAnalyzer = ft.getIndexAnalyzer();
    Analyzer queryAnalyzer = ft.getQueryAnalyzer();
    // optional parameters
    boolean exactMatchFirst = (params.get(AnalyzingLookupFactory.EXACT_MATCH_FIRST) != null) ? Boolean.valueOf(params.get(AnalyzingLookupFactory.EXACT_MATCH_FIRST).toString()) : true;
    boolean preserveSep = (params.get(AnalyzingLookupFactory.PRESERVE_SEP) != null) ? Boolean.valueOf(params.get(AnalyzingLookupFactory.PRESERVE_SEP).toString()) : true;
    int options = 0;
    if (exactMatchFirst) {
        options |= FuzzySuggester.EXACT_FIRST;
    }
    if (preserveSep) {
        options |= FuzzySuggester.PRESERVE_SEP;
    }
    int maxSurfaceFormsPerAnalyzedForm = (params.get(AnalyzingLookupFactory.MAX_SURFACE_FORMS) != null) ? Integer.parseInt(params.get(AnalyzingLookupFactory.MAX_SURFACE_FORMS).toString()) : 256;
    int maxGraphExpansions = (params.get(AnalyzingLookupFactory.MAX_EXPANSIONS) != null) ? Integer.parseInt(params.get(AnalyzingLookupFactory.MAX_EXPANSIONS).toString()) : -1;
    boolean preservePositionIncrements = params.get(AnalyzingLookupFactory.PRESERVE_POSITION_INCREMENTS) != null ? Boolean.valueOf(params.get(AnalyzingLookupFactory.PRESERVE_POSITION_INCREMENTS).toString()) : false;
    int maxEdits = (params.get(MAX_EDITS) != null) ? Integer.parseInt(params.get(MAX_EDITS).toString()) : FuzzySuggester.DEFAULT_MAX_EDITS;
    boolean transpositions = (params.get(TRANSPOSITIONS) != null) ? Boolean.parseBoolean(params.get(TRANSPOSITIONS).toString()) : FuzzySuggester.DEFAULT_TRANSPOSITIONS;
    int nonFuzzyPrefix = (params.get(NON_FUZZY_PREFIX) != null) ? Integer.parseInt(params.get(NON_FUZZY_PREFIX).toString()) : FuzzySuggester.DEFAULT_NON_FUZZY_PREFIX;
    int minFuzzyLength = (params.get(MIN_FUZZY_LENGTH) != null) ? Integer.parseInt(params.get(MIN_FUZZY_LENGTH).toString()) : FuzzySuggester.DEFAULT_MIN_FUZZY_LENGTH;
    boolean unicodeAware = (params.get(UNICODE_AWARE) != null) ? Boolean.valueOf(params.get(UNICODE_AWARE).toString()) : FuzzySuggester.DEFAULT_UNICODE_AWARE;
    return new FuzzySuggester(getTempDir(), "suggester", indexAnalyzer, queryAnalyzer, options, maxSurfaceFormsPerAnalyzedForm, maxGraphExpansions, preservePositionIncrements, maxEdits, transpositions, nonFuzzyPrefix, minFuzzyLength, unicodeAware);
}
Also used : FuzzySuggester(org.apache.lucene.search.suggest.analyzing.FuzzySuggester) Analyzer(org.apache.lucene.analysis.Analyzer) FieldType(org.apache.solr.schema.FieldType)

Example 78 with FieldType

use of org.apache.solr.schema.FieldType in project lucene-solr by apache.

the class AnalyzingLookupFactory method create.

@Override
public Lookup create(NamedList params, SolrCore core) {
    // mandatory parameter
    Object fieldTypeName = params.get(QUERY_ANALYZER);
    if (fieldTypeName == null) {
        throw new IllegalArgumentException("Error in configuration: " + QUERY_ANALYZER + " parameter is mandatory");
    }
    FieldType ft = core.getLatestSchema().getFieldTypeByName(fieldTypeName.toString());
    if (ft == null) {
        throw new IllegalArgumentException("Error in configuration: " + fieldTypeName.toString() + " is not defined in the schema");
    }
    Analyzer indexAnalyzer = ft.getIndexAnalyzer();
    Analyzer queryAnalyzer = ft.getQueryAnalyzer();
    // optional parameters
    boolean exactMatchFirst = params.get(EXACT_MATCH_FIRST) != null ? Boolean.valueOf(params.get(EXACT_MATCH_FIRST).toString()) : true;
    boolean preserveSep = params.get(PRESERVE_SEP) != null ? Boolean.valueOf(params.get(PRESERVE_SEP).toString()) : true;
    int flags = 0;
    if (exactMatchFirst) {
        flags |= AnalyzingSuggester.EXACT_FIRST;
    }
    if (preserveSep) {
        flags |= AnalyzingSuggester.PRESERVE_SEP;
    }
    int maxSurfaceFormsPerAnalyzedForm = params.get(MAX_SURFACE_FORMS) != null ? Integer.parseInt(params.get(MAX_SURFACE_FORMS).toString()) : 256;
    int maxGraphExpansions = params.get(MAX_EXPANSIONS) != null ? Integer.parseInt(params.get(MAX_EXPANSIONS).toString()) : -1;
    boolean preservePositionIncrements = params.get(PRESERVE_POSITION_INCREMENTS) != null ? Boolean.valueOf(params.get(PRESERVE_POSITION_INCREMENTS).toString()) : false;
    return new AnalyzingSuggester(getTempDir(), "suggester", indexAnalyzer, queryAnalyzer, flags, maxSurfaceFormsPerAnalyzedForm, maxGraphExpansions, preservePositionIncrements);
}
Also used : Analyzer(org.apache.lucene.analysis.Analyzer) AnalyzingSuggester(org.apache.lucene.search.suggest.analyzing.AnalyzingSuggester) FieldType(org.apache.solr.schema.FieldType)

Example 79 with FieldType

use of org.apache.solr.schema.FieldType in project lucene-solr by apache.

the class FieldQParserPlugin method createParser.

@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
    return new QParser(qstr, localParams, params, req) {

        @Override
        public Query parse() {
            String field = localParams.get(QueryParsing.F);
            String queryText = localParams.get(QueryParsing.V);
            SchemaField sf = req.getSchema().getField(field);
            FieldType ft = sf.getType();
            return ft.getFieldQuery(this, sf, queryText);
        }
    };
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) FieldType(org.apache.solr.schema.FieldType)

Example 80 with FieldType

use of org.apache.solr.schema.FieldType in project lucene-solr by apache.

the class GraphTermsQParserPlugin method createParser.

@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
    return new QParser(qstr, localParams, params, req) {

        @Override
        public Query parse() throws SyntaxError {
            String fname = localParams.get(QueryParsing.F);
            FieldType ft = req.getSchema().getFieldTypeNoEx(fname);
            int maxDocFreq = localParams.getInt("maxDocFreq", Integer.MAX_VALUE);
            //never null
            String qstr = localParams.get(QueryParsing.V);
            if (qstr.length() == 0) {
                return new MatchNoDocsQuery();
            }
            final String[] splitVals = qstr.split(",");
            Term[] terms = new Term[splitVals.length];
            BytesRefBuilder term = new BytesRefBuilder();
            for (int i = 0; i < splitVals.length; i++) {
                String stringVal = splitVals[i].trim();
                if (ft != null) {
                    ft.readableToIndexed(stringVal, term);
                } else {
                    term.copyChars(stringVal);
                }
                BytesRef ref = term.toBytesRef();
                terms[i] = new Term(fname, ref);
            }
            ArrayUtil.timSort(terms);
            return new ConstantScoreQuery(new GraphTermsQuery(fname, terms, maxDocFreq));
        }
    };
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Term(org.apache.lucene.index.Term) BytesRef(org.apache.lucene.util.BytesRef) FieldType(org.apache.solr.schema.FieldType)

Aggregations

FieldType (org.apache.solr.schema.FieldType)93 SchemaField (org.apache.solr.schema.SchemaField)37 SolrException (org.apache.solr.common.SolrException)29 ArrayList (java.util.ArrayList)23 BytesRef (org.apache.lucene.util.BytesRef)23 NamedList (org.apache.solr.common.util.NamedList)23 IOException (java.io.IOException)18 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)15 IndexSchema (org.apache.solr.schema.IndexSchema)14 Query (org.apache.lucene.search.Query)13 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)13 Analyzer (org.apache.lucene.analysis.Analyzer)12 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)10 CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)10 StrField (org.apache.solr.schema.StrField)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Map (java.util.Map)7 DocIterator (org.apache.solr.search.DocIterator)7 DocList (org.apache.solr.search.DocList)7