Example usage for org.apache.lucene.queries.function.valuesource FloatFieldSource FloatFieldSource

List of usage examples for org.apache.lucene.queries.function.valuesource FloatFieldSource FloatFieldSource

Introduction

In this page you can find the example usage for org.apache.lucene.queries.function.valuesource FloatFieldSource FloatFieldSource.

Prototype

public FloatFieldSource(String field) 

Source Link

Usage

From source file:edu.rpi.tw.linkipedia.search.query.WeightedQuery.java

License:Open Source License

public Query parse(String label, String[] contexts) throws ParseException {

    label = label.toLowerCase().trim();/* ww  w.ja v a  2s. c  om*/
    for (int i = 0; i < contexts.length; i++) {
        contexts[i] = contexts[i].toLowerCase().trim();
    }

    Query typeQuery = getTypeQuery(label);
    label = label.replaceAll("\\(.*\\)", "");
    if (debug)
        System.out.println("new label: " + label);
    BooleanQuery luceneQuery = new BooleanQuery();
    ArrayList<String> dictLabels = dict_exp.getExpandQueryString(label);
    //      ArrayList<String> temp = new ArrayList<String>( Arrays.asList(contexts));
    //      if(dictLabels != null){
    //      for(String dictLabel:dictLabels){
    //         temp.add(dictLabel);         
    //      }   
    //      }
    //      contexts = temp.toArray(new String[temp.size()]);
    ArrayList<String> weightedContext = assignContextWeight(label, contexts, contexts.length);

    //System.out.println(label+" "+weightedContext.toString());

    Query labelQuery = getLabelQuery(label);
    //Query labelQuery = getLabelSpanQuery(label);
    Query contentQuery = getContentQuery(label, weightedContext);
    Query relationQuery = getRelationQuery(label, weightedContext);

    //      if(relationQuery!=null)
    //         relationQuery.setBoost(100);

    if (typeQuery != null) {
        typeQuery.setBoost(typeBoost);
        luceneQuery.add(typeQuery, BooleanClause.Occur.SHOULD);
    }

    if (labelQuery != null) {
        //         if(relationQuery == null && labelBoost == 1){
        //            labelQuery.setBoost(10);
        //         }
        labelQuery.setBoost(labelBoost);
        luceneQuery.add(labelQuery, BooleanClause.Occur.MUST);
    }

    if (contentQuery != null) {
        contentQuery.setBoost(contentBoost);
        luceneQuery.add(contentQuery, BooleanClause.Occur.SHOULD);
    }

    if (relationQuery != null) {
        //relationQuery.setBoost(2);
        relationQuery.setBoost(relationBoost);
        luceneQuery.add(relationQuery, BooleanClause.Occur.SHOULD);
    }

    //      System.out.println("parse: labelBoost: "+labelBoost);
    //      System.out.println("parse: contentBoost: "+contentBoost);
    //      System.out.println("parse: relationBoost: "+relationBoost);
    //      System.out.println("parse: typeBoost: "+typeBoost);
    //      System.out.println("parse: defaultBoost: "+defaultBoost);

    //      Query dictQuery = getDictExpandQuery(label, weightedContext);
    //      if(dictQuery != null){
    //         luceneQuery.add(dictQuery, BooleanClause.Occur.SHOULD);   
    //      }
    FunctionQuery boostQuery = new FunctionQuery(new FloatFieldSource("boost"));
    boostQuery.setBoost(defaultBoost);

    finalQuery = new CustomScoreQuery(luceneQuery, boostQuery);

    return finalQuery;
}

From source file:edu.rpi.tw.linkipedia.search.query.WeightedQuery.java

License:Open Source License

