Example usage for org.apache.solr.common.params CommonParams FL

List of usage examples for org.apache.solr.common.params CommonParams FL

Introduction

In this page you can find the example usage for org.apache.solr.common.params CommonParams FL.

Prototype

String FL

To view the source code for org.apache.solr.common.params CommonParams FL.

Click Source Link

Document

query and init param for field list

Usage

From source file:org.mitre.opensextant.extraction.SolrGazetteer.java

License:Apache License

/** This only returns Country objects that are names;
 *  It does not produce any abbreviation variants.
 * //from w  w  w  .jav a 2 s .c o m
 * TODO: allow caller to get all entries, including abbreviations.
 */
protected void loadCountries() throws SolrServerException {
    country_lookup = new HashMap<>();

    ModifiableSolrParams ctryparams = new ModifiableSolrParams();
    ctryparams.set(CommonParams.FL,
            "id,name,cc,FIPS_cc,ISO3_cc,adm1,adm2,feat_class,feat_code,lat,lon,name_type");

    ctryparams.set("q", "+feat_class:A +feat_code:PCL* +name_type:N");
    ctryparams.set("rows", 2000);

    QueryResponse response = solr.getInternalSolrServer().query(ctryparams);

    // -- Process Solr Response;  This for loop matches the one in SolrMatcher
    // 
    SolrDocumentList docList = response.getResults();
    for (SolrDocument solrDoc : docList) {
        String code = SolrProxy.getString(solrDoc, "cc");
        //String fips = SolrProxy.getString(solrDoc, "cc_fips");
        String name = SolrProxy.getString(solrDoc, "name");

        // NOTE: FIPS could be "*", where ISO2 column is always non-trivial. if ("*".equals(code)){code = fips; }

        Country C = country_lookup.get(code);
        if (C != null) {
            C.addAlias(name); // all other metadata is same. 
            continue;
        }

        C = new Country(code, name);
        C.setName_type(SolrProxy.getChar(solrDoc, "name_type"));
        C.setLatitude(SolrProxy.getDouble(solrDoc, "lat"));
        C.setLongitude(SolrProxy.getDouble(solrDoc, "lon"));
        C.addAlias(C.name); // don't loose this entry as a likely variant.

        country_lookup.put(code, C);
    }

    /** Finally choose default official names given the map of name:iso2
     */
    for (Country C : country_lookup.values()) {
        String n = _default_country_names.get(C.country_id);
        if (n != null) {
            for (String alias : C.getAliases()) {
                if (n.equalsIgnoreCase(alias.toLowerCase())) {
                    C.setName(alias);
                }
            }
        }
    }
}

From source file:org.mitre.opensextant.extraction.TaxonMatcher.java

License:Apache License

protected static void initialize() throws IOException {

    if (solr != null) {
        return;//www  .  ja  v a  2s .  c o m
    }

    String config_solr_home = System.getProperty("solr.solr.home");
    solr = new SolrProxy(config_solr_home, "taxcat");

    params = new ModifiableSolrParams();
    params.set(CommonParams.QT, requestHandler);
    params.set(CommonParams.FL, "id,catalog,taxnode,phrase,tag");

    params.set("tagsLimit", 100000);
    params.set("subTags", false);
    params.set("matchText", false);//we've got the input doc as a string instead

    /* Possible overlaps: ALL, NO_SUB, LONGEST_DOMINANT_RIGHT
     * See Solr Text Tagger documentation for details.
     */
    params.set("overlaps", "NO_SUB");

}

From source file:org.opencms.search.solr.CmsSolrQuery.java

License:Open Source License

/**
 * Ensures that at least the 'path' and the 'type' are part of the fields returned field list.<p>
 *
 * @see CommonParams#FL//from   w w w .j  a v a 2  s .co  m
 */
private void ensureReturnFields() {

    String[] fl = getParams(CommonParams.FL);
    if ((fl != null) && (fl.length > 0)) {
        List<String> result = new ArrayList<String>();
        for (String field : fl) {
            String commasep = field.replaceAll(" ", ",");
            List<String> list = CmsStringUtil.splitAsList(commasep, ',');
            if (!list.contains("*")) {
                for (String reqField : CmsStringUtil.splitAsList(MINIMUM_FIELDS, ",")) {
                    if (!list.contains(reqField)) {
                        list.add(reqField);
                    }
                }
            }
            result.addAll(list);
        }
        setParam(CommonParams.FL, CmsStringUtil.arrayAsString(result.toArray(new String[0]), ","));
    }
}

