List of usage examples for org.apache.solr.client.solrj SolrQuery setQuery
public SolrQuery setQuery(String query)
From source file:lux.solr.TLogTest.java
License:Mozilla Public License
private QueryResponse search(String q) throws SolrServerException { SolrQuery query = new SolrQuery(); query.setQuery(q); query.addField("lux_uri"); query.addField("lux_xml"); return solr.query(query); }
From source file:net.cloudfree.apps.shop.internal.app.ListingServlet.java
License:Open Source License
@Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { final IListingManager manager = ModelUtil.getManager(IListingManager.class, getContext()); final ISolrQueryExecutor queryExecutor = (ISolrQueryExecutor) manager.getAdapter(ISolrQueryExecutor.class); if (null == queryExecutor) { resp.sendError(404);/* w w w. j a v a2s . com*/ return; } final List<String> selectedFacets = new ArrayList<String>(); final SolrQuery query = new SolrQuery(); boolean facet = true; boolean checkVariations = false; final String path = req.getPathInfo(); if ((null != path) && (path.length() > 1)) { query.setFilterQueries(Document.URI_PATH + ":" + path.substring(1)); query.setFields("id", "title", "price", "name", "score", "img480", "uripath", "description"); facet = false; checkVariations = true; } else { final String q = req.getParameter("q"); if (StringUtils.isNotBlank(q)) { query.setQuery(q); } query.setFields("id", "title", "price", "name", "score", "img48", "uripath"); // ignore variations final String f = req.getParameter("f"); if (StringUtils.isNotBlank(f)) { query.addFilterQuery(f); } else { query.addFilterQuery("-type:variation"); } // facet narrowing? final String[] narrows = req.getParameterValues("narrow"); if (null != narrows) { for (final String narrow : narrows) { if (StringUtils.isBlank(narrow)) { continue; } final String[] split = StringUtils.split(narrow, ':'); if (split.length != 2) { continue; } final String name = split[0]; final String value = split[1]; if (StringUtils.isBlank(name) || StringUtils.isBlank(value)) { continue; } final FacetFilter filter = facetFilters.get(name); if (null != filter) { if (filter.addFilter(query, value)) { selectedFacets.add(name + ":" + value); } } } } } query.setQueryType("dismax"); query.set("debug", true); // facet fields if (facet) { query.setFacet(true); for (final FacetFilter filter : facetFilters.values()) { filter.defineFilter(query); } } final QueryResponse response = queryExecutor.query(query); final SolrDocumentList results = response.getResults(); resp.setContentType("text/html"); resp.setCharacterEncoding("UTF-8"); final PrintWriter writer = resp.getWriter(); writer.println("<html><head>"); writer.println("<title>"); writer.println(path); writer.println(" - Shop Listings</title>"); writer.println("</head><body>"); writer.println("<h1>Found Listings</h1>"); writer.println("<p>"); writer.println("CloudFree found <strong>" + results.getNumFound() + "</strong> products in " + (response.getQTime() < 1000 ? "less than a second." : (response.getQTime() + "ms."))); if (results.size() < results.getNumFound()) { writer.println("<br/>"); if (results.getStart() == 0) { writer.println("Only the first " + results.size() + " products are shown."); } else { writer.println("Only products " + results.getStart() + " till " + (results.getStart() + results.size()) + " will be shown."); } } writer.println("</p>"); final List<FacetField> facetFields = response.getFacetFields(); if ((null != facetFields) && !facetFields.isEmpty()) { writer.println("<p>"); writer.println("You can filter the results by: </br>"); for (final FacetField facetField : facetFields) { final List<Count> values = facetField.getValues(); if ((null != values) && !values.isEmpty()) { writer.println("<div style=\"float:left;\">"); writer.print("<em>"); writer.print(facetField.getName()); writer.print("</em>"); writer.println("<ul style=\"margin:0;\">"); int filters = 0; for (final Count count : values) { if (count.getCount() == 0) { continue; } writer.print("<li>"); writer.print(count.getName()); writer.print(" ("); writer.print(count.getCount()); writer.print(")"); writer.print("</li>"); filters++; } if (filters == 0) { writer.print("<li>none</li>"); } writer.println("</ul>"); writer.println("</div>"); } } writer.println("<div style=\"clear:both;\"> </div>"); writer.println("</p>"); } writer.println("<p>"); if (!results.isEmpty()) { for (final SolrDocument listing : results) { writeListing(listing, writer, req); if (checkVariations) { final SolrQuery query2 = new SolrQuery(); query2.setQuery("parentid:" + listing.getFirstValue("id")); query2.setFields("id", "title", "price", "name", "score", "img48", "uripath", "color", "size"); final QueryResponse response2 = queryExecutor.query(query2); final SolrDocumentList results2 = response2.getResults(); if ((null != results2) && !results2.isEmpty()) { writer.println("There are " + results2.size() + " variations available."); for (final SolrDocument variation : results2) { writeListing(variation, writer, req); } } } } } else { writer.println("No listings found!"); } writer.println("</p>"); writer.println("</body>"); }
From source file:net.hydromatic.optiq.impl.solr.SolrSchema.java
License:Apache License
@Override protected Map<String, Table> getTableMap() { final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder(); server = new HttpSolrServer(host); final List<SolrFieldType> fieldTypes = new ArrayList<SolrFieldType>(); SolrQuery query = new SolrQuery(); query.setQuery("*:*"); query.setFacetLimit(100000);/*from w w w .j a v a 2 s . c o m*/ query.setParam("rows", "10000"); SolrDocumentList res = null; try { res = server.query(query).getResults(); } catch (SolrServerException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } assert res != null; final SolrTable table; table = new SolrTable(this, core, res, server); builder.put(core, table); return builder.build(); }
From source file:net.yacy.cora.federate.solr.connector.AbstractSolrConnector.java
License:Open Source License
public static SolrQuery getSolrQuery(final String querystring, final String sort, final int offset, final int count, final String... fields) { // construct query final SolrQuery params = new SolrQuery(); //if (count < 2 && querystring.startsWith("{!raw f=")) { // params.setQuery("*:*"); // params.addFilterQuery(querystring); //} else {// w w w . ja va 2 s . c o m params.setQuery(querystring); //} params.clearSorts(); if (sort != null) { params.set(CommonParams.SORT, sort); } params.setRows(count); params.setStart(offset); params.setFacet(false); if (fields != null && fields.length > 0) params.setFields(fields); params.setIncludeScore(false); params.setParam("defType", "edismax"); params.setParam(DisMaxParams.QF, CollectionSchema.text_t.getSolrFieldName() + "^1.0"); return params; }
From source file:net.yacy.cora.federate.solr.connector.AbstractSolrConnector.java
License:Open Source License
/** * check if a given document, identified by url hash as document id exists * @param id the url hash and document id * @return metadata if any entry in solr exists, null otherwise * @throws IOException/* w w w .j a v a 2 s . c o m*/ */ @Override public LoadTimeURL getLoadTimeURL(String id) throws IOException { // construct raw query final SolrQuery params = new SolrQuery(); //params.setQuery(CollectionSchema.id.getSolrFieldName() + ":\"" + id + "\""); String q = "{!cache=false raw f=" + CollectionSchema.id.getSolrFieldName() + "}" + id; params.setQuery(q); params.setRows(1); params.setStart(0); params.setFacet(false); params.clearSorts(); params.setFields(CollectionSchema.id.getSolrFieldName(), CollectionSchema.sku.getSolrFieldName(), CollectionSchema.load_date_dt.getSolrFieldName()); params.setIncludeScore(false); // query the server final SolrDocumentList sdl = getDocumentListByParams(params); if (sdl == null || sdl.getNumFound() <= 0) return null; SolrDocument doc = sdl.iterator().next(); LoadTimeURL md = getLoadTimeURL(doc); return md; }
From source file:net.yacy.cora.federate.solr.connector.AbstractSolrConnector.java
License:Open Source License
/** * get the number of results when this query is done. * This should only be called if the actual result is never used, and only the count is interesting * @param querystring/*from ww w. ja v a 2s .co m*/ * @return the number of results for this query */ @Override public long getCountByQuery(String querystring) throws IOException { // construct query final SolrQuery params = new SolrQuery(); params.setQuery(querystring); params.setRows(0); // essential to just get count params.setStart(0); params.setFacet(false); params.clearSorts(); params.setFields(CollectionSchema.id.getSolrFieldName()); params.setIncludeScore(false); // query the server final SolrDocumentList sdl = getDocumentListByParams(params); return sdl == null ? 0 : sdl.getNumFound(); }
From source file:net.yacy.cora.federate.solr.connector.AbstractSolrConnector.java
License:Open Source License
/** * get facets of the index: a list of lists with values that are most common in a specific field * @param query a query which is performed to get the facets * @param fields the field names which are selected as facet * @param maxresults the maximum size of the resulting maps * @return a map with key = facet field name, value = an ordered map of field values for that field * @throws IOException//from w w w . j a v a 2 s.c o m */ @Override public LinkedHashMap<String, ReversibleScoreMap<String>> getFacets(String query, int maxresults, final String... fields) throws IOException { // construct query assert fields.length > 0; final SolrQuery params = new SolrQuery(); params.setQuery(query); params.setRows(0); params.setStart(0); params.setFacet(true); params.setFacetMinCount(1); // there are many 0-count facets in the uninverted index cache params.setFacetLimit(maxresults); params.setFacetSort(FacetParams.FACET_SORT_COUNT); params.setParam(FacetParams.FACET_METHOD, FacetParams.FACET_METHOD_fc /*FACET_METHOD_fcs*/); params.setFields(fields); params.clearSorts(); params.setIncludeScore(false); for (String field : fields) params.addFacetField(field); // query the server QueryResponse rsp = getResponseByParams(params); LinkedHashMap<String, ReversibleScoreMap<String>> facets = new LinkedHashMap<String, ReversibleScoreMap<String>>( fields.length); for (String field : fields) { FacetField facet = rsp.getFacetField(field); ReversibleScoreMap<String> result = new ClusteredScoreMap<String>(UTF8.insensitiveUTF8Comparator); List<Count> values = facet.getValues(); if (values == null) continue; for (Count ff : values) if (ff.getCount() > 0) result.set(ff.getName(), (int) ff.getCount()); facets.put(field, result); } return facets; }
From source file:net.yacy.cora.federate.solr.connector.AbstractSolrConnector.java
License:Open Source License
@Override public SolrDocument getDocumentById(final String id, final String... fields) throws IOException { assert id.length() == Word.commonHashLength : "wrong id: " + id; final SolrQuery query = new SolrQuery(); // construct query StringBuilder sb = new StringBuilder(23); sb.append("{!cache=false raw f=").append(CollectionSchema.id.getSolrFieldName()).append('}').append(id); query.setQuery(sb.toString()); //query.setQuery("*:*"); //query.addFilterQuery(sb.toString()); query.clearSorts();//w w w. j a v a 2 s . c o m query.setRows(1); query.setStart(0); if (fields != null && fields.length > 0) query.setFields(fields); query.setIncludeScore(false); // query the server try { final SolrDocumentList docs = getDocumentListByParams(query); if (docs == null || docs.isEmpty()) return null; SolrDocument doc = docs.get(0); return doc; } catch (final Throwable e) { clearCaches(); // we clear the in case that this is caused by OOM throw new IOException(e.getMessage(), e); } }
From source file:net.yacy.crawler.data.CrawlQueues.java
License:Open Source License
public boolean autocrawlJob() { if (!this.sb.getConfigBool(SwitchboardConstants.AUTOCRAWL, false)) { return false; }/*from w w w . j a va2 s. co m*/ if (isPaused(SwitchboardConstants.CRAWLJOB_LOCAL_CRAWL)) { return false; } if (coreCrawlJobSize() > 200) { return false; } String rows = this.sb.getConfig(SwitchboardConstants.AUTOCRAWL_ROWS, "100"); String dateQuery = String.format("load_date_dt:[* TO NOW-%sDAY]", this.sb.getConfig(SwitchboardConstants.AUTOCRAWL_DAYS, "1")); final SolrQuery query = new SolrQuery(); query.add("group", "true"); query.add("group.field", "host_s"); query.add("group.limit", "1"); query.add("group.main", "true"); query.add("rows", rows); query.setQuery(this.sb.getConfig(SwitchboardConstants.AUTOCRAWL_QUERY, "*:*")); query.setFields("host_s,url_protocol_s"); query.addSort("load_date_dt", SolrQuery.ORDER.asc); query.addFilterQuery(dateQuery); try { QueryResponse resp = sb.index.fulltext().getDefaultConnector().getResponseByParams(query); int i = 0; int deepRatio = Integer.parseInt(this.sb.getConfig(SwitchboardConstants.AUTOCRAWL_RATIO, "50")); for (SolrDocument doc : resp.getResults()) { boolean deep = false; i++; if (i % deepRatio == 0) { deep = true; } DigestURL url; final String u = doc.getFieldValue("url_protocol_s").toString() + "://" + doc.getFieldValue("host_s").toString(); try { url = new DigestURL(u); } catch (final MalformedURLException e) { continue; } final String urlRejectReason = this.sb.crawlStacker.urlInAcceptedDomain(url); if (urlRejectReason == null) { this.sb.crawlStacker.enqueueEntry(new Request(ASCII.getBytes(this.sb.peers.mySeed().hash), url, null, "CRAWLING-ROOT", new Date(), deep ? this.sb.crawler.defaultAutocrawlDeepProfile.handle() : this.sb.crawler.defaultAutocrawlShallowProfile.handle(), 0, deep ? this.sb.crawler.defaultAutocrawlDeepProfile.timezoneOffset() : this.sb.crawler.defaultAutocrawlShallowProfile.timezoneOffset())); } else { CrawlQueues.log.warn("autocrawl: Rejected URL '" + urlToString(url) + "': " + urlRejectReason); } } } catch (SolrException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; }
From source file:net.yacy.search.index.ErrorCache.java
License:Open Source License
public ErrorCache(final Fulltext fulltext) { this.fulltext = fulltext; this.cache = new LinkedHashMap<String, CollectionConfiguration.FailDoc>(); // concurrently fill stack with latest values new Thread() { @Override/* w ww . j a v a 2 s . c o m*/ public void run() { final SolrQuery params = new SolrQuery(); params.setParam("defType", "edismax"); params.setStart(0); params.setRows(1000); params.setFacet(false); params.setSort( new SortClause(CollectionSchema.last_modified.getSolrFieldName(), SolrQuery.ORDER.desc)); params.setFields(CollectionSchema.id.getSolrFieldName()); params.setQuery( CollectionSchema.failreason_s.getSolrFieldName() + AbstractSolrConnector.CATCHALL_DTERM); params.set(CommonParams.DF, CollectionSchema.id.getSolrFieldName()); // DisMaxParams.QF or CommonParams.DF must be given SolrDocumentList docList; try { docList = fulltext.getDefaultConnector().getDocumentListByParams(params); if (docList != null) for (int i = docList.size() - 1; i >= 0; i--) { SolrDocument doc = docList.get(i); String hash = (String) doc.getFieldValue(CollectionSchema.id.getSolrFieldName()); cache.put(hash, null); } } catch (IOException e) { ConcurrentLog.logException(e); } } }.start(); }