public Query getFuzzyQuery(String label, String[] contexts) throws ParseException {
    label = label.toLowerCase().trim();/*from   w w w .  j  a  v  a2s .co m*/
    for (int i = 0; i < contexts.length; i++) {
        contexts[i] = contexts[i].toLowerCase().trim();
    }

    Query typeQuery = getTypeQuery(label);
    label = label.replaceAll("\\(.*\\)", "");
    //System.out.println("new label: "+label);
    BooleanQuery luceneQuery = new BooleanQuery();
    ArrayList<String> weightedContext = assignContextWeight(label, contexts, contexts.length);

    //System.out.println(label+" "+weightedContext.toString());
    Term labelTerm = new Term("label", label);
    FuzzyQuery fuzzyLabelQuery = new FuzzyQuery(labelTerm);
    //Query labelQuery = getLabelQuery(label);   
    Query contentQuery = getContentQuery(label, weightedContext);
    Query relationQuery = getRelationQuery(label, weightedContext);

    //      if(relationQuery!=null)
    //         relationQuery.setBoost(100);

    if (typeQuery != null) {
        //typeQuery.setBoost(15);
        luceneQuery.add(typeQuery, BooleanClause.Occur.MUST);
    }

    if (fuzzyLabelQuery != null) {
        //         if(relationQuery == null)
        //            labelQuery.setBoost(10);
        luceneQuery.add(fuzzyLabelQuery, BooleanClause.Occur.MUST);
    }

    if (contentQuery != null) {
        luceneQuery.add(contentQuery, BooleanClause.Occur.SHOULD);
    }

    if (relationQuery != null) {
        //relationQuery.setBoost(2);
        luceneQuery.add(relationQuery, BooleanClause.Occur.SHOULD);
    }

    FunctionQuery boostQuery = new FunctionQuery(new FloatFieldSource("boost"));

    finalQuery = new CustomScoreQuery(luceneQuery, boostQuery);

    return finalQuery;
}

From source file:edu.rpi.tw.linkipedia.search.query.WeightedQueryFilterBackup.java

License:Open Source License

public Query parse(String label, String[] contexts) throws ParseException {

    label = label.toLowerCase().trim();/*  w  w w. ja v  a 2 s .c om*/
    for (int i = 0; i < contexts.length; i++) {
        contexts[i] = contexts[i].toLowerCase().trim();
    }

    CachingWrapperFilter typeFilter = getTypeFilter(label);
    label = label.replaceAll("\\(.*\\)", "");
    //System.out.println("new label: "+label);
    BooleanQuery luceneQuery = new BooleanQuery();
    ArrayList<String> weightedContext = assignContextWeight(label, contexts, contexts.length);

    //System.out.println(label+" "+weightedContext.toString());

    Query labelQuery = getLabelQuery(label);
    Query contentQuery = getContentQuery(label, weightedContext);
    Query relationQuery = getRelationQuery(label, weightedContext);

    //      if(relationQuery!=null)
    //         relationQuery.setBoost(100);

    if (labelQuery != null) {

        luceneQuery.add(labelQuery, BooleanClause.Occur.MUST);
    }

    FunctionQuery boostQuery = new FunctionQuery(new FloatFieldSource("boost"));

    if (relationQuery == null && labelQuery != null) {
        labelQuery.setBoost(10);
        boostQuery.setBoost(5);
    }

    if (contentQuery != null && relationQuery != null) {
        luceneQuery.add(contentQuery, BooleanClause.Occur.SHOULD);
    }

    if (relationQuery != null) {
        //relationQuery.setBoost(2);
        luceneQuery.add(relationQuery, BooleanClause.Occur.SHOULD);
    }

    Query query = new CustomScoreQuery(luceneQuery, boostQuery);

    if (typeFilter != null) {
        finalQuery = new FilteredQuery(query, typeFilter);
        return finalQuery;
    }
    return query;

}

From source file:edu.rpi.tw.linkipedia.search.query.WeightedQueryFilterBackup.java

License:Open Source License

public Query getFuzzyQuery(String label, String[] contexts) throws ParseException {
    label = label.toLowerCase().trim();/*from w w  w.  jav a2s .c  o m*/

    for (int i = 0; i < contexts.length; i++) {
        contexts[i] = contexts[i].toLowerCase().trim();
    }

    CachingWrapperFilter typeFilter = getTypeFilter(label);
    label = label.replaceAll("\\(.*\\)", "");

    ArrayList<String> weightedContext = assignContextWeight(label, contexts, contexts.length);

    Term labelTerm = new Term("label", label);
    FuzzyQuery fuzzyLabelQuery = new FuzzyQuery(labelTerm);
    BooleanQuery luceneQuery = new BooleanQuery();

    if (fuzzyLabelQuery != null) {

        luceneQuery.add(fuzzyLabelQuery, BooleanClause.Occur.MUST);
    }
    Query contentQuery = getContentQuery(label, weightedContext);
    Query relationQuery = getRelationQuery(label, weightedContext);

    FunctionQuery boostQuery = new FunctionQuery(new FloatFieldSource("boost"));
    if (relationQuery == null && fuzzyLabelQuery != null) {
        fuzzyLabelQuery.setBoost(5);
        boostQuery.setBoost(5);
    }

    if (contentQuery != null && relationQuery != null) {
        luceneQuery.add(contentQuery, BooleanClause.Occur.SHOULD);
    }

    if (relationQuery != null) {
        //relationQuery.setBoost(2);
        luceneQuery.add(relationQuery, BooleanClause.Occur.SHOULD);
    }

    Query query = new CustomScoreQuery(luceneQuery, boostQuery);

    if (typeFilter != null) {
        finalQuery = new FilteredQuery(query, typeFilter);
        return finalQuery;
    }
    return query;
}

