List of usage examples for org.apache.solr.client.solrj SolrQuery setParam
public SolrQuery setParam(String name, boolean value)
From source file:actors.SolrActor.java
License:Apache License
public void performedSearch(SolrSearchEvent msg) { try {// w ww . ja va 2 s .com System.out.println("SolrSearchEvent"); System.out.println(""); SolrQuery parameters = new SolrQuery(); //parameters.setRequestHandler("/get"); parameters.set("q", msg.getQuery()); parameters.setFields("title", "id", "path", "when", "icon", "size", "content_type"); parameters.set("defType", "edismax"); parameters.addFilterQuery("title", "content"); parameters.setStart(0); parameters.setHighlight(true).setHighlightSnippets(1); //set other params as needed parameters.setParam("hl.fl", "content"); parameters.setParam("wt", "json"); server.query(parameters); System.out.println(parameters); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:at.pagu.soldockr.core.QueryParser.java
License:Apache License
/** * Convert given Query into a SolrQuery executable via {@link SolrServer} * /*from w ww . j a v a 2 s . c om*/ * @param query * @return */ public final SolrQuery constructSolrQuery(SolDockRQuery query) { Assert.notNull(query, "Cannot construct solrQuery from null value."); Assert.notNull(query.getCriteria(), "Query has to have a criteria."); SolrQuery solrQuery = new SolrQuery(); solrQuery.setParam(CommonParams.Q, getQueryString(query)); if (query instanceof Query) { processQueryOptions(solrQuery, (Query) query); } if (query instanceof FacetQuery) { processFacetOptions(solrQuery, (FacetQuery) query); } return solrQuery; }
From source file:at.pagu.soldockr.core.QueryParser.java
License:Apache License
private void appendProjectionOnFields(SolrQuery solrQuery, List<Field> fields) { if (CollectionUtils.isEmpty(fields)) { return;/* www . j a v a 2s . com*/ } solrQuery.setParam(CommonParams.FL, StringUtils.join(fields, ",")); }
From source file:at.pagu.soldockr.core.QueryParser.java
License:Apache License
private void appendGroupByFields(SolrQuery solrQuery, List<Field> fields) { if (CollectionUtils.isEmpty(fields)) { return;/*from w ww . j av a2 s. c o m*/ } if (fields.size() > 1) { // there is a bug in solj which prevents multiple grouping // although available via HTTP call throw new ApiUsageException( "Cannot group on more than one field with current SolrJ API. Group on single field insead"); } solrQuery.set(GroupParams.GROUP, true); solrQuery.setParam(GroupParams.GROUP_MAIN, true); for (Field field : fields) { solrQuery.add(GroupParams.GROUP_FIELD, field.getName()); } }
From source file:com.comm.sr.common.solr.SolrQueryGenerator.java
@Override public SolrQuery generateFinalQuery(SolrCommonQuery query_) { SolrQuery solrQuery = new SolrQuery(); solrQuery.setParam("collection", query_.getCollectionName()); SubQuery query = query_.getSubQuery(); if (query == null) { return solrQuery; }/* w ww .jav a 2s . c o m*/ StringBuffer solrQueryBuffer = new StringBuffer(); makeFinalSolrQuery(query, solrQueryBuffer); int pageNum = query_.getPageNum(); if (pageNum < 1) { // default first page pageNum = 1; } else { pageNum = query_.getPageNum(); } List<SortItem> sortItems = query_.getSortItems(); if (sortItems == null) { sortItems = Lists.newArrayList(); } for (String functionQueryString : query_.getFunctionQuerysList()) { solrQueryBuffer.append("_query_:\"{!func}" + functionQueryString + "\"" + " AND "); } if (solrQueryBuffer.toString().contains("AND")) { int and = solrQueryBuffer.lastIndexOf("AND"); solrQueryBuffer.replace(and, and + 3, ""); } if (solrQueryBuffer.toString().trim().length() == 0) { solrQueryBuffer.append("*:*"); } for (SortItem sortItem : sortItems) { String fieldName = sortItem.getFieldName(); String order = sortItem.getSort(); if (order == null) { LOGGER.info("ingore sort fieldName:" + fieldName + ", please configure it."); continue; } if (order.trim().equals("asc")) { solrQuery.setSort(fieldName, SolrQuery.ORDER.asc); } if (order.trim().equals("desc")) { solrQuery.addSort(fieldName, SolrQuery.ORDER.desc); } } for (String fl : query_.getFls()) { solrQuery.addField(fl); } int pageSize = query_.getPageSize(); solrQuery.add("start", String.valueOf((pageNum - 1) * pageSize)); solrQuery.add("rows", String.valueOf(pageSize)); // if have distance query // fq=_query_:%22{!geofilt}%22&sfield=location&pt=45.15,-93.85&d=50000&sort=geodist()%20asc&fl=score,geodist(),location String location = query_.getLocationPoint(); Double distance = query_.getDistance(); if (location != null && distance != null) { solrQuery.add("d", distance.toString()); solrQuery.add("pt", location); solrQuery.add("sfield", "location"); solrQuery.add("fq", "_query_:{!geofilt}"); solrQuery.addSort("geodist()", SolrQuery.ORDER.asc); } LOGGER.info(solrQueryBuffer.toString()); solrQuery.setQuery(solrQueryBuffer.toString()); return solrQuery; }
From source file:com.doculibre.constellio.services.AutocompleteServicesImpl.java
License:Open Source License
@SuppressWarnings("unchecked") static private NamedList<Object> suggest(String q, String fieldName, SolrServer server, Boolean isStringField) { NamedList<Object> returnList = new NamedList<Object>(); // escape special characters SolrQuery query = new SolrQuery(); /*/*w ww . jav a2 s. c o m*/ * // * Set terms.lower to the input term * query.setParam(TermsParams.TERMS_LOWER, q); // * Set terms.prefix to * the input term query.setParam(TermsParams.TERMS_PREFIX, q); // * Set * terms.lower.incl to false * query.setParam(TermsParams.TERMS_LOWER_INCLUSIVE, "false"); // * Set * terms.fl to the name of the source field * query.setParam(TermsParams.TERMS_FIELD, fieldName); */ query.setParam(TermsParams.TERMS_FIELD, fieldName);// query.addTermsField("spell"); query.setParam(TermsParams.TERMS_LIMIT, TERMS_LIMIT);// query.setTermsLimit(MAX_TERMS); query.setParam(TermsParams.TERMS, "true");// query.setTerms(true); query.setParam(TermsParams.TERMS_LOWER, q);// query.setTermsLower(q); query.setParam(TermsParams.TERMS_PREFIX, q);// query.setTermsPrefix(q); query.setParam(TermsParams.TERMS_MINCOUNT, TERMS_MINCOUNT); query.setRequestHandler(SolrServices.AUTOCOMPLETE_QUERY_NAME); try { QueryResponse qr = server.query(query); NamedList<Object> values = qr.getResponse(); NamedList<Object> terms = (NamedList<Object>) values.get("terms");// TermsResponse // resp // = // qr.getTermsResponse(); NamedList<Object> suggestions = (NamedList<Object>) terms.get(fieldName);// items // = // resp.getTerms("spell"); if (!isStringField) { q = AnalyzerUtils.analyzePhrase(q, false); } for (int i = 0; i < suggestions.size(); i++) { String currentSuggestion = suggestions.getName(i); // System.out.println(currentSuggestion); if (isStringField) { if (currentSuggestion.contains(q)) { // String suffix = // StringUtils.substringAfter(currentSuggestion, q); // if (suffix.isEmpty() && // !currentSuggestion.equals(q)){ // //q n est pas dans currentSuggestion // break; // } if (currentSuggestion.contains(SPECIAL_CHAR)) { // le resultat de recherche retourne des fois une // partie de la valeur existant // dans le champ! currentSuggestion = StringUtils.substringAfter(currentSuggestion, SPECIAL_CHAR); } returnList.add(currentSuggestion, suggestions.getVal(i)); } } else { currentSuggestion = AnalyzerUtils.analyzePhrase(currentSuggestion, false); if (currentSuggestion.contains(q)) { returnList.add(currentSuggestion, suggestions.getVal(i)); } } } } catch (SolrServerException e) { throw new RuntimeException(e); } return returnList; }
From source file:com.doculibre.constellio.services.ClusteringServicesImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/*from w w w .j a va 2 s . c o m*/ public List<SimpleOrderedMap<Object>> cluster(SimpleSearch simpleSearch, CollectionFacet facet, int start, int row, ConstellioUser user) { // String luceneQuery = simpleSearch.getLuceneQuery(); RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices(); String solrServerName = simpleSearch.getCollectionName(); RecordCollection collection = collectionServices.get(solrServerName); //Remarque useDismax est false car nous allons utiliser le type de requete "/clustering" // donc pas besoin d'ajouter les paramtres necessaires dismax SolrQuery query = SearchServicesImpl.toSolrQuery(simpleSearch, false, true, true); query.setRequestHandler("/clustering"); query.setParam(ClusteringComponent.COMPONENT_NAME, Boolean.TRUE); query.setParam(ClusteringParams.USE_COLLECTION, facet.isClusteringUseCollection()); query.setParam(ClusteringParams.USE_SEARCH_RESULTS, facet.isClusteringUseSearchResults()); query.setParam(ClusteringParams.USE_DOC_SET, facet.isClusteringUseDocSet()); query.setParam(ClusteringParams.ENGINE_NAME, facet.getClusteringEngine()); //The maximum number of labels to produce query.setParam(CarrotParams.NUM_DESCRIPTIONS, "" + maxClusters); // query.setParam(CarrotParams.NUM_DESCRIPTIONS, "" + facet.getCarrotNumDescriptions()); query.setParam(CarrotParams.OUTPUT_SUB_CLUSTERS, facet.isCarrotOutputSubclusters()); query.setParam(ConstellioSolrQueryParams.LUCENE_QUERY, simpleSearch.getLuceneQuery()); query.setParam(ConstellioSolrQueryParams.COLLECTION_NAME, simpleSearch.getCollectionName()); query.setParam(ConstellioSolrQueryParams.SIMPLE_SEARCH, simpleSearch.toSimpleParams().toString()); if (user != null) { query.setParam(ConstellioSolrQueryParams.USER_ID, "" + user.getId()); } //If true, then the snippet field (if no snippet field, then the title field) will be highlighted and the highlighted text will be used for the snippet. query.setParam(CarrotParams.PRODUCE_SUMMARY, facet.isCarrotProduceSummary()); if (facet.getCarrotTitleField() != null) { IndexField indexField = facet.getCarrotTitleField(); query.setParam(CarrotParams.TITLE_FIELD_NAME, indexField.getName()); } if (facet.getCarrotUrlField() != null) { IndexField indexField = facet.getCarrotUrlField(); query.setParam(CarrotParams.URL_FIELD_NAME, indexField.getName()); } if (facet.getCarrotSnippetField() != null) { IndexField indexField = facet.getCarrotSnippetField(); query.setParam(CarrotParams.SNIPPET_FIELD_NAME, indexField.getName()); } // Requte Lucene // query.setQuery(luceneQuery); // nb rsultats par page query.setRows(row); // page de dbut query.setStart(start); //Les resultats ne vont pas etre affichs /*query.setHighlight(true); query.setHighlightFragsize(100); query.setHighlightSnippets(2);*/ if (collection.isOpenSearch()) { query.setParam("openSearchURL", collection.getOpenSearchURL()); } SolrServer server = SolrCoreContext.getSolrServer(solrServerName); QueryResponse queryResponse; try { queryResponse = server.query(query); } catch (SolrServerException e) { throw new RuntimeException(e); } NamedList<Object> values = queryResponse.getResponse(); Object clusters = values.get("clusters"); return (List<SimpleOrderedMap<Object>>) clusters; }
From source file:com.doculibre.constellio.services.FacetServicesImpl.java
License:Open Source License
public static SolrQuery toSolrQuery(SimpleSearch simpleSearch, int start, int row, boolean includeSingleValueFacets, boolean notIncludedOnly, List<String> customFieldFacets, List<String> customQueryFacets, ConstellioUser user) { String solrServerName = simpleSearch.getCollectionName(); RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices(); RecordCollection collection = collectionServices.get(solrServerName); SolrServices solrServices = ConstellioSpringUtils.getSolrServices(); Boolean usesDisMax = solrServices.usesDisMax(collection); SolrQuery query; if (!collection.isOpenSearch()) { query = SearchServicesImpl.toSolrQuery(simpleSearch, usesDisMax, true, includeSingleValueFacets, notIncludedOnly);/*from w ww . j av a 2 s .c o m*/ } else { query = SearchServicesImpl.toSolrQuery(simpleSearch, usesDisMax, false, true, false); } query.setParam(ConstellioSolrQueryParams.COLLECTION_NAME, simpleSearch.getCollectionName()); query.setParam(ConstellioSolrQueryParams.LUCENE_QUERY, simpleSearch.getLuceneQuery(includeSingleValueFacets, true)); query.setParam(ConstellioSolrQueryParams.SIMPLE_SEARCH, simpleSearch.toSimpleParams().toString()); if (user != null) { query.setParam(ConstellioSolrQueryParams.USER_ID, "" + user.getId()); } if (StringUtils.isEmpty(query.getQuery())) { query.setQuery(SimpleSearch.SEARCH_ALL); query.setRequestHandler("/elevate"); } query.set("shards.qt", "/elevate"); query.setRequestHandler("/elevate"); query.setRows(row); query.setStart(start); query.setHighlight(true); query.setHighlightFragsize(100); query.setHighlightSnippets(2); query.setFacet(true); query.setFacetLimit(400); query.setFacetMinCount(1); if (collection.isOpenSearch()) { query.setParam("openSearchURL", collection.getOpenSearchURL()); Locale locale = simpleSearch.getSingleSearchLocale(); if (locale != null) { query.setParam("lang", locale.getLanguage()); } } else { for (CollectionFacet collectionFacet : collection.getCollectionFacets()) { if (customFieldFacets == null && collectionFacet.isFieldFacet()) { IndexField indexField = collectionFacet.getFacetField(); String indexFieldName = indexField.getName(); if (!notIncludedOnly) { query.addFacetField(indexFieldName); } else { SearchedFacet searchedFacet = simpleSearch.getSearchedFacet(indexFieldName); if (searchedFacet != null) { if (!searchedFacet.getIncludedValues().isEmpty()) { StringBuffer sbEx = new StringBuffer(); sbEx.append("{!ex=dt}"); // sbEx.append("{!ex="); // boolean first = true; // for (String includedValue : searchedFacet.getIncludedValues()) { // if (first) { // first = false; // } else { // sbEx.append(","); // } // sbEx.append(includedValue); // } // sbEx.append("}"); // query.setParam("facet.field", sbEx.toString() + indexFieldName); query.addFacetField(sbEx.toString() + indexFieldName); } else { query.addFacetField(indexFieldName); } } } } else if (customQueryFacets == null && collectionFacet.isQueryFacet()) { // Modification Rida, remplacement de collectionFacet.getLabels() par // collectionFacet.getLabelledValues() // for (I18NLabel valueLabels : collectionFacet.getLabels()) { for (I18NLabel valueLabels : collectionFacet.getLabelledValues()) { String facetQuery = valueLabels.getKey(); query.addFacetQuery(facetQuery); } } } if (customFieldFacets != null) { for (String facetField : customFieldFacets) { if (!notIncludedOnly) { query.addFacetField(facetField); } else { StringBuffer sbEx = new StringBuffer(); sbEx.append("{!ex=dt}"); // sbEx.append("{!ex="); // boolean first = true; // for (String includedValue : searchedFacet.getIncludedValues()) { // if (first) { // first = false; // } else { // sbEx.append(","); // } // sbEx.append(includedValue); // } // sbEx.append("}"); query.setParam("facet.field", sbEx.toString() + facetField); } } } if (customQueryFacets != null) { for (String facetQuery : customQueryFacets) { query.addFacetQuery(facetQuery); } } } return query; }
From source file:com.doculibre.constellio.services.IndexFieldServicesImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/*from ww w . j av a 2 s . c o m*/ public List<String> suggestValues(IndexField indexField, String text) { List<String> values = new ArrayList<String>(); RecordCollection collection = indexField.getRecordCollection(); SolrServices solrServices = ConstellioSpringUtils.getSolrServices(); SolrServer solrServer = solrServices.getSolrServer(collection); if (solrServer != null) { SolrQuery query = new SolrQuery(); query.setRequestHandler("/admin/luke"); query.setParam(CommonParams.FL, indexField.getName()); query.setParam(LukeRequestHandler.NUMTERMS, "" + 100); if (text != null) { query.setQuery(indexField.getName() + ":" + text + "*"); } if (collection.isOpenSearch()) { query.setParam("openSearchURL", collection.getOpenSearchURL()); } try { QueryResponse queryResponse = solrServer.query(query); NamedList<Object> fields = (NamedList<Object>) queryResponse.getResponse().get("fields"); if (fields != null) { NamedList<Object> field = (NamedList<Object>) fields.get(indexField.getName()); if (field != null) { NamedList<Object> topTerms = (NamedList<Object>) field.get("topTerms"); if (topTerms != null) { for (Map.Entry<String, Object> topTerm : topTerms) { String topTermKey = topTerm.getKey(); if (text == null || topTermKey.toLowerCase().startsWith(text.toLowerCase())) { values.add(topTerm.getKey()); } } } } } } catch (SolrServerException e) { throw new RuntimeException(e); } } return values; }
From source file:com.doculibre.constellio.services.SearchServicesImpl.java
License:Open Source License
@Override public QueryResponse search(SimpleSearch simpleSearch, int start, int rows, SearchParams searchParams, ConstellioUser user) {/*w ww. ja v a 2 s. c o m*/ QueryResponse queryResponse; String collectionName = simpleSearch.getCollectionName(); if (collectionName != null) { RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices(); RecordServices recordServices = ConstellioSpringUtils.getRecordServices(); RecordCollection collection = collectionServices.get(collectionName); SolrServices solrServices = ConstellioSpringUtils.getSolrServices(); Boolean usesDisMax = solrServices.usesDisMax(collection); SolrQuery query; if (!collection.isOpenSearch()) { query = toSolrQuery(simpleSearch, usesDisMax, true, true); } else { query = toSolrQuery(simpleSearch, usesDisMax, false, true); } // displayQuery(query); String luceneQuery = simpleSearch.getLuceneQuery(); query.setParam(ConstellioSolrQueryParams.LUCENE_QUERY, luceneQuery); query.setParam(ConstellioSolrQueryParams.SIMPLE_SEARCH, simpleSearch.toSimpleParams().toString()); query.setParam(ConstellioSolrQueryParams.COLLECTION_NAME, collectionName); if (user != null) { query.setParam(ConstellioSolrQueryParams.USER_ID, "" + user.getId()); } String queryString = query.getQuery(); if (StringUtils.isEmpty(queryString)) { queryString = SimpleSearch.SEARCH_ALL; } List<Record> pendingExclusions = recordServices.getPendingExclusions(collection); while (!pendingExclusions.isEmpty()) { IndexingManager indexingManager = IndexingManager.get(collection); if (indexingManager.isActive()) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } pendingExclusions = recordServices.getPendingExclusions(collection); } else { return null; } } // SolrQuery query = new SolrQuery(); query.set("collectionName", simpleSearch.getCollectionName()); // query.setQuery(luceneQuery); query.set("shards.qt", "/elevate"); query.setRequestHandler("/elevate"); // nb rsultats par page query.setRows(rows); // page de dbut query.setStart(start); query.setHighlight(searchParams.isHighlightingEnabled()); if (searchParams.isHighlightingEnabled()) { query.setHighlightFragsize(searchParams.getFragsize()); query.setHighlightSnippets(searchParams.getSnippets()); } if (simpleSearch.getSortField() != null) { ORDER order = SimpleSearch.SORT_DESCENDING.equals(simpleSearch.getSortOrder()) ? ORDER.desc : ORDER.asc; IndexFieldServices indexFieldServices = ConstellioSpringUtils.getIndexFieldServices(); IndexField indexField = indexFieldServices.get(simpleSearch.getSortField(), collection); if (indexField != null) { IndexField sortIndexField = indexFieldServices.getSortFieldOf(indexField); if (sortIndexField != null) { query.setSort(sortIndexField.getName(), order); } } } if (collection.isOpenSearch()) { query.setParam("openSearchURL", collection.getOpenSearchURL()); Locale locale = simpleSearch.getSingleSearchLocale(); if (locale != null) { query.setParam("lang", locale.getLanguage()); } } if (searchParams.getHighlightedFields() == null) { IndexField defaultSearchField = collection.getDefaultSearchIndexField(); query.addHighlightField(defaultSearchField.getName()); for (CopyField copyFieldDest : defaultSearchField.getCopyFieldsDest()) { IndexField copyIndexFieldSource = copyFieldDest.getIndexFieldSource(); if (copyIndexFieldSource != null && !copyIndexFieldSource.isTitleField() && copyIndexFieldSource.isHighlighted()) { query.addHighlightField(copyIndexFieldSource.getName()); } } IndexField titleField = collection.getTitleIndexField(); if (titleField != null && titleField.isHighlighted()) { query.addHighlightField(titleField.getName()); } } else { for (String highlightedField : searchParams.getHighlightedFields()) { IndexField field = collection.getIndexField(highlightedField); if (field != null) { query.addHighlightField(highlightedField); } } } SolrServer server = SolrCoreContext.getSolrServer(collectionName); if (server != null) { try { // displayQuery(query); queryResponse = server.query(query); } catch (SolrServerException e) { queryResponse = null; } } else { queryResponse = null; } // if (queryResponse != null && !collection.isOpenSearch()) { // StatsCompiler statCompiler = StatsCompiler.getInstance(); // try { // statCompiler.saveStats(collectionName, SolrLogContext.getStatsSolrServer(), // SolrLogContext.getStatsCompileSolrServer(), queryResponse, luceneQuery); // } catch (SolrServerException e) { // throw new RuntimeException(e); // } catch (IOException e) { // throw new RuntimeException(e); // } // } } else { queryResponse = null; } // improveQueryResponse(collectionName, queryResponse); // System.out.println("Response size" + queryResponse.getResults().getNumFound()); return queryResponse; }