private final void populateTerm( final HQuery hQuery, final boolean isShould, final boolean isMust, final Query subQueryL) throws FederatedSearchException { HTerm hTerm = new HTerm(); hTerm.isShould = isShould; hTerm.isMust = isMust; hTerm.boost = subQueryL.getBoost(); hQuery.terms.add(hTerm); if (subQueryL instanceof TermQuery) { TermQuery lTerm = (TermQuery) subQueryL; hTerm.type = lTerm.getTerm().field(); hTerm.text = lTerm.getTerm().text(); } else if (subQueryL instanceof FuzzyQuery) { FuzzyQuery lTerm = (FuzzyQuery) subQueryL; hTerm.isFuzzy = true; hTerm.type = lTerm.getTerm().field(); hTerm.text = lTerm.getTerm().text(); } else if (subQueryL instanceof TermRangeQuery) { TermRangeQuery lTerm = (TermRangeQuery) subQueryL; hTerm.isFuzzy = false; hTerm.type = lTerm.getField(); hTerm.minRange = lTerm.getLowerTerm(); hTerm.maxRange = lTerm.getUpperTerm(); } else { throw new FederatedSearchException( "HQueryParser: Not Implemented Query :" + subQueryL.getClass().toString()); } }
@Test public void testQueryText() { Float boost = 0.7f; Object[] values = new Object[] {"houses", "cats"}; Schema schema = schema().mapper("name", textMapper()).defaultAnalyzer("english").build(); ContainsCondition condition = new ContainsCondition(boost, "name", values); Query query = condition.query(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", BooleanQuery.class, query.getClass()); BooleanQuery booleanQuery = (BooleanQuery) query; assertEquals("Query boost is wrong", 0.7f, query.getBoost(), 0); BooleanClause[] clauses = booleanQuery.getClauses(); assertEquals( "Query is wrong", "hous", ((TermQuery) clauses[0].getQuery()).getTerm().bytes().utf8ToString()); assertEquals( "Query is wrong", "cat", ((TermQuery) clauses[1].getQuery()).getTerm().bytes().utf8ToString()); }
synchronized void evictIfNecessary() { // under a lock to make sure that mostRecentlyUsedQueries and cache keep sync'ed if (requiresEviction()) { Iterator<Query> iterator = mostRecentlyUsedQueries.iterator(); do { final Query query = iterator.next(); final int size = mostRecentlyUsedQueries.size(); iterator.remove(); if (size == mostRecentlyUsedQueries.size()) { // size did not decrease, because the hash of the query changed since it has been // put into the cache throw new ConcurrentModificationException( "Removal from the cache failed! This " + "is probably due to a query which has been modified after having been put into " + " the cache or a badly implemented clone(). Query class: [" + query.getClass() + "], query: [" + query + "]"); } onEviction(query); } while (iterator.hasNext() && requiresEviction()); } }
public static void wildTest() throws Exception { Directory dir = FSDirectory.open(new File(tempDir)); IndexSearcher searcher = new IndexSearcher(dir); // 来对索引进行检索 // WildcardQuery query = new WildcardQuery(new Term("path", ("www." + "si\u003fna" + "*" ))); QueryParser p = new QueryParser(Version.LUCENE_36, "aa", new KeywordAnalyzer()); Query query = p.parse("path:www." + "s*i\\?na.com"); System.out.println(query.toString() + "=" + query.getClass()); TopDocs top = searcher.search(query, 1000); ScoreDoc[] sdoc = top.scoreDocs; System.out.println(sdoc.length); printResult(sdoc, "path", searcher); }
@Test public void testRangeQueryBelow() throws Exception { Transaction transaction = fullTextSession.beginTransaction(); final QueryBuilder monthQb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Month.class).get(); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); calendar.set(10 + 1800, 2, 12, 0, 0, 0); Date to = calendar.getTime(); Query query = monthQb .range() .onField("estimatedCreation") .andField("justfortest") .ignoreFieldBridge() .ignoreAnalyzer() .below(to) .createQuery(); FullTextQuery hibQuery = fullTextSession.createFullTextQuery(query, Month.class); assertEquals(1, hibQuery.getResultSize()); assertEquals("March", ((Month) hibQuery.list().get(0)).getName()); query = monthQb .range() .onField("estimatedCreation") .ignoreFieldBridge() .andField("justfortest") .ignoreFieldBridge() .ignoreAnalyzer() .below(DateTools.round(to, DateTools.Resolution.MINUTE)) .createQuery(); hibQuery = fullTextSession.createFullTextQuery(query, Month.class); assertEquals(1, hibQuery.getResultSize()); assertEquals("March", ((Month) hibQuery.list().get(0)).getName()); query = monthQb.range().onField("raindropInMm").below(0.24d).createQuery(); assertTrue(query.getClass().isAssignableFrom(NumericRangeQuery.class)); List<?> results = fullTextSession.createFullTextQuery(query, Month.class).list(); assertEquals("test range numeric ", 1, results.size()); assertEquals("test range numeric ", "January", ((Month) results.get(0)).getName()); transaction.commit(); }
@Test public void testNumericFieldsTermQuery() { Transaction transaction = fullTextSession.beginTransaction(); final QueryBuilder monthQb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Month.class).get(); Query query = monthQb.keyword().onField("raindropInMm").matching(0.231d).createQuery(); assertTrue(query.getClass().isAssignableFrom(NumericRangeQuery.class)); assertEquals( "test term numeric ", 1, fullTextSession.createFullTextQuery(query, Month.class).getResultSize()); transaction.commit(); }
@Test public void testNumericRangeQueries() { Transaction transaction = fullTextSession.beginTransaction(); final QueryBuilder monthQb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Month.class).get(); Query query = monthQb.range().onField("raindropInMm").from(0.23d).to(0.24d).createQuery(); assertTrue(query.getClass().isAssignableFrom(NumericRangeQuery.class)); List<?> results = fullTextSession.createFullTextQuery(query, Month.class).list(); assertEquals("test range numeric ", 1, results.size()); assertEquals("test range numeric ", "January", ((Month) results.get(0)).getName()); transaction.commit(); }
@Test public void testPhraseQuery() { Map<String, ColumnMapper> map = new HashMap<>(); map.put("name", new ColumnMapperBoolean()); Schema mappers = new Schema(map, null, EnglishAnalyzer.class.getName()); List<String> values = new ArrayList<>(); values.add("hola"); values.add("adios"); PhraseCondition phraseCondition = new PhraseCondition(0.5f, "name", values, 2); Query query = phraseCondition.query(mappers); Assert.assertNotNull(query); Assert.assertEquals(org.apache.lucene.search.PhraseQuery.class, query.getClass()); org.apache.lucene.search.PhraseQuery luceneQuery = (org.apache.lucene.search.PhraseQuery) query; Assert.assertEquals(values.size(), luceneQuery.getTerms().length); Assert.assertEquals(2, luceneQuery.getSlop()); Assert.assertEquals(0.5f, query.getBoost(), 0); }
/** * Construct the query (using spans). If there are 'boolean' clauses contained in the Query, the * recursive buildQuery_recursive method will be leveraged. * * @param scope Author, affiliation, or Reference * @param q Query * @return SpanQuery */ private SpanQuery buildQuery(String scope, Query q) { SpanQuery spanQuery = null; ArrayList<SpanQuery> spans = null; if (q.getClass() == BooleanQuery.class) { System.out.println("BooleanQuery clause found."); spanQuery = buildQuery_recursive( q, scope, 0, new ArrayList<SpanQuery>(), new ArrayList<SpanQuery>(), new ArrayList<SpanQuery>()); } else if (q.getClass() == TermQuery.class) { spanQuery = buildTermQuery(scope, q); } else if (q.getClass() == WildcardQuery.class) { spanQuery = buildWildcardQuery(scope, q); } else if (q.getClass() == PrefixQuery.class) { spanQuery = buildPrefixQuery(scope, q); } else if (q.getClass() == PhraseQuery.class) { spanQuery = buildPhraseQuery(scope, q); } else { System.out.println("[" + q.getClass() + "]"); } if (spanQuery != null) { return spanQuery; } else { System.out.println("Failed to create a span query."); return null; } }
public static void EmptyTest() throws Exception { Directory dir = FSDirectory.open(new File(tempDir)); IndexSearcher searcher = new IndexSearcher(dir); // 来对索引进行检索 QueryParser p = new QueryParser(Version.LUCENE_36, "aa", new KeywordAnalyzer()); Query query = p.parse("title:新浪 +path:\"\""); /* TermQuery path = new TermQuery(new Term("path", "")); TermQuery title = new TermQuery(new Term("title", "新浪")); BooleanQuery query = new BooleanQuery(); query.add( title, BooleanClause.Occur.MUST ); query.add(path, BooleanClause.Occur.MUST_NOT);*/ System.out.println(query.toString() + "=" + query.getClass()); TopDocs top = searcher.search(query, 1000); ScoreDoc[] sdoc = top.scoreDocs; System.out.println(sdoc.length); printResult(sdoc, "path", searcher); }
private static void toPlan(final StringBuilder builder, final Query query) { builder.append(query.getClass().getSimpleName()); builder.append("("); if (query instanceof TermQuery) { planTermQuery(builder, (TermQuery) query); } else if (query instanceof BooleanQuery) { planBooleanQuery(builder, (BooleanQuery) query); } else if (query instanceof TermRangeQuery) { planTermRangeQuery(builder, (TermRangeQuery) query); } else if (query instanceof PrefixQuery) { planPrefixQuery(builder, (PrefixQuery) query); } else if (query instanceof WildcardQuery) { planWildcardQuery(builder, (WildcardQuery) query); } else if (query instanceof FuzzyQuery) { planFuzzyQuery(builder, (FuzzyQuery) query); } else if (query instanceof NumericRangeQuery<?>) { planNumericRangeQuery(builder, (NumericRangeQuery<?>) query); } else { builder.append(query); } builder.append(",boost=" + query.getBoost() + ")"); }
/** * Recursive helper to finalize a node in the dependency tree * * @param token The node we are currently finalizing * @return A hierarchical representation of the tree inclusive of children at this level */ private ProfileResult doGetQueryTree(int token) { Query query = queries.get(token); ProfileBreakdown breakdown = timings.get(token); Map<String, Long> timings = breakdown.toTimingMap(); List<Integer> children = tree.get(token); List<ProfileResult> childrenProfileResults = Collections.emptyList(); if (children != null) { childrenProfileResults = new ArrayList<>(children.size()); for (Integer child : children) { ProfileResult childNode = doGetQueryTree(child); childrenProfileResults.add(childNode); } } // TODO this would be better done bottom-up instead of top-down to avoid // calculating the same times over and over...but worth the effort? long nodeTime = getNodeTime(timings, childrenProfileResults); String queryDescription = query.getClass().getSimpleName(); String luceneName = query.toString(); return new ProfileResult( queryDescription, luceneName, timings, childrenProfileResults, nodeTime); }
@Test @TestForIssue(jiraKey = "HSEARCH-1378") public void testNumericRangeQueryBelow() { Transaction transaction = fullTextSession.beginTransaction(); final QueryBuilder monthQb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Month.class).get(); // inclusive Query query = monthQb.range().onField("raindropInMm").below(0.435d).createQuery(); assertTrue(query.getClass().isAssignableFrom(NumericRangeQuery.class)); List<?> results = fullTextSession.createFullTextQuery(query, Month.class).list(); assertThat(results).onProperty("name").containsOnly("January", "February", "March"); // exclusive query = monthQb.range().onField("raindropInMm").below(0.435d).excludeLimit().createQuery(); results = fullTextSession.createFullTextQuery(query, Month.class).list(); assertThat(results).onProperty("name").containsOnly("January"); transaction.commit(); }
@Test public void testQueryNumeric() { Float boost = 0.7f; Object[] values = new Object[] {1, 2, 3}; Schema schema = schema().mapper("name", integerMapper()).build(); ContainsCondition condition = new ContainsCondition(boost, "name", values); Query query = condition.query(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", BooleanQuery.class, query.getClass()); BooleanQuery booleanQuery = (BooleanQuery) query; assertEquals("Boost is not set", 0.7f, query.getBoost(), 0); BooleanClause[] clauses = booleanQuery.getClauses(); assertEquals("Query is wrong", values.length, clauses.length); for (int i = 0; i < values.length; i++) { NumericRangeQuery<?> numericRangeQuery = (NumericRangeQuery<?>) clauses[i].getQuery(); assertEquals("Query is wrong", values[i], numericRangeQuery.getMin()); assertEquals("Query is wrong", values[i], numericRangeQuery.getMax()); } }
@Override protected String getTypeFromElement(Query query) { return query.getClass().getSimpleName(); }
/** * Construct the query (using spans). This method will be called recursively. * * @param q Query * @param scope Author, affiliation, or Reference * @param level Used only for formatting (indentation) the level of recursion * @param andSpans ArrayList of Spans that should be 'and' * @param orSpans ArrayList of Spans that should be 'or' * @param notSpans ArrayList of Spans that should be 'not' * @return SpanQuery */ private SpanQuery buildQuery_recursive( Query q, String scope, int level, ArrayList<SpanQuery> andSpans, ArrayList<SpanQuery> orSpans, ArrayList<SpanQuery> notSpans) { BooleanQuery castQuery = (BooleanQuery) q; String subscope = null; for (BooleanClause clause : castQuery.getClauses()) { Class queryclazz = clause.getQuery().getClass(); System.out.println( repeat(' ', level) + "[" + queryclazz + "][" + clause.getOccur() + "] " + clause.toString()); if (queryclazz == BooleanQuery.class) { System.out.println("Number of Clauses is " + castQuery.clauses().size()); System.out.println("Minimum Number to Match is " + castQuery.getMinimumNumberShouldMatch()); if (subscope == null) { if (clause.getOccur() == BooleanClause.Occur.MUST) { andSpans.add( buildQuery_recursive( clause.getQuery(), scope, level + 1, new ArrayList<SpanQuery>(), new ArrayList<SpanQuery>(), new ArrayList<SpanQuery>())); } else if (clause.getOccur() == BooleanClause.Occur.SHOULD) { orSpans.add( buildQuery_recursive( clause.getQuery(), scope, level + 1, new ArrayList<SpanQuery>(), new ArrayList<SpanQuery>(), new ArrayList<SpanQuery>())); } else if (clause.getOccur() == BooleanClause.Occur.MUST_NOT) { // FIX notSpans.add( buildQuery_recursive( clause.getQuery(), scope, level + 1, new ArrayList<SpanQuery>(), new ArrayList<SpanQuery>(), new ArrayList<SpanQuery>())); } } else { ArrayList<SpanQuery> subscopeQuery = new ArrayList<SpanQuery>(); subscopeQuery.add( new SpanTermQuery(new Term(scope, getTag(scope, subscope, TAG_TYPE.BEG_TAG)))); subscopeQuery.add( new SpanTermQuery(new Term(scope, getTag(scope, subscope, TAG_TYPE.END_TAG)))); subscopeQuery.add( buildQuery_recursive( clause.getQuery(), scope, level + 1, new ArrayList<SpanQuery>(), new ArrayList<SpanQuery>(), new ArrayList<SpanQuery>())); if (clause.getOccur() == BooleanClause.Occur.MUST) { andSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } else if (clause.getOccur() == BooleanClause.Occur.SHOULD) { orSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } else if (clause.getOccur() == BooleanClause.Occur.MUST_NOT) { // FIX notSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } } } else if (queryclazz == TermQuery.class) { TermQuery tq = (TermQuery) clause.getQuery(); if (tq.getTerm().field().compareTo(SUBSCOPE_FIELD) == 0) { // Set the subscope subscope = tq.getTerm().text(); // Need to add a term here (otherwise we have problems) WildcardQuery wildcard = new WildcardQuery(new Term(scope, "*")); if (clause.getOccur() == BooleanClause.Occur.MUST) { andSpans.add(new SpanMultiTermQueryWrapper<WildcardQuery>(wildcard)); } else if (clause.getOccur() == BooleanClause.Occur.SHOULD) { orSpans.add(new SpanMultiTermQueryWrapper<WildcardQuery>(wildcard)); } else if (clause.getOccur() == BooleanClause.Occur.MUST_NOT) { notSpans.add(new SpanMultiTermQueryWrapper<WildcardQuery>(wildcard)); } } else if (subscope == null) { if (clause.getOccur() == BooleanClause.Occur.MUST) { andSpans.add(buildTermQuery(scope, tq)); } else if (clause.getOccur() == BooleanClause.Occur.SHOULD) { orSpans.add(buildTermQuery(scope, tq)); } else if (clause.getOccur() == BooleanClause.Occur.MUST_NOT) { notSpans.add(buildTermQuery(scope, tq)); } } else { ArrayList<SpanQuery> subscopeQuery = new ArrayList<SpanQuery>(); subscopeQuery.add( new SpanTermQuery(new Term(scope, getTag(scope, subscope, TAG_TYPE.BEG_TAG)))); subscopeQuery.add( new SpanTermQuery(new Term(scope, getTag(scope, subscope, TAG_TYPE.END_TAG)))); subscopeQuery.add(buildTermQuery(scope, tq)); if (clause.getOccur() == BooleanClause.Occur.MUST) { andSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } else if (clause.getOccur() == BooleanClause.Occur.SHOULD) { orSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } else if (clause.getOccur() == BooleanClause.Occur.MUST_NOT) { // FIX notSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } } } else if (queryclazz == WildcardQuery.class) { if (subscope == null) { if (clause.getOccur() == BooleanClause.Occur.MUST) { andSpans.add(buildWildcardQuery(scope, clause.getQuery())); } else if (clause.getOccur() == BooleanClause.Occur.SHOULD) { orSpans.add(buildWildcardQuery(scope, clause.getQuery())); } else if (clause.getOccur() == BooleanClause.Occur.MUST_NOT) { // FIX notSpans.add(buildWildcardQuery(scope, clause.getQuery())); } } else { ArrayList<SpanQuery> subscopeQuery = new ArrayList<SpanQuery>(); subscopeQuery.add( new SpanTermQuery(new Term(scope, getTag(scope, subscope, TAG_TYPE.BEG_TAG)))); subscopeQuery.add( new SpanTermQuery(new Term(scope, getTag(scope, subscope, TAG_TYPE.END_TAG)))); subscopeQuery.add(buildWildcardQuery(scope, clause.getQuery())); if (clause.getOccur() == BooleanClause.Occur.MUST) { andSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } else if (clause.getOccur() == BooleanClause.Occur.SHOULD) { orSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } else if (clause.getOccur() == BooleanClause.Occur.MUST_NOT) { // FIX notSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } } } else if (queryclazz == PrefixQuery.class) { if (subscope == null) { if (clause.getOccur() == BooleanClause.Occur.MUST) { andSpans.add(buildPrefixQuery(scope, clause.getQuery())); } else if (clause.getOccur() == BooleanClause.Occur.SHOULD) { orSpans.add(buildPrefixQuery(scope, clause.getQuery())); } else if (clause.getOccur() == BooleanClause.Occur.MUST_NOT) { // FIX notSpans.add(buildPrefixQuery(scope, clause.getQuery())); } } else { ArrayList<SpanQuery> subscopeQuery = new ArrayList<SpanQuery>(); subscopeQuery.add( new SpanTermQuery(new Term(scope, getTag(scope, subscope, TAG_TYPE.BEG_TAG)))); subscopeQuery.add( new SpanTermQuery(new Term(scope, getTag(scope, subscope, TAG_TYPE.END_TAG)))); subscopeQuery.add(buildPrefixQuery(scope, clause.getQuery())); if (clause.getOccur() == BooleanClause.Occur.MUST) { andSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } else if (clause.getOccur() == BooleanClause.Occur.SHOULD) { orSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } else if (clause.getOccur() == BooleanClause.Occur.MUST_NOT) { // FIX notSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } } } else if (queryclazz == PhraseQuery.class) { if (subscope == null) { if (clause.getOccur() == BooleanClause.Occur.MUST) { andSpans.add(buildPhraseQuery(scope, clause.getQuery())); } else if (clause.getOccur() == BooleanClause.Occur.SHOULD) { orSpans.add(buildPhraseQuery(scope, clause.getQuery())); } else if (clause.getOccur() == BooleanClause.Occur.MUST_NOT) { // FIX notSpans.add(buildPhraseQuery(scope, clause.getQuery())); } } else { ArrayList<SpanQuery> subscopeQuery = new ArrayList<SpanQuery>(); subscopeQuery.add( new SpanTermQuery(new Term(scope, getTag(scope, subscope, TAG_TYPE.BEG_TAG)))); subscopeQuery.add( new SpanTermQuery(new Term(scope, getTag(scope, subscope, TAG_TYPE.END_TAG)))); subscopeQuery.add(buildPhraseQuery(scope, clause.getQuery())); if (clause.getOccur() == BooleanClause.Occur.MUST) { andSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } else if (clause.getOccur() == BooleanClause.Occur.SHOULD) { orSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } else if (clause.getOccur() == BooleanClause.Occur.MUST_NOT) { // FIX notSpans.add( new SpanBetweenQuery(subscopeQuery.toArray(new SpanQuery[subscopeQuery.size()]))); } } } else { System.out.println("[" + q.getClass() + "]"); } } ArrayList<SpanQuery> includeSpans = new ArrayList<SpanQuery>(); ; // Add the 'and' queries to the includeSpans (if there were any) if (!andSpans.isEmpty()) { if (andSpans.size() > 1) { includeSpans.add(new SpanAndQuery(andSpans.toArray(new SpanQuery[andSpans.size()]))); } else { includeSpans.add(andSpans.get(0)); } } // Add the 'or' queries to the includeSpans (if there were any) if (!orSpans.isEmpty()) { includeSpans.add(new SpanOrQuery(orSpans.toArray(new SpanQuery[orSpans.size()]))); } // Exclude the 'not' queries from the includeSpans (if there were any) if (!notSpans.isEmpty()) { if (includeSpans.size() > 1) { if (notSpans.size() > 1) { return new SpanNotQuery( new SpanAndQuery(includeSpans.toArray(new SpanQuery[includeSpans.size()])), new SpanAndQuery(notSpans.toArray(new SpanQuery[notSpans.size()]))); } else { return new SpanNotQuery( new SpanAndQuery(includeSpans.toArray(new SpanQuery[includeSpans.size()])), notSpans.get(0)); } } else { if (notSpans.size() > 1) { return new SpanNotQuery( includeSpans.get(0), new SpanAndQuery(notSpans.toArray(new SpanQuery[notSpans.size()]))); } else { return new SpanNotQuery(includeSpans.get(0), notSpans.get(0)); } } } else { if (includeSpans.size() > 1) { return new SpanAndQuery(includeSpans.toArray(new SpanQuery[includeSpans.size()])); } else { return includeSpans.get(0); } } }
/** @see #toString(Query,IndexSchema) */ public static void toString(Query query, IndexSchema schema, Appendable out, int flags) throws IOException { boolean writeBoost = true; if (query instanceof TermQuery) { TermQuery q = (TermQuery) query; Term t = q.getTerm(); FieldType ft = writeFieldName(t.field(), schema, out, flags); writeFieldVal(t.bytes(), ft, out, flags); } else if (query instanceof TermRangeQuery) { TermRangeQuery q = (TermRangeQuery) query; String fname = q.getField(); FieldType ft = writeFieldName(fname, schema, out, flags); out.append(q.includesLower() ? '[' : '{'); BytesRef lt = q.getLowerTerm(); BytesRef ut = q.getUpperTerm(); if (lt == null) { out.append('*'); } else { writeFieldVal(lt, ft, out, flags); } out.append(" TO "); if (ut == null) { out.append('*'); } else { writeFieldVal(ut, ft, out, flags); } out.append(q.includesUpper() ? ']' : '}'); } else if (query instanceof NumericRangeQuery) { NumericRangeQuery q = (NumericRangeQuery) query; String fname = q.getField(); FieldType ft = writeFieldName(fname, schema, out, flags); out.append(q.includesMin() ? '[' : '{'); Number lt = q.getMin(); Number ut = q.getMax(); if (lt == null) { out.append('*'); } else { out.append(lt.toString()); } out.append(" TO "); if (ut == null) { out.append('*'); } else { out.append(ut.toString()); } out.append(q.includesMax() ? ']' : '}'); } else if (query instanceof BooleanQuery) { BooleanQuery q = (BooleanQuery) query; boolean needParens = false; if (q.getBoost() != 1.0 || q.getMinimumNumberShouldMatch() != 0 || q.isCoordDisabled()) { needParens = true; } if (needParens) { out.append('('); } boolean first = true; for (BooleanClause c : q.clauses()) { if (!first) { out.append(' '); } else { first = false; } if (c.isProhibited()) { out.append('-'); } else if (c.isRequired()) { out.append('+'); } Query subQuery = c.getQuery(); boolean wrapQuery = false; // TODO: may need to put parens around other types // of queries too, depending on future syntax. if (subQuery instanceof BooleanQuery) { wrapQuery = true; } if (wrapQuery) { out.append('('); } toString(subQuery, schema, out, flags); if (wrapQuery) { out.append(')'); } } if (needParens) { out.append(')'); } if (q.getMinimumNumberShouldMatch() > 0) { out.append('~'); out.append(Integer.toString(q.getMinimumNumberShouldMatch())); } if (q.isCoordDisabled()) { out.append("/no_coord"); } } else if (query instanceof PrefixQuery) { PrefixQuery q = (PrefixQuery) query; Term prefix = q.getPrefix(); FieldType ft = writeFieldName(prefix.field(), schema, out, flags); out.append(prefix.text()); out.append('*'); } else if (query instanceof WildcardQuery) { out.append(query.toString()); writeBoost = false; } else if (query instanceof FuzzyQuery) { out.append(query.toString()); writeBoost = false; } else if (query instanceof ConstantScoreQuery) { out.append(query.toString()); writeBoost = false; } else if (query instanceof WrappedQuery) { WrappedQuery q = (WrappedQuery) query; out.append(q.getOptions()); toString(q.getWrappedQuery(), schema, out, flags); writeBoost = false; // we don't use the boost on wrapped queries } else { out.append(query.getClass().getSimpleName() + '(' + query.toString() + ')'); writeBoost = false; } if (writeBoost && query.getBoost() != 1.0f) { out.append("^"); out.append(Float.toString(query.getBoost())); } }