public void testDefaults() throws ExecutionException, InterruptedException { MatchQuery.Type type = randomBoolean() ? MatchQueryBuilder.DEFAULT_TYPE : MatchQuery.Type.BOOLEAN; SearchResponse searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("marvel hero captain america", "full_name", "first_name", "last_name", "category") .operator(Operator.OR))).get(); Set<String> topNIds = Sets.newHashSet("theone", "theother"); for (int i = 0; i < searchResponse.getHits().getHits().length; i++) { topNIds.remove(searchResponse.getHits().getAt(i).getId()); // very likely that we hit a random doc that has the same score so orders are random since // the doc id is the tie-breaker } assertThat(topNIds, empty()); assertThat(searchResponse.getHits().getHits()[0].getScore(), greaterThan(searchResponse.getHits().getHits()[1].getScore())); searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("marvel hero captain america", "full_name", "first_name", "last_name", "category") .operator(Operator.OR).useDisMax(false).type(type))).get(); assertFirstHit(searchResponse, anyOf(hasId("theone"), hasId("theother"))); assertThat(searchResponse.getHits().getHits()[0].getScore(), greaterThan(searchResponse.getHits().getHits()[1].getScore())); searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("marvel hero", "full_name", "first_name", "last_name", "category") .operator(Operator.OR).type(type))).get(); assertFirstHit(searchResponse, hasId("theother")); searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("captain america", "full_name", "first_name", "last_name", "category") .operator(Operator.AND).type(type))).get(); assertHitCount(searchResponse, 1L); assertFirstHit(searchResponse, hasId("theone")); searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("captain america", "full_name", "first_name", "last_name", "category") .operator(Operator.AND).type(type))).get(); assertHitCount(searchResponse, 1L); assertFirstHit(searchResponse, hasId("theone")); }
public void testFailedSearchWithWrongQuery() throws Exception { prepareData(); NumShards test = getNumShards("test"); logger.info("Start Testing failed search with wrong query"); try { SearchResponse searchResponse = client().search( searchRequest("test").source(new SearchSourceBuilder().query(new MatchQueryBuilder("foo", "biz")))).actionGet(); assertThat(searchResponse.getTotalShards(), equalTo(test.numPrimaries)); assertThat(searchResponse.getSuccessfulShards(), equalTo(0)); assertThat(searchResponse.getFailedShards(), equalTo(test.numPrimaries)); fail("search should fail"); } catch (ElasticsearchException e) { assertThat(e.unwrapCause(), instanceOf(SearchPhaseExecutionException.class)); // all is well } logger.info("Done Testing failed search"); }
public void testFailedMultiSearchWithWrongQuery() throws Exception { prepareData(); logger.info("Start Testing failed multi search with a wrong query"); MultiSearchResponse response = client().prepareMultiSearch() .add(client().prepareSearch("test").setQuery(new MatchQueryBuilder("foo", "biz"))) .add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("nid", 2))) .add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery())) .execute().actionGet(); assertThat(response.getResponses().length, equalTo(3)); assertThat(response.getResponses()[0].getFailureMessage(), notNullValue()); assertThat(response.getResponses()[1].getFailureMessage(), nullValue()); assertThat(response.getResponses()[1].getResponse().getHits().getHits().length, equalTo(1)); assertThat(response.getResponses()[2].getFailureMessage(), nullValue()); assertThat(response.getResponses()[2].getResponse().getHits().getHits().length, equalTo(10)); logger.info("Done Testing failed search"); }
public List<Person> searchByUserName(String userName) throws Exception { List<Person> result = new ArrayList<>(); MatchQueryBuilder query = QueryBuilders.matchQuery("userName", userName); SearchRequestBuilder searchRequestBuilder = client.prepareSearch(ES_INDEX).setTypes(ES_DOCTYPE).setQuery(query); SearchResponse response = searchRequestBuilder.execute().actionGet(); if (response != null) { SearchHits hits = response.getHits(); if (hits != null && hits.getTotalHits() > 0) { for (SearchHit hit : hits) { String personJson = hit.getSourceAsString(); ObjectMapper objectMapper = new ObjectMapper(); try { Person person = objectMapper.readValue(personJson, Person.class); result.add(person); } catch (Exception e) { LOGGER.error("Error while deserializing Elasticsearch Person document"); throw e; } } } } return result; }
private QueryBuilder buildQueryBuilder(ElasticsearchSearchOption option) { ElasticsearchSearchOption.Operator op = option.getOperator(); MultiMatchQueryBuilder query = multiMatchQuery( option.getKeyword(), option.getSearchFields().stream().toArray(String[]::new)); if (op != null && ElasticsearchSearchOption.Operator.AND == op) { query = query.operator(MatchQueryBuilder.Operator.AND); } if (option.getOptionalSearchConditions().isEmpty()) { return query; } else { BoolQueryBuilder q = boolQuery().must(query); option.getOptionalSearchConditions().forEach((k, v) -> { q.must(matchQuery(k, v)); }); return q; } }
public void testParseFeatureAsString() throws IOException { String featureString = "{\n" + "\"name\": \"testFeature\",\n" + "\"params\": [\"param1\", \"param2\"],\n" + "\"template_language\": \"mustache\",\n" + "\"template\": \"" + new MatchQueryBuilder("match_field", "match_word").toString(NOT_PRETTY) .replace("\"", "\\\"") + "\"\n}\n"; StoredFeature feature = parse(featureString); assertEquals("testFeature", feature.name()); assertArrayEquals(Arrays.asList("param1", "param2").toArray(), feature.queryParams().toArray()); assertEquals("mustache", feature.templateLanguage()); assertEquals(new MatchQueryBuilder("match_field", "match_word").toString(NOT_PRETTY), feature.template()); assertTrue(feature.templateAsString()); }
@Before public void setUp() throws Exception { super.setUp(); store.clear(); StoredFeature feature1 = new StoredFeature("match1", Collections.singletonList("query_string"), "mustache", new MatchQueryBuilder("field1", "{{query_string}}").toString()); StoredFeature feature2 = new StoredFeature("match2", Collections.singletonList("query_string"), "mustache", new MatchQueryBuilder("field2", "{{query_string}}").toString()); StoredFeature feature3 = new StoredFeature("score3", Collections.emptyList(), "mustache", new FunctionScoreQueryBuilder(new FieldValueFactorFunctionBuilder("scorefield2") .factor(1.2F) .modifier(FieldValueFactorFunction.Modifier.LN2P) .missing(0F)).toString()); StoredFeatureSet set = new StoredFeatureSet("set1", Arrays.asList(feature1, feature2, feature3)); store.add(set); LtrRanker ranker = new LinearRanker(new float[]{0.1F, 0.2F, 0.3F}); CompiledLtrModel model = new CompiledLtrModel("model1", set, ranker); store.add(model); }
@Test public void testQueryWithIndex() throws Exception { // Prepare: Setup Thrift Client and add some docs client = pool.getClient(SERVICE_NAME, EzElastic.Client.class); client.bulkPut( Arrays.asList(washingtonMonumentDoc, whiteHouseDoc, oneaaColumbiaDoc, oneaaNavyYardDoc), fakeSecurity); // Action: Create and execute an AND match query on title final String elasticQuery = QueryBuilders.matchQuery("title", "42Six HQ").operator(MatchQueryBuilder.Operator.AND).toString(); final SearchResult results = client.query(new Query(elasticQuery), fakeSecurity); // Assert: There should be a single matching document due to the AND operator assertEquals(1, results.getTotalHits()); assertEquals(oneaaColumbiaDoc.get_id(), results.getMatchingDocuments().get(0).get_id()); pool.returnToPool(client); }
@Override public void visit( ContainsOperand op ) throws NoFullTextIndexException { final String name = op.getProperty().getValue().toLowerCase(); final String value = op.getLiteral().getValue().toString().toLowerCase(); // or field is just a string that does need a prefix if ( value.indexOf( "*" ) != -1 ) { final WildcardQueryBuilder wildcardQuery = QueryBuilders.wildcardQuery( IndexingUtils.FIELD_STRING_NESTED, value ); queryBuilders.push( fieldNameTerm( name, wildcardQuery ) ); } else { final MatchQueryBuilder termQuery = QueryBuilders.matchQuery( IndexingUtils.FIELD_STRING_NESTED, value ); queryBuilders.push( fieldNameTerm( name, termQuery ) ); } //no op for filters, push an empty operation //TODO, validate this works filterBuilders.push( NoOpFilterBuilder.INSTANCE ); }
public void testConcurrentDeleteByQueriesOnSameDocs() throws Throwable { final long docs = randomIntBetween(50, 100); List<IndexRequestBuilder> builders = new ArrayList<>(); for (int i = 0; i < docs; i++) { builders.add(client().prepareIndex("test", "doc", String.valueOf(i)).setSource("foo", "bar")); } indexRandom(true, true, true, builders); final Thread[] threads = new Thread[scaledRandomIntBetween(2, 9)]; final CountDownLatch start = new CountDownLatch(1); final MatchQueryBuilder query = matchQuery("foo", "bar"); final AtomicLong deleted = new AtomicLong(0); for (int t = 0; t < threads.length; t++) { Runnable r = () -> { try { start.await(); BulkByScrollResponse response = deleteByQuery().source("test").filter(query).refresh(true).get(); // Some deletions might fail due to version conflict, but // what matters here is the total of successful deletions deleted.addAndGet(response.getDeleted()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }; threads[t] = new Thread(r); threads[t].start(); } start.countDown(); for (Thread thread : threads) { thread.join(); } assertHitCount(client().prepareSearch("test").setSize(0).get(), 0L); assertThat(deleted.get(), equalTo(docs)); }
public void testHighlightersIgnoreParentChild() { assertAcked(prepareCreate("test") .addMapping("parent-type", "searchText", "type=text,term_vector=with_positions_offsets,index_options=offsets") .addMapping("child-type", "_parent", "type=parent-type", "searchText", "type=text,term_vector=with_positions_offsets,index_options=offsets")); client().prepareIndex("test", "parent-type", "parent-id").setSource("searchText", "quick brown fox").get(); client().prepareIndex("test", "child-type", "child-id").setParent("parent-id").setSource("searchText", "quick brown fox").get(); refresh(); String[] highlightTypes = new String[] {"plain", "fvh", "postings"}; for (String highlightType : highlightTypes) { logger.info("Testing with highlight type [{}]", highlightType); SearchResponse searchResponse = client().prepareSearch("test") .setQuery(new BoolQueryBuilder() .must(new MatchQueryBuilder("searchText", "fox")) .must(new HasChildQueryBuilder("child-type", new MatchAllQueryBuilder(), ScoreMode.None)) ) .highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("searchText").highlighterType(highlightType))) .get(); assertHitCount(searchResponse, 1); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("parent-id")); HighlightField highlightField = searchResponse.getHits().getAt(0).getHighlightFields().get("searchText"); assertThat(highlightField.getFragments()[0].string(), equalTo("quick brown <em>fox</em>")); searchResponse = client().prepareSearch("test") .setQuery(new BoolQueryBuilder() .must(new MatchQueryBuilder("searchText", "fox")) .must(new HasParentQueryBuilder("parent-type", new MatchAllQueryBuilder(), false)) ) .highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("searchText").highlighterType(highlightType))) .get(); assertHitCount(searchResponse, 1); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("child-id")); highlightField = searchResponse.getHits().getAt(0).getHighlightFields().get("searchText"); assertThat(highlightField.getFragments()[0].string(), equalTo("quick brown <em>fox</em>")); } }
/** * Method of creating semantic query based on Threshold * * @param input regular query * @param T threshold raning from 0 to 1 * @param query_operator query mode * @return a multiMatch query builder */ public BoolQueryBuilder createSemQuery(String input, double T, String query_operator) { Map<String, Double> selected_Map = getRelatedTermsByT(input, T); selected_Map.put(input, (double) 1); String fieldsList[] = { "Dataset-Metadata", "Dataset-ShortName", "Dataset-LongName", "DatasetParameter-Topic", "DatasetParameter-VariableDetail", "DatasetParameter-Category", "DatasetParameter-Variable", "DatasetParameter-Term", "DatasetSource-Source-LongName", "DatasetSource-Source-LongName-Full", "DatasetSource-Source-ShortName", "DatasetSource-Source-ShortName-Full", "DatasetSource-Sensor-LongName", "DatasetSource-Sensor-LongName-Full", "DatasetSource-Sensor-ShortName", "DatasetSource-Sensor-ShortName-Full" }; BoolQueryBuilder qb = new BoolQueryBuilder(); for (Entry<String, Double> entry : selected_Map.entrySet()) { if (query_operator.toLowerCase().trim().equals("phrase")) { qb.should(QueryBuilders.multiMatchQuery(entry.getKey(), fieldsList).boost(entry.getValue().floatValue()).type(MultiMatchQueryBuilder.Type.PHRASE).tieBreaker((float) 0.5)); // when // set // to // 1.0, // it // would // be // equal // to // "most // fields" // query } else if (query_operator.toLowerCase().trim().equals("and")) { qb.should(QueryBuilders.multiMatchQuery(entry.getKey(), fieldsList).boost(entry.getValue().floatValue()).operator(MatchQueryBuilder.DEFAULT_OPERATOR.AND).tieBreaker((float) 0.5)); } else { qb.should(QueryBuilders.multiMatchQuery(entry.getKey(), fieldsList).boost(entry.getValue().floatValue()).operator(MatchQueryBuilder.DEFAULT_OPERATOR.OR).tieBreaker((float) 0.5)); } } // LOG.info(qb.toString()); return qb; }
private static MatchQueryBuilder createMatchQuery(String name, Object text, Float boost) { if (isNotNull(boost)) { return QueryBuilders.matchQuery(name, text).boost(boost); } else { return QueryBuilders.matchQuery(name, text); } }
public static ToXContent fullParamer(MatchQueryBuilder query, Paramer paramer) { if (paramer.analysis != null) { query.analyzer(paramer.analysis); } if (paramer.boost != null) { query.boost(paramer.boost); } return query; }
public static String generateTestFeature(String name) { return "{\n" + "\"name\": \""+name+"\",\n" + "\"params\": [\"param1\", \"param2\"],\n" + "\"template_language\": \"mustache\",\n" + "\"template\": \n" + new MatchQueryBuilder("match_field", "match_word").toString() + "\n}\n"; }
public void assertTestFeature(StoredFeature feature) { assertEquals("testFeature", feature.name()); assertArrayEquals(Arrays.asList("param1", "param2").toArray(), feature.queryParams().toArray()); assertEquals("mustache", feature.templateLanguage()); assertEquals(new MatchQueryBuilder("match_field", "match_word").toString(NOT_PRETTY), feature.template()); assertFalse(feature.templateAsString()); }
public void testParseErrorOnMissingName() throws IOException { String featureString = "{\n" + "\"params\":[\"param1\",\"param2\"]," + "\"template_language\":\"mustache\",\n" + "\"template\": \n" + new MatchQueryBuilder("match_field", "match_word").toString() + "}"; assertThat(expectThrows(ParsingException.class, () -> parse(featureString)).getMessage(), equalTo("Field [name] is mandatory")); }
public void testParseWithExternalName() throws IOException { String featureString = "{\n" + "\"params\":[\"param1\",\"param2\"]," + "\"template_language\":\"mustache\",\n" + "\"template\": \n" + new MatchQueryBuilder("match_field", "match_word").toString() + "}"; StoredFeature set = parse(featureString, "my_feature"); assertEquals("my_feature", set.name()); }
public void testParseWithInconsistentExternalName() throws IOException { String featureString = "{\n" + "\"name\": \"testFeature\",\n" + "\"params\":[\"param1\",\"param2\"]," + "\"template_language\":\"mustache\",\n" + "\"template\": \n" + new MatchQueryBuilder("match_field", "match_word").toString() + "}"; assertThat(expectThrows(ParsingException.class, () -> parse(featureString, "testFeature2")).getMessage(), CoreMatchers.equalTo("Invalid [name], expected [testFeature2] but got [testFeature]")); }
public void testParseErrorOnUnknownField() throws IOException { String featureString = "{\n" + "\"name\":\"testFeature\"," + "\"params\":[\"param1\",\"param2\"]," + "\"template_language\":\"mustache\",\n" + "\n\"bogusField\":\"oops\"," + "\"template\": \n" + new MatchQueryBuilder("match_field", "match_word").toString() + "}"; assertThat(expectThrows(ParsingException.class, () -> parse(featureString)).getMessage(), containsString("bogusField")); }
public void testParseWithoutParams() throws IOException { String featureString = "{\n" + "\"name\":\"testFeature\"," + "\"template_language\":\"mustache\",\n" + "\"template\": \n" + new MatchQueryBuilder("match_field", "match_word").toString() + "}"; StoredFeature feat = parse(featureString); assertTrue(feat.queryParams().isEmpty()); }
public void testParseWithEmptyParams() throws IOException { String featureString = "{\n" + "\"name\":\"testFeature\"," + "\"params\":[]," + "\"template_language\":\"mustache\",\n" + "\"template\": \n" + new MatchQueryBuilder("match_field", "match_word").toString() + "}"; StoredFeature feat = parse(featureString); assertTrue(feat.queryParams().isEmpty()); }
public void testRamBytesUsed() throws IOException, InterruptedException { String featureString = "{\n" + "\"name\":\"testFeature\"," + "\"params\":[\"param1\",\"param2\"]," + "\"template_language\":\"mustache\",\n" + "\"template\":\"" + new MatchQueryBuilder("match_field", "match_word").toString(NOT_PRETTY).replace("\"", "\\\"") + "\"}"; StoredFeature feature = parse(featureString); long approxSize = featureString.length()*Character.BYTES; assertThat(feature.ramBytesUsed(), allOf(greaterThan((long) (approxSize*0.66)), lessThan((long) (approxSize*1.33)))); }
public void testMustacheOptimization() throws IOException { String featureString = "{\n" + "\"name\":\"testFeature\"," + "\"params\":[\"param1\",\"param2\"]," + "\"template_language\":\"mustache\",\n" + "\"template\":\"" + new MatchQueryBuilder("match_field", "match_word").toString(NOT_PRETTY).replace("\"", "\\\"") + "\"}"; StoredFeature feature = parse(featureString); assertThat(feature.optimize(), instanceOf(PrecompiledTemplateFeature.class)); }
public void testDontOptimizeOnThirdPartyTemplateEngine() throws IOException { String featureString = "{\n" + "\"name\":\"testFeature\"," + "\"params\":[\"param1\",\"param2\"]," + "\"template_language\":\"third_party_template_engine\",\n" + "\"template\":\"" + new MatchQueryBuilder("match_field", "match_word").toString(NOT_PRETTY).replace("\"", "\\\"") + "\"}"; StoredFeature feature = parse(featureString); assertSame(feature, feature.optimize()); }
private static String generateRandomFeature(String name) { return "{\n" + "\"name\": \"" + name + "\",\n" + "\"params\": [\"" + rName() + "\", \"" + rName() + "\"],\n" + "\"template_language\": \"" + rName() + "\",\n" + "\"template\": \n" + new MatchQueryBuilder(rName(), randomRealisticUnicodeString(random())).toString() + "\n}\n"; }
@Override protected LtrQueryBuilder doCreateTestQueryBuilder() { LtrQueryBuilder builder = new LtrQueryBuilder(); builder.features(Arrays.asList( new MatchQueryBuilder("foo", "bar"), new MatchQueryBuilder("baz", "sham") )); builder.rankerScript(new Script(ScriptType.INLINE, "ranklib", // Remove escape sequences simpleModel.replace("\\\"", "\"") .replace("\\n", "\n"), Collections.emptyMap())); return builder; }
private List<QueryBuilder> createQueryBuilders(Map<ProductSearchField, Object> input) { final List<QueryBuilder> queryBuilders = new ArrayList<>(); final MatchQueryBuilder productNameQuery = addMatchQueryBuilder(queryBuilders, input, ProductSearchField.GTIN_NM); final MatchQueryBuilder brandNameQuery = addMatchQueryBuilder(queryBuilders, input, ProductSearchField.BRAND_NM); final MatchQueryBuilder descriptionQuery = addMatchQueryBuilder(queryBuilders, input, ProductSearchField.ALL, "_all"); if (descriptionQuery != null) { // description is there if (productNameQuery == null) { // also apply description to "product name" addMatchQueryBuilder(queryBuilders, input, ProductSearchField.ALL, ProductSearchField.GTIN_NM.getFieldName()); } if (brandNameQuery == null) { // also apply description to "brand name" addMatchQueryBuilder(queryBuilders, input, ProductSearchField.ALL, ProductSearchField.BRAND_NM.getFieldName()); } } return queryBuilders; }
private MatchQueryBuilder addMatchQueryBuilder(List<QueryBuilder> queryBuilders, Map<ProductSearchField, Object> input, ProductSearchField type, String fieldName) { final Object value = input.get(type); if (value != null) { final MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery(fieldName, value); queryBuilders.add(queryBuilder); return queryBuilder; } return null; }
@Test public void testQueryMatchAnd() throws Exception { // Prepare populateWithTestDocs(); // Action: Single expected match on exact title final String matchTitleQuery = QueryBuilders.matchQuery("title", "42Six HQ").operator(MatchQueryBuilder.Operator.AND).toString(); final SearchResult results = client.get(matchTitleQuery, TEST_TYPE, null, null, null, null, 0, (short) -1, null, USER_TOKEN); // Assertion assertEquals(1, results.getTotalHits()); assertEquals(oneaaColumbiaDoc.get_id(), results.getMatchingDocuments().get(0).get_id()); }
@Test public void testQueryFacet() { MatchQueryBuilder matchQuery = QueryBuilders.matchQuery(fieldShakeSpeare, "havoc"); QueryFacetBuilder queryFacet = FacetBuilders.queryFacet(queryFacetName, matchQuery); // no need to add query since facet itself is a query SearchResponse response = client.prepareSearch(indexShakeSpeare).addFacet(queryFacet).execute().actionGet(); QueryFacet f = (QueryFacet) response.getFacets().facetsAsMap().get(queryFacetName); // Number of docs that matched System.out.println(String.format("Number of docs that matched: %d", f.getCount())); }
@Before public void setup() { templateBase = new MatchQueryBuilder("field", "some values"); }
private void registerQueryParsers(List<SearchPlugin> plugins) { registerQuery(new QuerySpec<>(MatchQueryBuilder.NAME, MatchQueryBuilder::new, MatchQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MatchPhraseQueryBuilder.NAME, MatchPhraseQueryBuilder::new, MatchPhraseQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MatchPhrasePrefixQueryBuilder.NAME, MatchPhrasePrefixQueryBuilder::new, MatchPhrasePrefixQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MultiMatchQueryBuilder.NAME, MultiMatchQueryBuilder::new, MultiMatchQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(NestedQueryBuilder.NAME, NestedQueryBuilder::new, NestedQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(HasChildQueryBuilder.NAME, HasChildQueryBuilder::new, HasChildQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(HasParentQueryBuilder.NAME, HasParentQueryBuilder::new, HasParentQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(DisMaxQueryBuilder.NAME, DisMaxQueryBuilder::new, DisMaxQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(IdsQueryBuilder.NAME, IdsQueryBuilder::new, IdsQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MatchAllQueryBuilder.NAME, MatchAllQueryBuilder::new, MatchAllQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(QueryStringQueryBuilder.NAME, QueryStringQueryBuilder::new, QueryStringQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(BoostingQueryBuilder.NAME, BoostingQueryBuilder::new, BoostingQueryBuilder::fromXContent)); BooleanQuery.setMaxClauseCount(INDICES_MAX_CLAUSE_COUNT_SETTING.get(settings)); registerQuery(new QuerySpec<>(BoolQueryBuilder.NAME, BoolQueryBuilder::new, BoolQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(TermQueryBuilder.NAME, TermQueryBuilder::new, TermQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(TermsQueryBuilder.NAME, TermsQueryBuilder::new, TermsQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(FuzzyQueryBuilder.NAME, FuzzyQueryBuilder::new, FuzzyQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(RegexpQueryBuilder.NAME, RegexpQueryBuilder::new, RegexpQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(RangeQueryBuilder.NAME, RangeQueryBuilder::new, RangeQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(PrefixQueryBuilder.NAME, PrefixQueryBuilder::new, PrefixQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(WildcardQueryBuilder.NAME, WildcardQueryBuilder::new, WildcardQueryBuilder::fromXContent)); registerQuery( new QuerySpec<>(ConstantScoreQueryBuilder.NAME, ConstantScoreQueryBuilder::new, ConstantScoreQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanTermQueryBuilder.NAME, SpanTermQueryBuilder::new, SpanTermQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanNotQueryBuilder.NAME, SpanNotQueryBuilder::new, SpanNotQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanWithinQueryBuilder.NAME, SpanWithinQueryBuilder::new, SpanWithinQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanContainingQueryBuilder.NAME, SpanContainingQueryBuilder::new, SpanContainingQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(FieldMaskingSpanQueryBuilder.NAME, FieldMaskingSpanQueryBuilder::new, FieldMaskingSpanQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanFirstQueryBuilder.NAME, SpanFirstQueryBuilder::new, SpanFirstQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanNearQueryBuilder.NAME, SpanNearQueryBuilder::new, SpanNearQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(SpanOrQueryBuilder.NAME, SpanOrQueryBuilder::new, SpanOrQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MoreLikeThisQueryBuilder.NAME, MoreLikeThisQueryBuilder::new, MoreLikeThisQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(WrapperQueryBuilder.NAME, WrapperQueryBuilder::new, WrapperQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(CommonTermsQueryBuilder.NAME, CommonTermsQueryBuilder::new, CommonTermsQueryBuilder::fromXContent)); registerQuery( new QuerySpec<>(SpanMultiTermQueryBuilder.NAME, SpanMultiTermQueryBuilder::new, SpanMultiTermQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(FunctionScoreQueryBuilder.NAME, FunctionScoreQueryBuilder::new, FunctionScoreQueryBuilder::fromXContent)); registerQuery( new QuerySpec<>(SimpleQueryStringBuilder.NAME, SimpleQueryStringBuilder::new, SimpleQueryStringBuilder::fromXContent)); registerQuery(new QuerySpec<>(TypeQueryBuilder.NAME, TypeQueryBuilder::new, TypeQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(ScriptQueryBuilder.NAME, ScriptQueryBuilder::new, ScriptQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(GeoDistanceQueryBuilder.NAME, GeoDistanceQueryBuilder::new, GeoDistanceQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(GeoBoundingBoxQueryBuilder.NAME, GeoBoundingBoxQueryBuilder::new, GeoBoundingBoxQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(GeoPolygonQueryBuilder.NAME, GeoPolygonQueryBuilder::new, GeoPolygonQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(ExistsQueryBuilder.NAME, ExistsQueryBuilder::new, ExistsQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(MatchNoneQueryBuilder.NAME, MatchNoneQueryBuilder::new, MatchNoneQueryBuilder::fromXContent)); registerQuery(new QuerySpec<>(ParentIdQueryBuilder.NAME, ParentIdQueryBuilder::new, ParentIdQueryBuilder::fromXContent)); if (ShapesAvailability.JTS_AVAILABLE && ShapesAvailability.SPATIAL4J_AVAILABLE) { registerQuery(new QuerySpec<>(GeoShapeQueryBuilder.NAME, GeoShapeQueryBuilder::new, GeoShapeQueryBuilder::fromXContent)); } registerFromPlugin(plugins, SearchPlugin::getQueries, this::registerQuery); }
public void testSingleField() throws NoSuchFieldException, IllegalAccessException { SearchResponse searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("15", "skill"))).get(); assertNoFailures(searchResponse); assertFirstHit(searchResponse, hasId("theone")); searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("15", "skill", "int-field")).analyzer("category")).get(); assertNoFailures(searchResponse); assertFirstHit(searchResponse, hasId("theone")); String[] fields = { "full_name", "first_name", "last_name", "last_name_phrase", "first_name_phrase", "category_phrase", "category", "missing_field", "missing_fields*" }; String[] query = {"marvel","hero", "captain", "america", "15", "17", "1", "5", "ultimate", "Man", "marvel", "wolferine", "ninja"}; // check if it's equivalent to a match query. int numIters = scaledRandomIntBetween(10, 100); for (int i = 0; i < numIters; i++) { String field = RandomPicks.randomFrom(random(), fields); int numTerms = randomIntBetween(1, query.length); StringBuilder builder = new StringBuilder(); for (int j = 0; j < numTerms; j++) { builder.append(RandomPicks.randomFrom(random(), query)).append(" "); } MultiMatchQueryBuilder multiMatchQueryBuilder = randomizeType(multiMatchQuery(builder.toString(), field)); SearchResponse multiMatchResp = client().prepareSearch("test") // _uid sort field is a tie, in case hits have the same score, // the hits will be sorted the same consistently .addSort("_score", SortOrder.DESC) .addSort("_uid", SortOrder.ASC) .setQuery(multiMatchQueryBuilder).get(); MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(field, builder.toString()); if (multiMatchQueryBuilder.getType() != null) { matchQueryBuilder.type(MatchQuery.Type.valueOf(multiMatchQueryBuilder.getType().matchQueryType().toString())); } SearchResponse matchResp = client().prepareSearch("test") // _uid tie sort .addSort("_score", SortOrder.DESC) .addSort("_uid", SortOrder.ASC) .setQuery(matchQueryBuilder).get(); assertThat("field: " + field + " query: " + builder.toString(), multiMatchResp.getHits().getTotalHits(), equalTo(matchResp.getHits().getTotalHits())); SearchHits hits = multiMatchResp.getHits(); if (field.startsWith("missing")) { assertEquals(0, hits.getHits().length); } for (int j = 0; j < hits.getHits().length; j++) { assertThat(hits.getHits()[j].getScore(), equalTo(matchResp.getHits().getHits()[j].getScore())); assertThat(hits.getHits()[j].getId(), equalTo(matchResp.getHits().getHits()[j].getId())); } } }
public void testCutoffFreq() throws ExecutionException, InterruptedException { final long numDocs = client().prepareSearch("test").setSize(0) .setQuery(matchAllQuery()).get().getHits().getTotalHits(); MatchQuery.Type type = randomBoolean() ? MatchQueryBuilder.DEFAULT_TYPE : MatchQuery.Type.BOOLEAN; Float cutoffFrequency = randomBoolean() ? Math.min(1, numDocs * 1.f / between(10, 20)) : 1.f / between(10, 20); SearchResponse searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("marvel hero captain america", "full_name", "first_name", "last_name", "category") .operator(Operator.OR).cutoffFrequency(cutoffFrequency))).get(); Set<String> topNIds = Sets.newHashSet("theone", "theother"); for (int i = 0; i < searchResponse.getHits().getHits().length; i++) { topNIds.remove(searchResponse.getHits().getAt(i).getId()); // very likely that we hit a random doc that has the same score so orders are random since // the doc id is the tie-breaker } assertThat(topNIds, empty()); assertThat(searchResponse.getHits().getHits()[0].getScore(), greaterThanOrEqualTo(searchResponse.getHits().getHits()[1].getScore())); cutoffFrequency = randomBoolean() ? Math.min(1, numDocs * 1.f / between(10, 20)) : 1.f / between(10, 20); searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("marvel hero captain america", "full_name", "first_name", "last_name", "category") .operator(Operator.OR).useDisMax(false).cutoffFrequency(cutoffFrequency).type(type))).get(); assertFirstHit(searchResponse, anyOf(hasId("theone"), hasId("theother"))); assertThat(searchResponse.getHits().getHits()[0].getScore(), greaterThan(searchResponse.getHits().getHits()[1].getScore())); long size = searchResponse.getHits().getTotalHits(); searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("marvel hero captain america", "full_name", "first_name", "last_name", "category") .operator(Operator.OR).useDisMax(false).type(type))).get(); assertFirstHit(searchResponse, anyOf(hasId("theone"), hasId("theother"))); assertThat("common terms expected to be a way smaller result set", size, lessThan(searchResponse.getHits().getTotalHits())); cutoffFrequency = randomBoolean() ? Math.min(1, numDocs * 1.f / between(10, 20)) : 1.f / between(10, 20); searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("marvel hero", "full_name", "first_name", "last_name", "category") .operator(Operator.OR).cutoffFrequency(cutoffFrequency).type(type))).get(); assertFirstHit(searchResponse, hasId("theother")); searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("captain america", "full_name", "first_name", "last_name", "category") .operator(Operator.AND).cutoffFrequency(cutoffFrequency).type(type))).get(); assertHitCount(searchResponse, 1L); assertFirstHit(searchResponse, hasId("theone")); searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("captain america", "full_name", "first_name", "last_name", "category") .operator(Operator.AND).cutoffFrequency(cutoffFrequency).type(type))).get(); assertHitCount(searchResponse, 1L); assertFirstHit(searchResponse, hasId("theone")); searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("marvel hero", "first_name", "last_name", "category") .operator(Operator.AND).cutoffFrequency(cutoffFrequency) .analyzer("category") .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS))).get(); assertHitCount(searchResponse, 1L); assertFirstHit(searchResponse, hasId("theother")); }
public void testHighlightNoMatchSizeNumberOfFragments() throws IOException { assertAcked(prepareCreate("test").addMapping("type1", "text", "type=text," + randomStoreField() + "term_vector=with_positions_offsets,index_options=offsets")); ensureGreen(); String text1 = "This is the first sentence. This is the second sentence." + HighlightUtils.PARAGRAPH_SEPARATOR; String text2 = "This is the third sentence. This is the fourth sentence."; String text3 = "This is the fifth sentence"; index("test", "type1", "1", "text", new String[] {text1, text2, text3}); refresh(); // The no match fragment should come from the first value of a multi-valued field HighlightBuilder.Field field = new HighlightBuilder.Field("text") .fragmentSize(1) .numOfFragments(0) .highlighterType("plain") .noMatchSize(20); SearchResponse response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get(); assertHighlight(response, 0, "text", 0, 1, equalTo("This is the first")); field.highlighterType("fvh"); response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get(); assertHighlight(response, 0, "text", 0, 1, equalTo("This is the first sentence")); // Postings hl also works but the fragment is the whole first sentence (size ignored) field.highlighterType("postings"); response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get(); assertHighlight(response, 0, "text", 0, 1, equalTo("This is the first sentence.")); // Unified hl also works but the fragment is the whole first sentence (size ignored) field.highlighterType("unified"); response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get(); assertHighlight(response, 0, "text", 0, 1, equalTo("This is the first sentence.")); //if there's a match we only return the values with matches (whole value as number_of_fragments == 0) MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("text", "third fifth"); field.highlighterType("plain"); response = client().prepareSearch("test").setQuery(queryBuilder).highlighter(new HighlightBuilder().field(field)).get(); assertHighlight(response, 0, "text", 0, 2, equalTo("This is the <em>third</em> sentence. This is the fourth sentence.")); assertHighlight(response, 0, "text", 1, 2, equalTo("This is the <em>fifth</em> sentence")); field.highlighterType("fvh"); response = client().prepareSearch("test").setQuery(queryBuilder).highlighter(new HighlightBuilder().field(field)).get(); assertHighlight(response, 0, "text", 0, 2, equalTo("This is the <em>third</em> sentence. This is the fourth sentence.")); assertHighlight(response, 0, "text", 1, 2, equalTo("This is the <em>fifth</em> sentence")); field.highlighterType("postings"); response = client().prepareSearch("test").setQuery(queryBuilder).highlighter(new HighlightBuilder().field(field)).get(); assertHighlight(response, 0, "text", 0, 2, equalTo("This is the <em>third</em> sentence. This is the fourth sentence.")); assertHighlight(response, 0, "text", 1, 2, equalTo("This is the <em>fifth</em> sentence")); field.highlighterType("unified"); response = client().prepareSearch("test").setQuery(queryBuilder).highlighter(new HighlightBuilder().field(field)).get(); assertHighlight(response, 0, "text", 0, 2, equalTo("This is the <em>third</em> sentence. This is the fourth sentence.")); assertHighlight(response, 0, "text", 1, 2, equalTo("This is the <em>fifth</em> sentence")); }