List of usage examples for org.apache.solr.util DOMUtil getAttr
public static String getAttr(Node node, String name, String missing_err)
From source file:com.billiger.solr.handler.component.QLTBComponent.java
License:Apache License
/** * Load the QLTB map from a Config.// w w w.ja v a 2s .c om * * Read and process the "boosts/query" XPath nodes from the given * Config, and build them into a QLTB map. The XML format is described * in the class documentation. * * The result of this function is a map of (analyzed) query strings * with their respective lists of boosted query terms. These are * ConstantScoreQuery instances for each term with the corresponding * boost factor. (Invalid - i.e. non-numerical - boost factors are * logged as warnings). * * The SOLR core that is passed into this function is necessary for * determinating the FieldType of the boosted fields. Only with the * correct field type is it possible to boost non-string fields, as * these non-string values need to be ft.readableToIndexed(). * * @param cfg * Config object to read the XML QLTB from * @param core * SOLR Core the query is performed on * @return QLTB map * * @throws IOException * If the query could not be analysed */ private Map<String, List<Query>> loadQLTBMap(final Config cfg, final SolrCore core) throws IOException { Map<String, List<Query>> map = new HashMap<String, List<Query>>(); NodeList nodes = (NodeList) cfg.evaluate("boosts/query", XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); String qstr = DOMUtil.getAttr(node, "text", "missing query 'text'"); qstr = getAnalyzedQuery(qstr); NodeList children = node.getChildNodes(); List<Query> termBoosts = new ArrayList<Query>(); for (int j = 0; j < children.getLength(); j++) { Node child = children.item(j); if (!child.getNodeName().equals("term")) { continue; } String field = DOMUtil.getAttr(child, "field", "missing 'field'"); String value = DOMUtil.getAttr(child, "value", "missing 'value'"); String boost = DOMUtil.getAttr(child, "boost", "missing 'boost'"); float termBoost = 1; try { termBoost = Float.parseFloat(boost); } catch (NumberFormatException e) { log.warn("invalid boost " + boost + " for query \"" + qstr + "\", term: \"" + field + ":" + value + "\": " + e.getMessage()); continue; } // without readableToIndexed QLTB boosting would only work // for string field types FieldType ft = core.getLatestSchema().getField(field).getType(); value = ft.readableToIndexed(value); Term t = new Term(field, value); TermQuery tq = new TermQuery(t); ConstantScoreQuery csq = new ConstantScoreQuery(tq); csq.setBoost(termBoost); termBoosts.add(csq); } map.put(qstr, termBoosts); } return map; }