Blame | Last modification | View Log | RSS feed
/****/package in.shop2020.serving.services;import javax.xml.xpath.XPath;import javax.xml.xpath.XPathConstants;import javax.xml.xpath.XPathExpressionException;import javax.xml.xpath.XPathFactory;import org.apache.juli.logging.Log;import org.apache.juli.logging.LogFactory;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.InputSource;/*** @author naveen**/public class SolrSearchService {/****/private static Log log = LogFactory.getLog(SolrSearchService.class);/****/public static String SOLR_URL = "http://localhost:8983/solr/select/";/****/private XPath xpath;/****/private InputSource inputSource;/**** @param query* @param facetDefinitionIDs*/public SolrSearchService(String query, String[] facetqueries,long[] facetDefinitionIDs) {this.xpath = XPathFactory.newInstance().newXPath();String uri = SOLR_URL + "?wt=xml&q=" + query;if(facetqueries != null) {for(int i=0; i<facetqueries.length; i++) {uri += "&fq=" + facetqueries[i];}}uri += "&fl=ID,Name&facet=true&rows=20";uri += "&facet.field=Category";for(int i=0; i<facetDefinitionIDs.length; i++) {uri += "&facet.field=" + "F_" + facetDefinitionIDs[i];}log.info("uri=" + uri);this.inputSource = new InputSource(uri);}/**** @return*/public long[] getResultEntityIDs() {String expression = "/response/result/doc/long";NodeList nodes = null;try {nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,XPathConstants.NODESET);}catch(XPathExpressionException xpee) {return null;}if(nodes.getLength() == 0) {return null;}long[] values = new long[nodes.getLength()];for(int i=0; i<nodes.getLength(); i++) {Node node = nodes.item(i);String value = node.getTextContent();values[i] = Long.parseLong(value);}return values;}/**** @return*/public String[] getResultCategoryNames() {String expression = "/response/lst/lst[@name = 'facet_fields']/";expression += "lst[@name = 'Category']/int/@name";NodeList nodes = null;try {nodes = (NodeList) this.xpath.evaluate(expression,this.inputSource, XPathConstants.NODESET);}catch (XPathExpressionException xpee) {return null;}if(nodes.getLength() == 0) {return null;}String[] values = new String[nodes.getLength()];for(int i=0; i<nodes.getLength(); i++) {Node node = nodes.item(i);values[i] = node.getTextContent();}return values;}/**** @return*/public int[] getResultCategoryCounts() {String expression = "/response/lst/lst[@name = 'facet_fields']/";expression += "lst[@name = 'Category']/int";NodeList nodes = null;try {nodes = (NodeList) this.xpath.evaluate(expression,this.inputSource, XPathConstants.NODESET);}catch (XPathExpressionException xpee) {return null;}if(nodes.getLength() == 0) {return null;}int[] values = new int[nodes.getLength()];for(int i=0; i<nodes.getLength(); i++) {Node node = nodes.item(i);values[i] = Integer.parseInt(node.getTextContent());}return values;}/**** @return*/public String[] getResultEntityNames() {String expression = "/response/result/doc/str";NodeList nodes = null;try {nodes = (NodeList) this.xpath.evaluate(expression, this.inputSource,XPathConstants.NODESET);}catch(XPathExpressionException xpee) {return null;}if(nodes.getLength() == 0) {return null;}String[] values = new String[nodes.getLength()];for(int i=0; i<nodes.getLength(); i++) {Node node = nodes.item(i);String value = node.getTextContent();values[i] = value;}return values;}/**** @param facetDefinitionID* @return*/public String[] getFacetValues(long facetDefinitionID) {String expression = "/response/lst/lst[@name = 'facet_fields']/";expression += "lst[@name = 'F_"+ facetDefinitionID +"']/int/@name";NodeList nodes = null;try {nodes = (NodeList) this.xpath.evaluate(expression,this.inputSource, XPathConstants.NODESET);}catch (XPathExpressionException xpee) {return null;}if(nodes.getLength() == 0) {return null;}String[] values = new String[nodes.getLength()];for(int i=0; i<nodes.getLength(); i++) {Node node = nodes.item(i);values[i] = node.getTextContent();}return values;}/**** @param facetDefinitionID* @return*/public String[] getFacetCounts(long facetDefinitionID) {String expression = "/response/lst/lst[@name = 'facet_fields']/";expression += "lst[@name = 'F_" + facetDefinitionID + "']/int";NodeList nodes = null;try {nodes = (NodeList) this.xpath.evaluate(expression,this.inputSource, XPathConstants.NODESET);}catch (XPathExpressionException xpee) {return null;}if(nodes.getLength() == 0) {return null;}String[] values = new String[nodes.getLength()];for(int i=0; i<nodes.getLength(); i++) {Node node = nodes.item(i);values[i] = node.getTextContent();}return values;}}