public void testFromJson() throws IOException { String json = "{\n" + " \"multi_match\" : {\n" + " \"query\" : \"quick brown fox\",\n" + " \"fields\" : [ \"title^1.0\", \"title.original^1.0\", \"title.shingles^1.0\" ],\n" + " \"type\" : \"most_fields\",\n" + " \"operator\" : \"OR\",\n" + " \"slop\" : 0,\n" + " \"prefix_length\" : 0,\n" + " \"max_expansions\" : 50,\n" + " \"lenient\" : false,\n" + " \"zero_terms_query\" : \"NONE\",\n" + " \"boost\" : 1.0\n" + " }\n" + "}"; MultiMatchQueryBuilder parsed = (MultiMatchQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, "quick brown fox", parsed.value()); assertEquals(json, 3, parsed.fields().size()); assertEquals(json, MultiMatchQueryBuilder.Type.MOST_FIELDS, parsed.type()); assertEquals(json, Operator.OR, parsed.operator()); }
/** * `fuzziness` is not allowed for `cross_fields`, `phrase` and `phrase_prefix` and should throw an error */ public void testFuzzinessNotAllowedTypes() throws IOException { String[] notAllowedTypes = new String[]{ Type.CROSS_FIELDS.parseField().getPreferredName(), Type.PHRASE.parseField().getPreferredName(), Type.PHRASE_PREFIX.parseField().getPreferredName()}; for (String type : notAllowedTypes) { String json = "{\n" + " \"multi_match\" : {\n" + " \"query\" : \"quick brown fox\",\n" + " \"fields\" : [ \"title^1.0\", \"title.original^1.0\", \"title.shingles^1.0\" ],\n" + " \"type\" : \"" + type + "\",\n" + " \"fuzziness\" : 1" + " }\n" + "}"; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals("Fuzziness not allowed for type [" + type +"]", e.getMessage()); } }
public static String buildTemplate() throws IOException { XContentBuilder builder = jsonBuilder() .startObject() .field("query", boolQuery() .should(multiMatchQuery("categories", "title", "short_description").boost(3)) .should(multiMatchQuery("council", "title", "short_description").boost(0.3f)) .should(boostingQuery() .positive(queryString("my +ve query") .boost(0.6f)) .negative(queryString("my -ve query")) .negativeBoost(-2)) .must(matchQuery("stages", "STAGE")) .must(multiMatchQuery("region", "locations", "areas").type(Type.BEST_FIELDS).operator(Operator.AND)) .mustNot(matchPhraseQuery("title", "Access to Work")) ) .endObject().startArray("fields").field("short_description").field("title").endArray(); builder.string(); return builder.toString(); }
@Override protected MultiMatchQueryBuilder doCreateTestQueryBuilder() { String fieldName = randomFrom(STRING_FIELD_NAME, INT_FIELD_NAME, DOUBLE_FIELD_NAME, BOOLEAN_FIELD_NAME, DATE_FIELD_NAME, MISSING_FIELD_NAME, MISSING_WILDCARD_FIELD_NAME); if (fieldName.equals(DATE_FIELD_NAME)) { assumeTrue("test with date fields runs only when at least a type is registered", getCurrentTypes().length > 0); } // creates the query with random value and field name Object value; if (fieldName.equals(STRING_FIELD_NAME)) { value = getRandomQueryText(); } else { value = getRandomValueForFieldName(fieldName); } MultiMatchQueryBuilder query = new MultiMatchQueryBuilder(value, fieldName); // field with random boost if (randomBoolean()) { query.field(fieldName, randomFloat() * 10); } // sets other parameters of the multi match query if (randomBoolean()) { query.type(randomFrom(MultiMatchQueryBuilder.Type.values())); } if (randomBoolean()) { query.operator(randomFrom(Operator.values())); } if (randomBoolean()) { if (fieldName.equals(DATE_FIELD_NAME)) { // tokenized dates would trigger parse errors query.analyzer("keyword"); } else { query.analyzer(randomAnalyzer()); } } if (randomBoolean()) { query.slop(randomIntBetween(0, 5)); } if (fieldName.equals(STRING_FIELD_NAME) && randomBoolean() && (query.type() == Type.BEST_FIELDS || query.type() == Type.MOST_FIELDS)) { query.fuzziness(randomFuzziness(fieldName)); } if (randomBoolean()) { query.prefixLength(randomIntBetween(0, 5)); } if (randomBoolean()) { query.maxExpansions(randomIntBetween(1, 5)); } if (randomBoolean()) { query.minimumShouldMatch(randomMinimumShouldMatch()); } if (randomBoolean()) { query.fuzzyRewrite(getRandomRewriteMethod()); } if (randomBoolean()) { query.useDisMax(randomBoolean()); } if (randomBoolean()) { query.tieBreaker(randomFloat()); } if (randomBoolean()) { query.lenient(randomBoolean()); } if (randomBoolean()) { query.cutoffFrequency((float) 10 / randomIntBetween(1, 100)); } if (randomBoolean()) { query.zeroTermsQuery(randomFrom(MatchQuery.ZeroTermsQuery.values())); } // test with fields with boost and patterns delegated to the tests further below return query; }
private QueryBuilder buildAllThings(String query, String motivations, String allDateRanges, String users, String type, String within) { List<QueryBuilder> queryList = new ArrayList<>(); BoolQueryBuilder must = QueryBuilders.boolQuery(); if (null != query) { // String tidyQuery = // annotationUtils.convertSpecialCharacters(query); String tidyQuery = query; if (null == type) { must = must.must(QueryBuilders.multiMatchQuery(tidyQuery, "body", "target", "bodyURI", "targetURI") .type(Type.PHRASE)); } if ("topic".equals(type)) { must = must.must(QueryBuilders.multiMatchQuery(tidyQuery, "bodyURI")); } } if (null != motivations) { List<String> motivationsList = annotationUtils.getListFromSpaceSeparatedTerms(motivations); if (motivations.contains("non-")) { if (motivationsList.size() > 1) { throw new SearchQueryException( "You have a motivation that is a non-<motivation>, there can only be one motivation in this instance."); } else { String tidyMotivations = motivations.replaceAll("non-", ""); queryList.add(QueryBuilders.existsQuery(AnnotationSearchConstants.FIELD_MOTIVATIONS)); must.mustNot(QueryBuilders.termQuery(AnnotationSearchConstants.FIELD_MOTIVATIONS, tidyMotivations)); } } else { must.filter(QueryBuilders.termsQuery(AnnotationSearchConstants.FIELD_MOTIVATIONS, motivationsList)); } } if (null != allDateRanges) { queryList.add(buildDates("created", allDateRanges)); } if (null != users) { List<String> usersList = annotationUtils.getListFromSpaceSeparatedTerms(users); must.filter(QueryBuilders.termsQuery("creators", usersList)); } for (QueryBuilder eachQuery : queryList) { must = must.must(eachQuery); } if (null != within) { String decodedWithinUrl = annotationUtils.decodeWithinUrl(within); if (null != decodedWithinUrl) { must.must(QueryBuilders.matchQuery("manifest", decodedWithinUrl)); } } return must; }