Java Code Examples for org.apache.solr.client.solrj.SolrQuery#setFilterQueries()

The following examples show how to use org.apache.solr.client.solrj.SolrQuery#setFilterQueries() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SolrDataSet.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
public void setSolrQueryParameters(SolrQuery solrQuery, Map parametersMap) {
	try {
		JSONObject jsonConfiguration = new JSONObject(configuration);
		List<Couple<String, String>> filterQueries = getListProp(SolrDataSetConstants.SOLR_FILTER_QUERY, jsonConfiguration, true);
		if (filterQueries != null && !filterQueries.isEmpty()) {
			String[] array = new String[filterQueries.size()];
			for (int i = 0; i < array.length; i++) {
				if (filterQueries != null && filterQueries.get(i) != null && filterQueries.get(i).getSecond() != null
						&& filterQueries.get(i).getSecond().contains(",")) {
					String multivalue = filterQueries.get(i).getSecond().replace(",", " OR ");
					multivalue = "(" + multivalue + ")";
					array[i] = filterQueries.get(i).getFirst() + ":" + multivalue;
				} else
					array[i] = filterQueries.get(i).getFirst() + ":" + filterQueries.get(i).getSecond();
			}
			solrQuery.setFilterQueries(array);
		}

	} catch (JSONException e) {
		throw new ConfigurationException("Problems in configuration of data proxy", e);
	}
}
 
Example 2
Source File: MCROAIQuerySetResolver.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private SolrQuery getQuery() {
    SolrQuery solrQuery = new SolrQuery();
    // query
    String idQuery = getResult().stream()
        .map(getIdentifier())
        .map(MCRSolrUtils::escapeSearchValue)
        .collect(Collectors.joining(" OR ", "id:(", ")"));
    solrQuery.setQuery(idQuery);
    solrQuery.setFilterQueries(query);
    solrQuery.setFields("id");
    solrQuery.setRows(getResult().size());
    // request handler
    solrQuery.setRequestHandler(
        MCRConfiguration2.getString(getConfigPrefix() + "Search.RequestHandler").orElse("/select"));
    return solrQuery;
}
 
Example 3
Source File: ContentEditor.java    From jease with GNU General Public License v3.0 6 votes vote down vote up
public String checkDuplication() {
	try {
		String solrurl = jease.Registry.getParameter(jease.Names.JEASE_SOLR_URL, "");
		SolrClient client = new HttpSolrClient.Builder(solrurl).build();

		SolrQuery query = new SolrQuery();
		query.setQuery("*:*");
		query.setFilterQueries("jeaseid:\"" + id.getValue() + "\" ");
		query.setFilterQueries("jeasepath:\"" + getNode().getPath() + "\"");
		SolrDocumentList results = client.query(query).getResults();
		if (results.size() > 0) {
			return results.get(0).getFieldValue("id").toString();
		}
	} catch (Exception s) {
		s.printStackTrace();
	}
	return "";
}
 
Example 4
Source File: SolrUtil.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
public static void removeDoubleOrTripleEscapeFromFilters(SolrQuery solrQuery) {
  String[] filterQueries = solrQuery.getFilterQueries();
  List<String> newArray = new ArrayList<>();
  if (filterQueries != null && filterQueries.length > 0) {
    for (String filterQuery : filterQueries) {
      newArray.add(filterQuery.replaceAll("\\\\\\\\\\\\|\\\\\\\\", "\\\\"));
    }
  }
  solrQuery.setFilterQueries(newArray.toArray(new String[0]));
}
 
Example 5
Source File: DefaultQueryParser.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * Set filter filter queries for
 * {@link org.apache.solr.client.solrj.SolrQuery}
 *
 * @param solrQuery
 * @param filterQueries
 */
protected void appendFilterQuery(SolrQuery solrQuery, List<FilterQuery> filterQueries) {
	if (CollectionUtils.isEmpty(filterQueries)) {
		return;
	}
	List<String> filterQueryStrings = getFilterQueryStrings(filterQueries);

	if (!filterQueryStrings.isEmpty()) {
		solrQuery.setFilterQueries(convertStringListToArray(filterQueryStrings));
	}
}
 