From source file:org.opencommercesearch.RuleManager.java

License:Apache License

/**
 * Loads the rules that matches the given query
 * /*from w  ww.j  av  a  2s  . c o m*/
 * @param q is the user query
 * @param categoryPath is the current category path, used to filter out rules (i.e. rule based pages)
 * @param categoryFilterQuery is the current category search token that will be used for filtering out rules and facets
 * @param isSearch indicates if we are browsing or searching the site
 * @param isRuleBasedPage tells whether or not we are on a rule based page
 * @param catalog the current catalog we are browsing/searching
 * @param isOutletPage tells whether or not the current page is outlet
 * @param brandId is the current brand id currently browsed, if any.
 * @throws RepositoryException if an exception happens retrieving a rule from the repository
 * @throws SolrServerException if an exception happens querying the search engine
 */
void loadRules(String q, String categoryPath, String categoryFilterQuery, boolean isSearch,
        boolean isRuleBasedPage, RepositoryItem catalog, boolean isOutletPage, String brandId,
        Set<String> includeExperiments, Set<String> excludeExperiments)
        throws RepositoryException, SolrServerException {
    if (isSearch && StringUtils.isBlank(q)) {
        throw new IllegalArgumentException("Missing query");
    }

    SolrQuery query = new SolrQuery("*:*");
    query.setStart(DEFAULT_START);
    query.setRows(DEFAULT_ROWS);
    query.addSort(FIELD_SORT_PRIORITY, ORDER.asc);
    query.addSort(FIELD_SCORE, ORDER.asc);
    query.addSort(FIELD_ID, ORDER.asc);
    query.add(CommonParams.FL, FIELD_ID, FIELD_BOOST_FUNCTION, FIELD_FACET_FIELD, FIELD_COMBINE_MODE,
            FIELD_QUERY, FIELD_CATEGORY, FIELD_EXPERIMENTAL);

    StringBuilder reusableStringBuilder = new StringBuilder();
    query.addFilterQuery(getTargetFilter(reusableStringBuilder, isSearch, q));
    query.addFilterQuery(getCategoryFilter(reusableStringBuilder, categoryFilterQuery, categoryPath));
    query.addFilterQuery(getSiteFilter(reusableStringBuilder, catalog));
    query.addFilterQuery(getBrandFilter(reusableStringBuilder, brandId));
    query.addFilterQuery(getSubTargetFilter(reusableStringBuilder, isOutletPage));

    StringBuilder catalogFilter = reuseStringBuilder(reusableStringBuilder);
    catalogFilter.append("catalogId:").append(WILDCARD).append(" OR ").append("catalogId:")
            .append(catalog.getRepositoryId());
    query.addFilterQuery(catalogFilter.toString());

    //Notice how the current datetime (NOW wildcard on Solr) is rounded to days (NOW/DAY). This allows filter caches
    //to be reused and hopefully improve performance. If you don't round to day, NOW is very precise (up to milliseconds); so every query
    //would need a new entry on the filter cache...
    //Also, notice that NOW/DAY is midnight from last night, and NOW/DAY+1DAY is midnight today.
    //The below query is intended to match rules with null start or end dates, or start and end dates in the proper range.
    query.addFilterQuery(
            "-(((startDate:[* TO *]) AND -(startDate:[* TO NOW/DAY+1DAY])) OR (endDate:[* TO *] AND -endDate:[NOW/DAY+1DAY TO *]))");

    int queryTime = 0;
    QueryResponse res = server.query(query);
    queryTime += res.getQTime();

    if (res.getResults() == null || res.getResults().getNumFound() == 0) {
        rules = Collections.emptyMap();
        loadRulesTime = queryTime;
        return;
    }

    rules = new HashMap<String, List<RepositoryItem>>(RuleType.values().length);
    ruleDocs = new HashMap<String, SolrDocument>();
    SolrDocumentList docs = res.getResults();
    int total = (int) docs.getNumFound();
    int processed = 0;
    while (processed < total) {
        for (int i = 0; i < docs.size(); i++) {
            ++processed;
            SolrDocument doc = docs.get(i);

            if (isSearch && !matchesQuery(q, doc)) {
                // skip this rule
                continue;
            }

            RepositoryItem rule = searchRepository.getItem((String) doc.getFieldValue("id"),
                    SearchRepositoryItemDescriptor.RULE);

            //for rule based categories, include all facet rules and ranking rules of only that category
            if (rule != null) {

                if (excludeExperiments.contains(rule.getRepositoryId())) {
                    continue;
                }

                Boolean experimental = (Boolean) doc.getFieldValue(FIELD_EXPERIMENTAL);
                if (experimental != null && experimental
                        && !includeExperiments.contains(rule.getRepositoryId())) {
                    continue;
                }

                String ruleType = (String) rule.getPropertyValue(RuleProperty.RULE_TYPE);
                if (ruleType.equals(RuleProperty.TYPE_FACET_RULE)) {
                    buildRuleLists(ruleType, rule, doc);
                } else {
                    if (categoryPath != null && isRuleBasedPage) {
                        List<String> ruleCategories = (List<String>) doc.getFieldValue(FIELD_CATEGORY);
                        if (ruleCategories != null) {
                            if (ruleCategories.contains(categoryPath)) {
                                buildRuleLists(ruleType, rule, doc);
                            }
                        }
                    } else {
                        buildRuleLists(ruleType, rule, doc);
                    }
                }
            } else {
                //TODO gsegura: add logging that we couldn't find the rule item in the DB
            }
        }
        if (processed < total) {
            query.setStart(processed);
            res = server.query(query);
            queryTime += res.getQTime();
            docs = res.getResults();
        }
    }

    loadRulesTime = queryTime;
}

