ZoomaLuceneSearchService extends Initializable { protected <T> List<T> doQuery(Query q, LuceneDocumentMapper<T> mapper) { return doQuery(q, mapper, -1); }  void setConfigurationProperties(Properties configuration); void setIndex(Directory index); void setSimilarity(Similarity similarity); Query formulateExactCombinedQuery(Query[] q, String fieldName, Object[] items);  }
@Test public void testDoQueryString() { String querystr = "foo"; try { Query q = new QueryParser("name", analyzer).parse(querystr); Collection<String> results = searchService.doQuery(q, new SingleFieldStringMapper("name")); assertEquals("Wrong number of results", 4, results.size()); assertTrue(results.contains("foo")); assertTrue(results.contains("foo bar")); assertTrue(results.contains("foo bar baz")); assertTrue(results.contains("foo baz")); q = new QueryParser("uri", analyzer).parse(querystr); results = searchService.doQuery(q, new SingleFieldStringMapper("uri")); assertEquals("Wrong number of results", 4, results.size()); assertTrue(results.contains(convertToURI("foo").toString())); assertTrue(results.contains(convertToURI("foo bar").toString())); assertTrue(results.contains(convertToURI("foo bar baz").toString())); assertTrue(results.contains(convertToURI("foo baz").toString())); } catch (ParseException e) { e.printStackTrace(); fail(); } catch (IOException e) { e.printStackTrace(); fail(); } }
@Test public void testDoQueryObject() { try { String querystr = "foo"; Query q = new QueryParser("uri", analyzer).parse(querystr); Collection<Identifiable> results = searchService.doQuery(q, new SingleFieldURIMapper("uri"), dao); assertEquals("Wrong number of results", 1, results.size()); assertTrue("Identifiable 'foo' not found", results.contains(foo)); querystr = "bar"; q = new QueryParser("uri", analyzer).parse(querystr); results = searchService.doQuery(q, new SingleFieldURIMapper("uri"), dao); assertEquals("Wrong number of results", 1, results.size()); assertTrue("Identifiable 'bar' not found", results.contains(bar)); querystr = "baz"; q = new QueryParser("uri", analyzer).parse(querystr); results = searchService.doQuery(q, new SingleFieldURIMapper("uri"), dao); assertEquals("Wrong number of results", 1, results.size()); assertTrue("Identifiable 'baz' not found", results.contains(baz)); } catch (ParseException e) { e.printStackTrace(); fail(); } catch (IOException e) { e.printStackTrace(); fail(); } }
@Test public void testDoQueryForSubstring() { String field = "name"; String searchString = "foo"; List<String> terms = new ArrayList<>(); try (TokenStream stream = analyzer.tokenStream(field, new StringReader(QueryParser.escape(searchString)))) { stream.reset(); CharTermAttribute termAtt = stream.addAttribute(CharTermAttribute.class); while (stream.incrementToken()) { terms.add(termAtt.toString()); } } catch (IOException e) { e.printStackTrace(); fail("Can't parse search string"); } Query q; if (terms.size() == 1) { q = new TermQuery(new Term(field, terms.get(0))); } else { List<SpanQuery> stqs = new ArrayList<>(); for (String term : terms) { stqs.add(new SpanTermQuery(new Term(field, term))); } q = new SpanNearQuery(stqs.toArray(new SpanQuery[stqs.size()]), 1, false); } Collection<String> results; try { results = searchService.doQuery(q, new SingleFieldStringMapper(field)); for (String s : results) { System.out.println("Next result: '" + s + "'"); } assertEquals("Wrong number of results", 4, results.size()); assertTrue(results.contains("foo")); assertTrue(results.contains("foo bar")); assertTrue(results.contains("foo bar baz")); assertTrue(results.contains("foo baz")); } catch (Exception e) { e.printStackTrace(); fail("Query failed"); } }