From source file:edu.rpi.tw.linkipedia.search.searching.SurfaceFormSearcher.java

License:Open Source License

public ArrayList<Annotation> mysearch(String query) {
    TopScoreDocCollector collector = TopScoreDocCollector.create(10, false);
    results = new ArrayList<Annotation>();
    try {/*from ww  w  .  ja  v a  2s .com*/
        BooleanQuery luceneQuery = new BooleanQuery();
        Term term = new Term("label", query);
        //TermQuery termQuery = new TermQuery(term);
        PayloadTermQuery ptq = new PayloadTermQuery(term, payloadFunction);
        ptq.setBoost(10);
        Analyzer analyzer = DefaultAnalyzer.getAnalyzer();
        QueryParser parser = new QueryParser(Version.LUCENE_47, "analyzedLabel", analyzer);
        Query parsedQuery = parser.parse(query);

        //luceneQuery.add(termQuery, BooleanClause.Occur.SHOULD);
        luceneQuery.add(ptq, BooleanClause.Occur.SHOULD);
        luceneQuery.add(parsedQuery, BooleanClause.Occur.SHOULD);
        FunctionQuery boostQuery = new FunctionQuery(new FloatFieldSource("boost"));
        //boostQuery.setBoost(100);
        Query finalQuery = new CustomScoreQuery(luceneQuery, boostQuery);
        System.out.println(finalQuery);
        searcher.search(finalQuery, collector);
    } catch (Exception e) {
        e.printStackTrace();
    }
    ScoreDoc[] hits = collector.topDocs().scoreDocs;
    int numTotalHits = collector.getTotalHits();

    int start = 0;
    int end = Math.min(numTotalHits, 10);

    for (int i = start; i < end; i++) {
        String thisResult = "";
        Document doc = null;
        try {
            doc = searcher.doc(hits[i].doc);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String url = doc.get("url");
        thisResult += url;//+" "+hits[i].score;      
        String[] triples = doc.getValues("triple");
        //         for(String label:labels){
        //            thisResult += label+"\n";   
        //         }
        Annotation annotation = new Annotation(query, url, hits[i].score);
        annotation.setTriples(new ArrayList<String>(Arrays.asList(triples)));
        results.add(annotation);
    }
    return results;
}

From source file:org.apache.solr.analytics.statistics.StatsCollectorSupplierFactory.java

License:Apache License

/**
 *  Builds a value source for a given field, making sure that the field fits a given source type.
 * @param schema the schema// ww w .  j  av a2 s  .  com
 * @param expressionString The name of the field to build a Field Source from.
 * @param sourceType FIELD_TYPE for any type of field, NUMBER_TYPE for numeric fields, 
 * DATE_TYPE for date fields and STRING_TYPE for string fields.
 * @return a value source
 */
private static ValueSource buildFieldSource(IndexSchema schema, String expressionString, int sourceType) {
    SchemaField sf;
    try {
        sf = schema.getField(expressionString);
    } catch (SolrException e) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "The field " + expressionString + " does not exist.", e);
    }
    FieldType type = sf.getType();
    if (type instanceof TrieIntField) {
        if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
            return null;
        }
        return new IntFieldSource(expressionString) {
            public String description() {
                return field;
            }
        };
    } else if (type instanceof TrieLongField) {
        if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
            return null;
        }
        return new LongFieldSource(expressionString) {
            public String description() {
                return field;
            }
        };
    } else if (type instanceof TrieFloatField) {
        if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
            return null;
        }
        return new FloatFieldSource(expressionString) {
            public String description() {
                return field;
            }
        };
    } else if (type instanceof TrieDoubleField) {
        if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
            return null;
        }
        return new DoubleFieldSource(expressionString) {
            public String description() {
                return field;
            }
        };
    } else if (type instanceof TrieDateField) {
        if (sourceType != DATE_TYPE && sourceType != FIELD_TYPE) {
            return null;
        }
        return new DateFieldSource(expressionString) {
            public String description() {
                return field;
            }
        };
    } else if (type instanceof StrField) {
        if (sourceType != STRING_TYPE && sourceType != FIELD_TYPE) {
            return null;
        }
        return new BytesRefFieldSource(expressionString) {
            public String description() {
                return field;
            }
        };
    }
    throw new SolrException(ErrorCode.BAD_REQUEST,
            type.toString() + " is not a supported field type in Solr Analytics.");
}