From source file:org.opensextant.extractors.geo.GazetteerMatcher.java

License:Apache License

@Override
public void initialize() throws ConfigException {

    super.initialize();

    /*//  w ww.j  a  va2s .c om
     * Setup matcher params.
     */
    params.set(CommonParams.FL,
            "id,name,cc,adm1,adm2,feat_class,feat_code,geo,place_id,name_bias,id_bias,name_type");
    params.set("tagsLimit", 100000);
    params.set(CommonParams.ROWS, 100000);
    params.set("subTags", false);

    // we've got the input doc as a string instead; matchText=false means
    // the tagger will not report the text, just span offsets.
    params.set("matchText", false);

    /*
     * Possible overlaps: ALL, NO_SUB, LONGEST_DOMINANT_RIGHT See Solr Text
     * Tagger documentation for details.
     */
    params.set("overlaps", "LONGEST_DOMINANT_RIGHT");
    // params.set("overlaps", "NO_SUB");

    gazetteer = new SolrGazetteer(this.solr);
}

From source file:org.opensextant.extractors.geo.SolrGazetteer.java

License:Apache License

/**
 * For larger areas choose a higher number of Rows to return. If you choose
 * to use Solr spatial score-by-distance for sorting or anything, then Solr
 * appears to want to load entire index into memory. So this sort mechanism
 * is off by default./*from   ww  w  .ja  va  2  s  .  c o m*/
 * 
 * @param rows
 *            rows to include in spatial lookups
 * @return solr params
 */
protected static ModifiableSolrParams createGeodeticLookupParams(int rows) {
    /*
     * Basic parameters for geospatial lookup. These are reused, and only pt
     * and d are set for each lookup.
     *
     */
    ModifiableSolrParams p = new ModifiableSolrParams();
    p.set(CommonParams.FL,
            "id,name,cc,adm1,adm2,feat_class,feat_code," + "geo,place_id,name_bias,id_bias,name_type");
    p.set(CommonParams.ROWS, rows);
    p.set(CommonParams.Q, "{!geofilt sfield=geo}");
    // p.set(CommonParams.SORT, "score desc");
    p.set("spatial", "true");

    return p;
}

From source file:org.opensextant.extractors.geo.SolrGazetteer.java

License:Apache License

/**
 * Initialize. Cascading env variables: First use value from constructor,
 * then opensextant.solr, then solr.solr.home
 *
 * @throws ConfigException/* w  ww .j a v a2  s. co  m*/
 *             Signals that a configuration exception has occurred.
 */
private void initialize(String solrHome) throws ConfigException {

    solr = solrHome != null ? new SolrProxy(solrHome, "gazetteer") : new SolrProxy("gazetteer");

    params.set(CommonParams.Q, "*:*");
    params.set(CommonParams.FL,
            "id,name,cc,adm1,adm2,feat_class,feat_code,geo,place_id,name_bias,id_bias,name_type");
    try {
        this.countryCodes = loadCountries(solr.getInternalSolrServer());
    } catch (SolrServerException loadErr) {
        throw new ConfigException("SolrGazetteer is unable to load countries due to Solr error", loadErr);
    } catch (IOException ioErr) {
        throw new ConfigException("SolrGazetteer is unable to load countries due to IO/file error", ioErr);
    }
}

