ContainsCondition extends SingleColumnCondition { @Override public Query doQuery(SingleColumnMapper<?> mapper, Analyzer analyzer) { BooleanQuery.Builder builder = new BooleanQuery.Builder(); for (Object value : values) { MatchCondition condition = new MatchCondition(null, field, value, docValues); builder.add(condition.doQuery(mapper, analyzer), BooleanClause.Occur.SHOULD); } return builder.build(); } ContainsCondition(Float boost, String field, Boolean docValues, Object... values); @Override Query doQuery(SingleColumnMapper<?> mapper, Analyzer analyzer); @Override MoreObjects.ToStringHelper toStringHelper(); static final boolean DEFAULT_DOC_VALUES; final String field; final Object[] values; final boolean docValues; }
@Test public void testQueryNumeric() { Object[] values = new Object[]{0, 1, 2}; Schema schema = schema().mapper("name", integerMapper()).build(); ContainsCondition condition = contains("name", values).build(); Query query = condition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", BooleanQuery.class, query.getClass()); BooleanQuery booleanQuery = (BooleanQuery) query; List<BooleanClause> clauses = booleanQuery.clauses(); assertEquals("Query is wrong", values.length, clauses.size()); TermQuery query0 = (TermQuery) clauses.get(0).getQuery(); TermQuery query1 = (TermQuery) clauses.get(1).getQuery(); TermQuery query2 = (TermQuery) clauses.get(2).getQuery(); assertEquals("Query value is wrong", "600800000000", ByteBufferUtils.toHex(query0.getTerm().bytes())); assertEquals("Query value is wrong", "600800000001", ByteBufferUtils.toHex(query1.getTerm().bytes())); assertEquals("Query value is wrong", "600800000002", ByteBufferUtils.toHex(query2.getTerm().bytes())); }
@Test public void testQueryString() { Object[] values = new Object[]{"houses", "cats"}; Schema schema = schema().mapper("name", stringMapper()).build(); ContainsCondition condition = contains("name", values).build(); Query query = condition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", BooleanQuery.class, query.getClass()); BooleanQuery booleanQuery = (BooleanQuery) query; List<BooleanClause> clauses = booleanQuery.clauses(); TermQuery query1 = (TermQuery) clauses.get(0).getQuery(); TermQuery query2 = (TermQuery) clauses.get(1).getQuery(); assertEquals("Query is wrong", "houses", query1.getTerm().bytes().utf8ToString()); assertEquals("Query is wrong", "cats", query2.getTerm().bytes().utf8ToString()); }
@Test public void testQueryText() { Object[] values = new Object[]{"houses", "cats"}; Schema schema = schema().mapper("name", textMapper()).defaultAnalyzer("english").build(); ContainsCondition condition = contains("name", values).build(); Query query = condition.doQuery(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", BooleanQuery.class, query.getClass()); BooleanQuery booleanQuery = (BooleanQuery) query; List<BooleanClause> clauses = booleanQuery.clauses(); TermQuery termQuery1 = (TermQuery) clauses.get(0).getQuery(); TermQuery termQuery2 = (TermQuery) clauses.get(1).getQuery(); assertEquals("Query is wrong", "hous", termQuery1.getTerm().bytes().utf8ToString()); assertEquals("Query is wrong", "cat", termQuery2.getTerm().bytes().utf8ToString()); }