From source file:org.apache.solr.schema.FloatField.java

License:Apache License

@Override
public ValueSource getValueSource(SchemaField field, QParser qparser) {
    field.checkFieldCacheSource(qparser);
    return new FloatFieldSource(field.name);
}

From source file:org.apache.solr.schema.FloatPointField.java

License:Apache License

@Override
public ValueSource getValueSource(SchemaField field, QParser qparser) {
    field.checkFieldCacheSource();
    return new FloatFieldSource(field.getName());
}

From source file:org.codice.ddf.spatial.geocoding.query.GeoNamesQueryLuceneIndex.java

License:Open Source License

protected Query createQuery(final String queryString) throws ParseException {
    final StandardAnalyzer standardAnalyzer = new StandardAnalyzer();

    final QueryParser nameQueryParser = new QueryParser(GeoNamesLuceneConstants.NAME_FIELD, standardAnalyzer);
    nameQueryParser.setEnablePositionIncrements(false);

    /* For the name, we construct a query searching for exactly the query string (the phrase
    query), a query searching for all the terms in the query string (the AND query), and a
    query searching for any of the terms in the query string (the OR query). We take the
    maximum of the scores generated by these three queries and use that as the score for the
    name. *///from   w w w  . j ava2s . c o m

    // Surround with quotes so Lucene looks for the words in the query as a phrase.
    // Phrase query gets the biggest boost - 3.2 was obtained after some experimentation.
    final Query phraseNameQuery = new BoostQuery(nameQueryParser.parse("\"" + queryString + "\""), 3.2f);

    // By default, QueryParser uses OR to separate terms.
    // We give OR queries the lowest boost because they're not as good as phrase matches or
    // AND matches - 1 (the default boost value) was obtained after some experimentation.
    final Query orNameQuery = nameQueryParser.parse(queryString);

    nameQueryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
    // We give AND queries the second-biggest boost because they're better than OR matches but
    // not as good as phrase matches - 2 was obtained after some experimentation.
    final Query andNameQuery = new BoostQuery(nameQueryParser.parse(queryString), 2f);

    final List<Query> nameQueryList = Arrays.asList(phraseNameQuery, orNameQuery, andNameQuery);
    // This query will score each document by the maximum of the three sub-queries.
    final Query nameQuery = new DisjunctionMaxQuery(nameQueryList, 0);

    final QueryParser alternateNamesQueryParser = new QueryParser(GeoNamesLuceneConstants.ALTERNATE_NAMES_FIELD,
            standardAnalyzer);

    // For the alternate names, we perform an AND query and an OR query, both of which are
    // boosted less than the name query because the alternate names are generally not as
    // important.
    // The OR query gets a lower boost - 0.5 was obtained after some experimentation.
    final Query orAlternateNamesQuery = new BoostQuery(alternateNamesQueryParser.parse(queryString), 0.5f);

    alternateNamesQueryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
    // The AND query gets a higher boost - 1 (the default boost value) was obtained after some
    // experimentation.
    final Query andAlternateNamesQuery = alternateNamesQueryParser.parse(queryString);

    final List<Query> alternateNamesQueryList = Arrays.asList(orAlternateNamesQuery, andAlternateNamesQuery);
    // This query will score each document by the maximum of the two sub-queries.
    final Query alternateNamesQuery = new DisjunctionMaxQuery(alternateNamesQueryList, 0);

    final List<Query> queryList = Arrays.asList(nameQuery, alternateNamesQuery);

    // This query will score each document by the sum of the two sub-queries, since both the
    // name and the alternate names are important.
    // The boost values ensure that how well the query matches the name has a bigger impact on
    // the final score than how well it matches the alternate names.
    final DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery(queryList, 1.0f);

    // This is the boost we calculated at index time, and it is applied in the CustomScoreQuery.
    final FunctionQuery boostQuery = new FunctionQuery(
            new FloatFieldSource(GeoNamesLuceneConstants.BOOST_FIELD));

    return new CustomScoreQuery(disjunctionMaxQuery, boostQuery);
}