From source file:org.opensextant.extractors.geo.SolrGazetteer.java

License:Apache License

/**
 * This only returns Country objects that are names; It does not produce any
 * abbreviation variants.//  ww w  .  jav a  2 s.  c  om
 * 
 * TODO: allow caller to get all entries, including abbreviations.
 *
 * @param index
 *            solr instance to query
 * @return country data hash
 * @throws SolrServerException
 *             the solr server exception
 * @throws IOException
 *             on err, if country metadata file is not found in classpath
 */
public static Map<String, Country> loadCountries(SolrServer index) throws SolrServerException, IOException {

    GeonamesUtility geodataUtil = new GeonamesUtility();
    Map<String, Country> countryCodeMap = geodataUtil.getISOCountries();

    Logger log = LoggerFactory.getLogger(SolrGazetteer.class);
    ModifiableSolrParams ctryparams = new ModifiableSolrParams();
    ctryparams.set(CommonParams.FL, "id,name,cc,FIPS_cc,ISO3_cc,adm1,adm2,feat_class,feat_code,geo,name_type");

    /* TODO: Consider different behaviors for PCLI vs. PCL[DFS] */
    ctryparams.set("q", "+feat_class:A +feat_code:(PCLI OR PCLIX OR TERR) +name_type:N");
    /* As of 2015 we have 2300+ name variants for countries and territories */
    ctryparams.set("rows", 5000);

    QueryResponse response = index.query(ctryparams);

    // Process Solr Response
    //
    SolrDocumentList docList = response.getResults();
    for (SolrDocument gazEntry : docList) {

        Country C = createCountry(gazEntry);

        Country existingCountry = countryCodeMap.get(C.getCountryCode());
        if (existingCountry != null) {
            if (existingCountry.ownsTerritory(C.getName())) {
                // do nothing.
            } else if (C.isTerritory) {
                log.debug("{} territory of {}", C, existingCountry);
                existingCountry.addTerritory(C);
            } else {
                log.debug("{} alias of {}", C, existingCountry);
                existingCountry.addAlias(C.getName()); // all other metadata is same.
            }
            continue;
        }

        log.info("Unknown country in gazetteer, that is not in flat files. C={}", C);

        countryCodeMap.put(C.getCountryCode(), C);
        countryCodeMap.put(C.CC_ISO3, C);
    }

    return countryCodeMap;
}

From source file:org.opensextant.extractors.xtax.TaxonMatcher.java

License:Apache License

public static List<Taxon> search(SolrServer index, String query) throws SolrServerException {
    ModifiableSolrParams qp = new ModifiableSolrParams();
    qp.set(CommonParams.FL, "id,catalog,taxnode,phrase,tag,name_type");
    qp.set(CommonParams.Q, query);/*from w  ww.  j a  v a  2s. c om*/
    return search(index, qp);
}

From source file:org.phenotips.ontology.internal.solr.SolrQueryUtils.java

License:Open Source License

/**
 * Adds extra parameters to a Solr query for better term searches, including custom options. More specifically, adds
 * parameters for requesting the score to be included in the results, for requesting a spellcheck result, and sets
 * the {@code start} and {@code rows} parameters when missing.
 *
 * @param originalParams the original Solr parameters to enhance
 * @param queryOptions extra options to include in the query; these override the default values, but don't override
 *            values already set in the query
 * @return the enhanced parameters//from   ww  w . j  a  v a2  s  .  c  om
 */
public static SolrParams enhanceParams(SolrParams originalParams, Map<String, String> queryOptions) {
    if (originalParams == null) {
        return null;
    }
    ModifiableSolrParams newParams = new ModifiableSolrParams();
    newParams.set(CommonParams.START, "0");
    newParams.set(CommonParams.ROWS, "1000");
    newParams.set(CommonParams.FL, "* score");
    if (queryOptions != null) {
        for (Map.Entry<String, String> item : queryOptions.entrySet()) {
            newParams.set(item.getKey(), item.getValue());
        }
    }
    for (Map.Entry<String, Object> item : originalParams.toNamedList()) {
        if (item.getValue() != null && item.getValue() instanceof String[]) {
            newParams.set(item.getKey(), (String[]) item.getValue());
        } else {
            newParams.set(item.getKey(), String.valueOf(item.getValue()));
        }
    }
    newParams.set("spellcheck", Boolean.toString(true));
    newParams.set(SpellingParams.SPELLCHECK_COLLATE, Boolean.toString(true));
    return newParams;
}