Example 6
Source File: SolrDataSet.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void initSolrConfiguration(JSONObject jsonConf, boolean resolveParams) {
	try {
		solrConfiguration = new SolrConfiguration();
		String address = getProp(SolrDataSetConstants.SOLR_BASE_ADDRESS, jsonConf, false, resolveParams);
		solrConfiguration.setUrl(address);
		String collection = getProp(SolrDataSetConstants.SOLR_COLLECTION, jsonConf, false, resolveParams);
		solrConfiguration.setCollection(collection);
		SolrQuery solrQuery = new SolrQuery();
		String query = getProp(SolrDataSetConstants.SOLR_QUERY, jsonConf, true, resolveParams);
		if (query == null || query.isEmpty()) {
			query = SOLR_DEFAULT_QUERY;
		}
		solrQuery.setQuery(query);
		String fieldList = getProp(SolrDataSetConstants.SOLR_FIELD_LIST, jsonConf, true, resolveParams);
		if (fieldList != null && !fieldList.trim().isEmpty()) {
			solrQuery.setFields(fieldList.split(","));
		}
		String solrFields = getProp(SolrDataSetConstants.SOLR_FIELDS, jsonConf, true, resolveParams);
		if (solrFields != null && !solrFields.trim().isEmpty()) {
			solrConfiguration.setSolrFields(solrFields);
		}

		List<Couple<String, String>> filterQueries = getListProp(SolrDataSetConstants.SOLR_FILTER_QUERY, jsonConf, true);
		if (filterQueries != null && !filterQueries.isEmpty()) {
			String[] array = new String[filterQueries.size()];
			for (int i = 0; i < array.length; i++) {
				array[i] = filterQueries.get(i).getFirst() + ":" + filterQueries.get(i).getSecond();
			}

			solrQuery.setFilterQueries(array);
		}
		solrQuery.setFacet(isFacet());
		solrConfiguration.setSolrQuery(solrQuery);
	} catch (JSONException e) {
		throw new ConfigurationException("Problems in configuration of solr query", e);
	}
}
 
Example 7
Source File: SolrDataSet.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void initSolrConfiguration(JSONObject jsonConf, boolean resolveParams, UserProfile userProfile) {
	try {
		solrConfiguration = new SolrConfiguration();
		String address = getProp(SolrDataSetConstants.SOLR_BASE_ADDRESS, jsonConf, false, resolveParams);
		solrConfiguration.setUrl(address);
		String collection = getProp(SolrDataSetConstants.SOLR_COLLECTION, jsonConf, false, resolveParams);
		solrConfiguration.setCollection(collection);
		SolrQuery solrQuery = new SolrQuery();
		String query = getProp(SolrDataSetConstants.SOLR_QUERY, jsonConf, true, resolveParams);
		if (query == null || query.isEmpty()) {
			query = SOLR_DEFAULT_QUERY;
		}
		solrQuery.setQuery(query);
		String fieldList = getProp(SolrDataSetConstants.SOLR_FIELD_LIST, jsonConf, true, resolveParams);
		if (fieldList != null && !fieldList.trim().isEmpty()) {
			solrQuery.setFields(fieldList.split(","));
		}
		String solrFields = getProp(SolrDataSetConstants.SOLR_FIELDS, jsonConf, true, resolveParams);
		if (solrFields != null && !solrFields.trim().isEmpty()) {
			solrConfiguration.setSolrFields(solrFields);
		}

		List<Couple<String, String>> filterQueries = getListProp(SolrDataSetConstants.SOLR_FILTER_QUERY, jsonConf, true, userProfile);
		if (filterQueries != null && !filterQueries.isEmpty()) {
			String[] array = new String[filterQueries.size()];
			for (int i = 0; i < array.length; i++) {
				array[i] = filterQueries.get(i).getFirst() + ":" + filterQueries.get(i).getSecond();
			}

			solrQuery.setFilterQueries(array);
		}
		solrQuery.setFacet(isFacet());
		solrConfiguration.setSolrQuery(solrQuery);
	} catch (JSONException e) {
		throw new ConfigurationException("Problems in configuration of solr query", e);
	}
}
 
