List of usage examples for org.apache.solr.client.solrj SolrQuery getQuery
public String getQuery()
From source file:com.sindicetech.siren.demo.ncpr.NCPRQuery.java
License:Open Source License
public void query(final SolrQuery query) throws SolrServerException { final QueryResponse rsp = this.server.query(query); logger.info("Query: {}", query.getQuery()); logger.info("Hits: {}", rsp.getResults().getNumFound()); logger.info("Execution time: {} ms", rsp.getQTime()); logger.info(rsp.toString());/*w ww . j av a2 s. co m*/ }
From source file:com.ushahidi.swiftriver.core.solr.repository.DropDocumentRepositoryImplTest.java
License:Open Source License
@Test public void find() throws Exception { PageRequest page = new PageRequest(0, 20); String searchTerm = "test search phrase"; QueryResponse mockQueryResponse = mock(QueryResponse.class); List<DropDocument> foundDocuments = new ArrayList<DropDocument>(); when(mockSolrServer.query(any(SolrQuery.class))).thenReturn(mockQueryResponse); when(mockQueryResponse.getBeans(DropDocument.class)).thenReturn(foundDocuments); dropSearchRepository.find(searchTerm, page); ArgumentCaptor<SolrQuery> solrQueryArgument = ArgumentCaptor.forClass(SolrQuery.class); verify(mockSolrServer).query(solrQueryArgument.capture()); SolrQuery solrQuery = solrQueryArgument.getValue(); assertEquals(searchTerm, solrQuery.getQuery()); assertEquals("edismax", solrQuery.get("defType")); }
From source file:com.ushahidi.swiftriver.core.solr.repository.DropDocumentRepositoryImplTest.java
License:Open Source License
@Test public void findInRiverWithBoundingBox() throws Exception { QueryResponse mockQueryResponse = mock(QueryResponse.class); List<DropDocument> foundDocuments = new ArrayList<DropDocument>(); when(mockSolrServer.query(any(SolrQuery.class))).thenReturn(mockQueryResponse); when(mockQueryResponse.getBeans(DropDocument.class)).thenReturn(foundDocuments); Pageable pageRequest = new PageRequest(0, 20); DropFilter dropFilter = new DropFilter(); dropFilter.setBoundingBox("36.8,-122.75,37.8,-121.75"); dropSearchRepository.findInRiver(1L, dropFilter, pageRequest); ArgumentCaptor<SolrQuery> solrQueryArgument = ArgumentCaptor.forClass(SolrQuery.class); verify(mockSolrServer).query(solrQueryArgument.capture()); SolrQuery solrQuery = solrQueryArgument.getValue(); String[] expectedFilterQuery = new String[] { "riverId:(1)", "geo:[36.8,-122.75 TO 37.8,-121.75]" }; String[] actualFilterQuery = solrQuery.getFilterQueries(); assertEquals("*:*", solrQuery.getQuery()); assertEquals(2, actualFilterQuery.length); assertEquals(expectedFilterQuery[0], actualFilterQuery[0]); assertEquals(expectedFilterQuery[1], actualFilterQuery[1]); }
From source file:com.ushahidi.swiftriver.core.solr.repository.DropDocumentRepositoryImplTest.java
License:Open Source License
@Test public void findInBucketWithBoundingBox() throws Exception { QueryResponse mockQueryResponse = mock(QueryResponse.class); List<DropDocument> foundDocuments = new ArrayList<DropDocument>(); when(mockSolrServer.query(any(SolrQuery.class))).thenReturn(mockQueryResponse); when(mockQueryResponse.getBeans(DropDocument.class)).thenReturn(foundDocuments); Pageable pageRequest = new PageRequest(0, 20); DropFilter dropFilter = new DropFilter(); dropFilter.setBoundingBox("40,-74,41,-73"); dropSearchRepository.findInBucket(20L, dropFilter, pageRequest); ArgumentCaptor<SolrQuery> solrQueryArgument = ArgumentCaptor.forClass(SolrQuery.class); verify(mockSolrServer).query(solrQueryArgument.capture()); SolrQuery solrQuery = solrQueryArgument.getValue(); String[] expectedFilterQuery = new String[] { "bucketId:(20)", "geo:[40.0,-74.0 TO 41.0,-73.0]" }; String[] actualFilterQuery = solrQuery.getFilterQueries(); assertEquals("*:*", solrQuery.getQuery()); assertEquals(2, actualFilterQuery.length); assertEquals(expectedFilterQuery[0], actualFilterQuery[0]); assertEquals(expectedFilterQuery[1], actualFilterQuery[1]); }
From source file:ddf.catalog.cache.solr.impl.SolrFilterDelegate.java
License:Open Source License
@Override public SolrQuery not(SolrQuery operand) { return new SolrQuery(" NOT " + operand.getQuery()); }
From source file:ddf.catalog.cache.solr.impl.SolrFilterDelegate.java
License:Open Source License
private SolrQuery logicalOperator(List<SolrQuery> operands, String operator) { if (operands == null || operands.size() < 1) { throw new UnsupportedOperationException("[" + operator + "] operation must contain 1 or more filters."); }//ww w. java 2 s .c o m // Due to a bug in how solr parses queries, a sorted spatial operand (combined with any // other operand) // must come first in any given expression, so we have to add it to the beginning of the // operand list. for (int i = 0; i < operands.size(); i++) { SolrQuery operand = operands.get(i); if (operand == null) { throw new UnsupportedOperationException("Null operand found"); } String operandAsString = operand.toString(); try { if (operandAsString.contains(URLEncoder.encode(SCORE_DISTANCE, "UTF-8"))) { SolrQuery temp = operands.get(0); operands.set(0, operand); operands.set(i, temp); break; } } catch (UnsupportedEncodingException e) { LOGGER.info("Unable to encode {}", SCORE_DISTANCE, e); } } int startIndex = 0; SolrQuery query = operands.get(startIndex); startIndex++; if (query == null) { throw new UnsupportedOperationException( "Query was not interpreted properly. Query should not be null."); } StringBuilder builder = new StringBuilder(); builder.append(START_PAREN); builder.append(query.getQuery()); for (int i = startIndex; i < operands.size(); i++) { SolrQuery localQuery = operands.get(i); if (localQuery != null) { String localPhrase = localQuery.getQuery(); builder.append(operator + localPhrase); } else { throw new UnsupportedOperationException( "Query was not interpreted properly. Query should not be null."); } } builder.append(END_PAREN); query.setQuery(builder.toString()); return query; }
From source file:ddf.catalog.source.solr.SolrFilterDelegate.java
License:Open Source License
private SolrQuery logicalOperator(List<SolrQuery> operands, String operator) { if (operands == null || operands.size() < 1) { throw new UnsupportedOperationException("[" + operator + "] operation must contain 1 or more filters."); }/* ww w .ja v a 2 s. co m*/ // Due to a bug in how solr parses queries, a sorted spatial operand (combined with any // other operand) // must come first in any given expression, so we have to add it to the beginning of the // operand list. for (int i = 0; i < operands.size(); i++) { SolrQuery operand = operands.get(i); if (operand == null) { throw new UnsupportedOperationException("Null operand found"); } String operandAsString = operand.toString(); try { if (operandAsString.contains(URLEncoder.encode(SCORE_DISTANCE, "UTF-8"))) { SolrQuery temp = operands.get(0); operands.set(0, operand); operands.set(i, temp); break; } } catch (UnsupportedEncodingException e) { LOGGER.info("Unable to encode {}", SCORE_DISTANCE, e); } } int startIndex = 0; SolrQuery query = operands.get(startIndex); startIndex++; if (query == null) { throw new UnsupportedOperationException( "Query was not interpreted properly. Query should not be null."); } StringBuilder builder = new StringBuilder(); builder.append(START_PAREN); builder.append(query.getQuery()); for (int i = startIndex; i < operands.size(); i++) { SolrQuery localQuery = operands.get(i); if (localQuery != null) { builder.append(operator).append(localQuery.getQuery()); } else { throw new UnsupportedOperationException( "Query was not interpreted properly. Query should not be null."); } } builder.append(END_PAREN); query.setQuery(builder.toString()); return query; }
From source file:ddf.catalog.source.solr.SolrFilterDelegateTest.java
License:Open Source License
@Test public void intersectsWithInvalidJtsWkt() { // given a geospatial property stub(mockResolver.getField("testProperty", AttributeFormat.GEOMETRY, false)) .toReturn("testProperty_geohash_index"); // when the delegate intersects on WKT not handled by JTS SolrQuery query = toTest.intersects("testProperty", "invalid JTS wkt"); // then return a valid Solr query using the given WKT assertThat(query.getQuery(), is("testProperty_geohash_index:\"Intersects(invalid JTS wkt)\"")); }
From source file:ddf.catalog.source.solr.SolrFilterDelegateTest.java
License:Open Source License
@Test public void intersectsWithValidJtsWkt() { // given a geospatial property stub(mockResolver.getField("testProperty", AttributeFormat.GEOMETRY, false)) .toReturn("testProperty_geohash_index"); // when the delegate intersects on WKT not handled by JTS SolrQuery query = toTest.intersects("testProperty", "POINT(1 0)"); // then return a valid Solr query using the given WKT assertThat(query.getQuery(), startsWith("testProperty_geohash_index:\"Intersects(BUFFER(POINT(1.0 0.0), ")); }
From source file:ddf.catalog.source.solr.SolrFilterDelegateTest.java
License:Open Source License
@Test public void reservedSpecialCharactersIsEqual() { // given a text property stub(mockResolver.getField("testProperty", AttributeFormat.STRING, true)) .toReturn("testProperty_txt_index"); // when searching for exact reserved characters SolrQuery equalQuery = toTest.propertyIsEqualTo("testProperty", "+ - && || ! ( ) { } [ ] ^ \" ~ :", true); // then return escaped special characters in the query assertThat(equalQuery.getQuery(), is("testProperty_txt_index:\"\\+ \\- \\&& \\|| \\! \\( \\) \\{ \\} \\[ \\] \\^ \\\" \\~ \\:\"")); }