Example 8
Source File: DistributedDebugComponentTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
// commented out on: 24-Dec-2018   @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Sep-2018
public void testCompareWithNonDistributedRequest() throws SolrServerException, IOException {
  SolrQuery query = new SolrQuery();
  query.setQuery("id:1 OR id:2");
  query.setFilterQueries("id:[0 TO 10]", "id:[0 TO 5]");
  query.setRows(1);
  query.setSort("id", SolrQuery.ORDER.asc); // thus only return id:1 since rows 1
  query.set("debug",  "true");
  query.set("distrib", "true");
  query.setFields("id");
  if (random().nextBoolean()) { // can affect rb.onePassDistributedQuery
    query.addField("text");
  }
  query.set(ShardParams.DISTRIB_SINGLE_PASS, random().nextBoolean());
  query.set("shards", shard1 + "," + shard2);
  QueryResponse distribResponse = collection1.query(query);
  
  // same query but not distributed
  query.set("distrib", "false");
  query.remove("shards");
  QueryResponse nonDistribResponse = collection1.query(query);
  
  assertNotNull(distribResponse.getDebugMap().get("track"));
  assertNull(nonDistribResponse.getDebugMap().get("track"));
  assertEquals(distribResponse.getDebugMap().size() - 1, nonDistribResponse.getDebugMap().size());
  
  assertSectionEquals(distribResponse, nonDistribResponse, "explain");
  assertSectionEquals(distribResponse, nonDistribResponse, "rawquerystring");
  assertSectionEquals(distribResponse, nonDistribResponse, "querystring");
  assertSectionEquals(distribResponse, nonDistribResponse, "parsedquery");
  assertSectionEquals(distribResponse, nonDistribResponse, "parsedquery_toString");
  assertSectionEquals(distribResponse, nonDistribResponse, "QParser");
  assertSectionEquals(distribResponse, nonDistribResponse, "filter_queries");
  assertSectionEquals(distribResponse, nonDistribResponse, "parsed_filter_queries");
  
  // timing should have the same sections:
  assertSameKeys((NamedList<?>)nonDistribResponse.getDebugMap().get("timing"), (NamedList<?>)distribResponse.getDebugMap().get("timing"));
}
 
Example 9
Source File: SearchService.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Integrity check.
 */
public void checkIndex()
{
   try
   {
      SolrQuery query = new SolrQuery("*:*");
      query.setFilterQueries("*");
      query.setStart(0);
      Iterator<SolrDocument> it = solrDao.scroll(query);
      while (it.hasNext())
      {
         SolrDocument doc = it.next();
         Long pid = (Long) doc.get("id");
         Product product = productService.systemGetProduct(pid);
         if (product == null)
         {
            Long id = (Long) doc.getFieldValue("id");
            LOGGER.warn("Removing unknown product " + id + " from solr index");
            try
            {
               solrDao.remove(id);
               // decrease the offset, because a product has been removed
               query.setStart(query.getStart() - 1);
            }
            catch (IOException e)
            {
               LOGGER.error("Cannot remove Solr entry " + id, e);
            }
         }
      }
   }
   catch (IOException|SolrServerException ex)
   {
      LOGGER.error("Cannot check the index", ex);
   }
}
 
Example 10
Source File: SolrEntityProcessor.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected QueryResponse doQuery() {
  SolrEntityProcessor.this.queryString = context.getResolvedEntityAttribute(QUERY);
  if (SolrEntityProcessor.this.queryString == null) {
    throw new DataImportHandlerException(
        DataImportHandlerException.SEVERE,
        "SolrEntityProcessor: parameter 'query' is required"
    );
  }

  String rowsP = context.getResolvedEntityAttribute(CommonParams.ROWS);
  if (rowsP != null) {
    rows = Integer.parseInt(rowsP);
  }

  String sortParam = context.getResolvedEntityAttribute(CommonParams.SORT);
  
  String fqAsString = context.getResolvedEntityAttribute(CommonParams.FQ);
  if (fqAsString != null) {
    SolrEntityProcessor.this.filterQueries = fqAsString.split(",");
  }

  String fieldsAsString = context.getResolvedEntityAttribute(CommonParams.FL);
  if (fieldsAsString != null) {
    SolrEntityProcessor.this.fields = fieldsAsString.split(",");
  }
  SolrEntityProcessor.this.requestHandler = context.getResolvedEntityAttribute(CommonParams.QT);
 

  SolrQuery solrQuery = new SolrQuery(queryString);
  solrQuery.setRows(rows);
  
  if (sortParam!=null) {
    solrQuery.setParam(CommonParams.SORT, sortParam);
  }
  
  passNextPage(solrQuery);
  
  if (fields != null) {
    for (String field : fields) {
      solrQuery.addField(field);
    }
  }
  solrQuery.setRequestHandler(requestHandler);
  solrQuery.setFilterQueries(filterQueries);
  
  
  QueryResponse response = null;
  try {
    response = solrClient.query(solrQuery);
  } catch (SolrServerException | IOException | SolrException e) {
    if (ABORT.equals(onError)) {
      wrapAndThrow(SEVERE, e);
    } else if (SKIP.equals(onError)) {
      wrapAndThrow(DataImportHandlerException.SKIP_ROW, e);
    }
  }
  
  if (response != null) {
    SolrEntityProcessor.this.rowIterator = createNextPageIterator(response);
  }
  return response;
}