public static QueryBuilder urlParamsToQueryBuilder(RestRequest request) { String queryString = request.param("q"); if (queryString == null) { return null; } QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(queryString); queryBuilder.defaultField(request.param("df")); queryBuilder.analyzer(request.param("analyzer")); queryBuilder.analyzeWildcard(request.paramAsBoolean("analyze_wildcard", false)); queryBuilder.lenient(request.paramAsBoolean("lenient", null)); String defaultOperator = request.param("default_operator"); if (defaultOperator != null) { queryBuilder.defaultOperator(Operator.fromString(defaultOperator)); } return queryBuilder; }
public void testMatchQueryWithStackedStems() throws IOException { CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder() .put(indexSettings()) .put("index.analysis.analyzer.index.type", "custom") .put("index.analysis.analyzer.index.tokenizer", "standard") .put("index.analysis.analyzer.index.filter", "lowercase") .put("index.analysis.analyzer.search.type", "custom") .put("index.analysis.analyzer.search.tokenizer", "standard") .putArray("index.analysis.analyzer.search.filter", "lowercase", "keyword_repeat", "porter_stem", "unique_stem") .put("index.analysis.filter.unique_stem.type", "unique") .put("index.analysis.filter.unique_stem.only_on_same_position", true)); assertAcked(builder.addMapping("test", "text", "type=text,analyzer=index,search_analyzer=search")); client().prepareIndex("test", "test", "1").setSource("text", "the fox runs across the street").get(); refresh(); SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "fox runs").operator(Operator.AND)).get(); assertHitCount(searchResponse, 1); client().prepareIndex("test", "test", "2").setSource("text", "run fox run").get(); refresh(); searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "fox runs").operator(Operator.AND)).get(); assertHitCount(searchResponse, 2); }
public void testCustomWordDelimiterQueryString() { assertAcked(client().admin().indices().prepareCreate("test") .setSettings("analysis.analyzer.my_analyzer.type", "custom", "analysis.analyzer.my_analyzer.tokenizer", "whitespace", "analysis.analyzer.my_analyzer.filter", "custom_word_delimiter", "analysis.filter.custom_word_delimiter.type", "word_delimiter", "analysis.filter.custom_word_delimiter.generate_word_parts", "true", "analysis.filter.custom_word_delimiter.generate_number_parts", "false", "analysis.filter.custom_word_delimiter.catenate_numbers", "true", "analysis.filter.custom_word_delimiter.catenate_words", "false", "analysis.filter.custom_word_delimiter.split_on_case_change", "false", "analysis.filter.custom_word_delimiter.split_on_numerics", "false", "analysis.filter.custom_word_delimiter.stem_english_possessive", "false") .addMapping("type1", "field1", "type=text,analyzer=my_analyzer", "field2", "type=text,analyzer=my_analyzer")); client().prepareIndex("test", "type1", "1").setSource("field1", "foo bar baz", "field2", "not needed").get(); refresh(); SearchResponse response = client() .prepareSearch("test") .setQuery( queryStringQuery("foo.baz").useDisMax(false).defaultOperator(Operator.AND) .field("field1").field("field2")).get(); assertHitCount(response, 1L); }
public void testExplicitAllFieldsRequested() throws Exception { String indexBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-query-index-with-all.json"); prepareCreate("test2").setSource(indexBody, XContentType.JSON).get(); ensureGreen("test2"); List<IndexRequestBuilder> reqs = new ArrayList<>(); reqs.add(client().prepareIndex("test2", "doc", "1").setSource("f1", "foo", "f2", "eggplant")); indexRandom(true, false, reqs); SearchResponse resp = client().prepareSearch("test2").setQuery( queryStringQuery("foo eggplent").defaultOperator(Operator.AND)).get(); assertHitCount(resp, 0L); resp = client().prepareSearch("test2").setQuery( queryStringQuery("foo eggplent").defaultOperator(Operator.AND).useAllFields(true)).get(); assertHits(resp.getHits(), "1"); assertHitCount(resp, 1L); Exception e = expectThrows(Exception.class, () -> client().prepareSearch("test2").setQuery( queryStringQuery("blah").field("f1").useAllFields(true)).get()); assertThat(ExceptionsHelper.detailedMessage(e), containsString("cannot use [all_fields] parameter in conjunction with [default_field] or [fields]")); }
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 testPhraseType() { SearchResponse searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("Man the Ultimate", "full_name_phrase", "first_name_phrase", "last_name_phrase", "category_phrase") .operator(Operator.OR).type(MatchQuery.Type.PHRASE))).get(); assertFirstHit(searchResponse, hasId("ultimate2")); assertHitCount(searchResponse, 1L); searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("Captain", "full_name_phrase", "first_name_phrase", "last_name_phrase", "category_phrase") .operator(Operator.OR).type(MatchQuery.Type.PHRASE))).get(); assertThat(searchResponse.getHits().getTotalHits(), greaterThan(1L)); searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("the Ul", "full_name_phrase", "first_name_phrase", "last_name_phrase", "category_phrase") .operator(Operator.OR).type(MatchQuery.Type.PHRASE_PREFIX))).get(); assertSearchHits(searchResponse, "ultimate2", "ultimate1"); assertHitCount(searchResponse, 2L); }
private static QueryBuilder randomCommonTermsQuery(List<String> fields, int numDocs) { int numTerms = randomInt(numDocs); QueryBuilder q = QueryBuilders.commonTermsQuery(randomField(fields), randomQueryString(numTerms)); if (randomBoolean()) { ((CommonTermsQueryBuilder)q).boost(randomFloat()); } if (randomBoolean()) { ((CommonTermsQueryBuilder)q).cutoffFrequency(randomFloat()); } if (randomBoolean()) { ((CommonTermsQueryBuilder)q).highFreqMinimumShouldMatch(Integer.toString(randomInt(numTerms))) .highFreqOperator(randomBoolean() ? Operator.AND : Operator.OR); } if (randomBoolean()) { ((CommonTermsQueryBuilder)q).lowFreqMinimumShouldMatch(Integer.toString(randomInt(numTerms))) .lowFreqOperator(randomBoolean() ? Operator.AND : Operator.OR); } return q; }
public void testSimpleMultiTermAnd() throws ExecutionException, InterruptedException { indexRandom(true, false, getDocs()); // first search using regular synonym field using phrase SearchResponse searchResponse = client().prepareSearch(INDEX).setQuery(QueryBuilders.matchQuery("field", "say what the fudge") .operator(Operator.AND).analyzer("lower_syns")).get(); // Old synonyms work fine in that case, but it is coincidental assertHitCount(searchResponse, 1L); assertSearchHits(searchResponse, "1"); // same query using graph should find correct result searchResponse = client().prepareSearch(INDEX).setQuery(QueryBuilders.matchQuery("field", "say what the fudge") .operator(Operator.AND).analyzer("lower_graphsyns")).get(); assertHitCount(searchResponse, 1L); assertSearchHits(searchResponse, "1"); }
public void testMinShouldMatch() throws ExecutionException, InterruptedException { indexRandom(true, false, getDocs()); // no min should match SearchResponse searchResponse = client().prepareSearch(INDEX).setQuery(QueryBuilders.matchQuery("field", "three what the fudge foo") .operator(Operator.OR).analyzer("lower_graphsyns")).get(); assertHitCount(searchResponse, 6L); assertSearchHits(searchResponse, "1", "2", "3", "4", "5", "6"); // same query, with min_should_match of 2 searchResponse = client().prepareSearch(INDEX).setQuery(QueryBuilders.matchQuery("field", "three what the fudge foo") .operator(Operator.OR).analyzer("lower_graphsyns").minimumShouldMatch("80%")).get(); // three wtf foo = 2 terms, match #1 // three wtf bar baz = 3 terms, match #6 // three what the fudge foo = 4 terms, no match // three what the fudge bar baz = 4 terms, match #2 assertHitCount(searchResponse, 3L); assertSearchHits(searchResponse, "1", "2", "6"); }
public String find() { try { final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.sort("_score"); searchSourceBuilder.sort("_uid"); final String queryText = "*"; final SimpleQueryStringBuilder simpleQueryStringBuilder = new SimpleQueryStringBuilder(queryText); simpleQueryStringBuilder.defaultOperator(Operator.AND); searchSourceBuilder.query(simpleQueryStringBuilder); searchSourceBuilder.size(500); //LOGGER.info("Elasticsearch query: {}", searchSourceBuilder.toString()); final SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("tweets"); searchRequest.types("tweet"); searchRequest.source(searchSourceBuilder); final SearchResponse searchResponse = restHighLevelClient.search(searchRequest); return searchResponse.toString(); } catch (IOException e) { e.printStackTrace(); return e.getMessage(); } }
/** * simple match * use whitespace analyzer * @param field * @param phrase already stemmed * @param operator and /or * @return */ public SearchResponse match(String field, String phrase, Operator operator){ SearchResponse response = client.prepareSearch(indexName).setSize(this.numDocs). setTrackScores(false). setFetchSource(false).setExplain(false).setFetchSource(false). setQuery(QueryBuilders.matchQuery(field, phrase).operator(operator) .analyzer("whitespace")). execute().actionGet(); return response; // debug // XContentBuilder builder = XContentFactory.jsonBuilder(); // builder.startObject(); // System.out.println(response.toXContent(builder, ToXContent.EMPTY_PARAMS)); // builder.endObject(); // System.out.println(builder.string()); }
public static QueryBuilder simpleQueryBuilder(String q) { if (q.equals("yacyall")) return new MatchAllQueryBuilder(); final MultiMatchQueryBuilder qb = QueryBuilders .multiMatchQuery(q) .operator(Operator.AND) .zeroTermsQuery(ZeroTermsQuery.ALL); QUERY_DEFAULT_FIELDS.forEach((mapping, boost) -> qb.field(mapping.getSolrFieldName(), boost)); return qb; }
public void testMatchQueryWithSynonyms() throws IOException { CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder() .put(indexSettings()) .put("index.analysis.analyzer.index.type", "custom") .put("index.analysis.analyzer.index.tokenizer", "standard") .put("index.analysis.analyzer.index.filter", "lowercase") .put("index.analysis.analyzer.search.type", "custom") .put("index.analysis.analyzer.search.tokenizer", "standard") .putArray("index.analysis.analyzer.search.filter", "lowercase", "synonym") .put("index.analysis.filter.synonym.type", "synonym") .putArray("index.analysis.filter.synonym.synonyms", "fast, quick")); assertAcked(builder.addMapping("test", "text", "type=text,analyzer=index,search_analyzer=search")); client().prepareIndex("test", "test", "1").setSource("text", "quick brown fox").get(); refresh(); SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "quick").operator(Operator.AND)).get(); assertHitCount(searchResponse, 1); searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "quick brown").operator(Operator.AND)).get(); assertHitCount(searchResponse, 1); searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "fast").operator(Operator.AND)).get(); assertHitCount(searchResponse, 1); client().prepareIndex("test", "test", "2").setSource("text", "fast brown fox").get(); refresh(); searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "quick").operator(Operator.AND)).get(); assertHitCount(searchResponse, 2); searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "quick brown").operator(Operator.AND)).get(); assertHitCount(searchResponse, 2); }
public void testQueryStringWithSynonyms() throws IOException { CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder() .put(indexSettings()) .put("index.analysis.analyzer.index.type", "custom") .put("index.analysis.analyzer.index.tokenizer", "standard") .put("index.analysis.analyzer.index.filter", "lowercase") .put("index.analysis.analyzer.search.type", "custom") .put("index.analysis.analyzer.search.tokenizer", "standard") .putArray("index.analysis.analyzer.search.filter", "lowercase", "synonym") .put("index.analysis.filter.synonym.type", "synonym") .putArray("index.analysis.filter.synonym.synonyms", "fast, quick")); assertAcked(builder.addMapping("test", "text", "type=text,analyzer=index,search_analyzer=search")); client().prepareIndex("test", "test", "1").setSource("text", "quick brown fox").get(); refresh(); SearchResponse searchResponse = client().prepareSearch("test").setQuery(queryStringQuery("quick").defaultField("text").defaultOperator(Operator.AND)).get(); assertHitCount(searchResponse, 1); searchResponse = client().prepareSearch("test").setQuery(queryStringQuery("quick brown").defaultField("text").defaultOperator(Operator.AND)).get(); assertHitCount(searchResponse, 1); searchResponse = client().prepareSearch().setQuery(queryStringQuery("fast").defaultField("text").defaultOperator(Operator.AND)).get(); assertHitCount(searchResponse, 1); client().prepareIndex("test", "test", "2").setSource("text", "fast brown fox").get(); refresh(); searchResponse = client().prepareSearch("test").setQuery(queryStringQuery("quick").defaultField("text").defaultOperator(Operator.AND)).get(); assertHitCount(searchResponse, 2); searchResponse = client().prepareSearch("test").setQuery(queryStringQuery("quick brown").defaultField("text").defaultOperator(Operator.AND)).get(); assertHitCount(searchResponse, 2); }
public void testExplicitAllFieldsRequested() throws Exception { String indexBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-query-index-with-all.json"); prepareCreate("test") .setSource(indexBody, XContentType.JSON) // .setSettings(Settings.builder().put("index.version.created", Version.V_5_0_0.id)).get(); .get(); ensureGreen("test"); List<IndexRequestBuilder> reqs = new ArrayList<>(); reqs.add(client().prepareIndex("test", "doc", "1").setSource("f1", "foo", "f2", "eggplant")); indexRandom(true, false, reqs); SearchResponse resp = client().prepareSearch("test").setQuery( simpleQueryStringQuery("foo eggplent").defaultOperator(Operator.AND)).get(); assertHitCount(resp, 0L); resp = client().prepareSearch("test").setQuery( simpleQueryStringQuery("foo eggplent").defaultOperator(Operator.AND).useAllFields(true)).get(); assertHits(resp.getHits(), "1"); assertHitCount(resp, 1L); Exception e = expectThrows(Exception.class, () -> client().prepareSearch("test").setQuery( simpleQueryStringQuery("blah").field("f1").useAllFields(true)).get()); assertThat(ExceptionsHelper.detailedMessage(e), containsString("cannot use [all_fields] parameter in conjunction with [fields]")); }
public void testSynonyms() throws IOException { Builder builder = Settings.builder() .put(indexSettings()) .put("index.analysis.analyzer.synonym.tokenizer", "whitespace") .putArray("index.analysis.analyzer.synonym.filter", "synonym", "lowercase") .put("index.analysis.filter.synonym.type", "synonym") .putArray("index.analysis.filter.synonym.synonyms", "fast,quick"); assertAcked(prepareCreate("test").setSettings(builder.build()) .addMapping("type1", "field1", "type=text,term_vector=with_positions_offsets,search_analyzer=synonym," + "analyzer=english,index_options=offsets")); ensureGreen(); client().prepareIndex("test", "type1", "0").setSource( "field1", "The quick brown fox jumps over the lazy dog").get(); refresh(); for (String highlighterType : ALL_TYPES) { logger.info("--> highlighting (type=" + highlighterType + ") and searching on field1"); SearchSourceBuilder source = searchSource() .query(matchQuery("field1", "quick brown fox").operator(Operator.AND)) .highlighter( highlight() .field("field1") .order("score") .preTags("<x>") .postTags("</x>") .highlighterType(highlighterType)); SearchResponse searchResponse = client().search(searchRequest("test").source(source)).actionGet(); assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <x>quick</x> <x>brown</x> <x>fox</x> jumps over the lazy dog")); source = searchSource() .query(matchQuery("field1", "fast brown fox").operator(Operator.AND)) .highlighter(highlight().field("field1").order("score").preTags("<x>").postTags("</x>")); searchResponse = client().search(searchRequest("test").source(source)).actionGet(); assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <x>quick</x> <x>brown</x> <x>fox</x> jumps over the lazy dog")); } }
public void testCommonTerms() throws ExecutionException, InterruptedException { String route = "commonTermsTest"; List<IndexRequestBuilder> builders = getDocs(); for (IndexRequestBuilder indexRequet : builders) { // route all docs to same shard for this test indexRequet.setRouting(route); } indexRandom(true, false, builders); // do a search with no cutoff frequency to show which docs should match SearchResponse searchResponse = client().prepareSearch(INDEX) .setRouting(route) .setQuery(QueryBuilders.matchQuery("field", "bar three happened") .operator(Operator.OR)).get(); assertHitCount(searchResponse, 4L); assertSearchHits(searchResponse, "1", "2", "5", "6"); // do same search with cutoff and see less documents match // in this case, essentially everything but "happened" gets excluded searchResponse = client().prepareSearch(INDEX) .setRouting(route) .setQuery(QueryBuilders.matchQuery("field", "bar three happened") .operator(Operator.OR).cutoffFrequency(1f)).get(); assertHitCount(searchResponse, 1L); assertSearchHits(searchResponse, "1"); }
private void addQuery(BoolQueryBuilder boolQuery, boolean positive, String query, String modifier) { if (Strings.isNullOrEmpty(query)) { return; } QueryStringQueryBuilder builder = QueryBuilders.queryStringQuery(query) .field("title." + modifier) .field("text." + modifier) .defaultOperator(Operator.AND); if (positive) { boolQuery.must(builder); } else { boolQuery.mustNot(builder); } }
/** * 根据domain获取结果 * * @param domain 网站域名 * @param size 每页数量 * @param page 页码 * @return */ public List<SpiderInfo> getByDomain(String domain, int size, int page) { SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME) .setTypes(TYPE_NAME) .setQuery(QueryBuilders.matchQuery("domain", domain).operator(Operator.AND)) .setSize(size).setFrom(size * (page - 1)); SearchResponse response = searchRequestBuilder.execute().actionGet(); return warpHits2List(response.getHits()); }
/** * simple match * use whitespace analyzer * @param field * @param phrase already stemmed * @param ids * @param operator * @return */ public SearchResponse match(String field, String phrase, String[] ids, Operator operator){ IdsQueryBuilder idsFilterBuilder = new IdsQueryBuilder(documentType); idsFilterBuilder.addIds(ids); SearchResponse response = client.prepareSearch(indexName).setSize(ids.length). setTrackScores(false). setFetchSource(false).setExplain(false).setFetchSource(false). setQuery(QueryBuilders.boolQuery() .should(QueryBuilders.matchQuery(field, phrase) .operator(operator).analyzer("whitespace")) .must(idsFilterBuilder)) .execute().actionGet(); return response; }
public void testPercolateQueryWithNestedDocuments() throws Exception { XContentBuilder mapping = XContentFactory.jsonBuilder(); mapping.startObject().startObject("properties").startObject("companyname").field("type", "text").endObject() .startObject("employee").field("type", "nested").startObject("properties") .startObject("name").field("type", "text").endObject().endObject().endObject().endObject() .endObject(); createIndex("test", client().admin().indices().prepareCreate("test") .addMapping("employee", mapping) .addMapping("queries", "query", "type=percolator") ); client().prepareIndex("test", "queries", "q1").setSource(jsonBuilder().startObject() .field("query", QueryBuilders.nestedQuery("employee", QueryBuilders.matchQuery("employee.name", "virginia potts").operator(Operator.AND), ScoreMode.Avg) ).endObject()) .get(); // this query should never match as it doesn't use nested query: client().prepareIndex("test", "queries", "q2").setSource(jsonBuilder().startObject() .field("query", QueryBuilders.matchQuery("employee.name", "virginia")).endObject()) .get(); client().admin().indices().prepareRefresh().get(); SearchResponse response = client().prepareSearch() .setQuery(new PercolateQueryBuilder("query", "employee", XContentFactory.jsonBuilder() .startObject().field("companyname", "stark") .startArray("employee") .startObject().field("name", "virginia potts").endObject() .startObject().field("name", "tony stark").endObject() .endArray() .endObject().bytes(), XContentType.JSON)) .addSort("_doc", SortOrder.ASC) .get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("q1")); response = client().prepareSearch() .setQuery(new PercolateQueryBuilder("query", "employee", XContentFactory.jsonBuilder() .startObject().field("companyname", "notstark") .startArray("employee") .startObject().field("name", "virginia stark").endObject() .startObject().field("name", "tony stark").endObject() .endArray() .endObject().bytes(), XContentType.JSON)) .addSort("_doc", SortOrder.ASC) .get(); assertHitCount(response, 0); response = client().prepareSearch() .setQuery(new PercolateQueryBuilder("query", "employee", XContentFactory.jsonBuilder().startObject().field("companyname", "notstark").endObject().bytes(), XContentType.JSON)) .addSort("_doc", SortOrder.ASC) .get(); assertHitCount(response, 0); }
public void testRescorePhrase() throws Exception { assertAcked(prepareCreate("test") .addMapping( "type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1") .field("analyzer", "whitespace").field("type", "text").endObject().endObject().endObject().endObject()) .setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", 1))); client().prepareIndex("test", "type1", "1").setSource("field1", "the quick brown fox").execute().actionGet(); client().prepareIndex("test", "type1", "2").setSource("field1", "the quick lazy huge brown fox jumps over the tree ").get(); client().prepareIndex("test", "type1", "3") .setSource("field1", "quick huge brown", "field2", "the quick lazy huge brown fox jumps over the tree").get(); refresh(); SearchResponse searchResponse = client().prepareSearch() .setQuery(QueryBuilders.matchQuery("field1", "the quick brown").operator(Operator.OR)) .setRescorer( queryRescorer(QueryBuilders.matchPhraseQuery("field1", "quick brown").slop(2).boost(4.0f)) .setRescoreQueryWeight(2), 5).execute().actionGet(); assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L)); assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore())); assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("3")); assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("2")); searchResponse = client().prepareSearch() .setQuery(QueryBuilders.matchQuery("field1", "the quick brown").operator(Operator.OR)) .setRescorer(queryRescorer(QueryBuilders.matchPhraseQuery("field1", "the quick brown").slop(3)), 5) .execute().actionGet(); assertHitCount(searchResponse, 3); assertFirstHit(searchResponse, hasId("1")); assertSecondHit(searchResponse, hasId("2")); assertThirdHit(searchResponse, hasId("3")); searchResponse = client().prepareSearch() .setQuery(QueryBuilders.matchQuery("field1", "the quick brown").operator(Operator.OR)) .setRescorer(queryRescorer((QueryBuilders.matchPhraseQuery("field1", "the quick brown"))), 5).execute() .actionGet(); assertHitCount(searchResponse, 3); assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore())); assertFirstHit(searchResponse, hasId("1")); assertSecondHit(searchResponse, hasId("2")); assertThirdHit(searchResponse, hasId("3")); }
public void testMoreDocs() throws Exception { Builder builder = Settings.builder(); builder.put("index.analysis.analyzer.synonym.tokenizer", "whitespace"); builder.putArray("index.analysis.analyzer.synonym.filter", "synonym", "lowercase"); builder.put("index.analysis.filter.synonym.type", "synonym"); builder.putArray("index.analysis.filter.synonym.synonyms", "ave => ave, avenue", "street => str, street"); XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") .startObject("field1").field("type", "text").field("analyzer", "whitespace").field("search_analyzer", "synonym") .endObject().endObject().endObject().endObject(); assertAcked(client().admin().indices().prepareCreate("test").addMapping("type1", mapping) .setSettings(builder.put("index.number_of_shards", 1))); client().prepareIndex("test", "type1", "1").setSource("field1", "massachusetts avenue boston massachusetts").execute().actionGet(); client().prepareIndex("test", "type1", "2").setSource("field1", "lexington avenue boston massachusetts").execute().actionGet(); client().prepareIndex("test", "type1", "3").setSource("field1", "boston avenue lexington massachusetts").execute().actionGet(); client().admin().indices().prepareRefresh("test").execute().actionGet(); client().prepareIndex("test", "type1", "4").setSource("field1", "boston road lexington massachusetts").execute().actionGet(); client().prepareIndex("test", "type1", "5").setSource("field1", "lexington street lexington massachusetts").execute().actionGet(); client().prepareIndex("test", "type1", "6").setSource("field1", "massachusetts avenue lexington massachusetts").execute() .actionGet(); client().prepareIndex("test", "type1", "7").setSource("field1", "bosten street san franciso california").execute().actionGet(); client().admin().indices().prepareRefresh("test").execute().actionGet(); client().prepareIndex("test", "type1", "8").setSource("field1", "hollywood boulevard los angeles california").execute().actionGet(); client().prepareIndex("test", "type1", "9").setSource("field1", "1st street boston massachussetts").execute().actionGet(); client().prepareIndex("test", "type1", "10").setSource("field1", "1st street boston massachusetts").execute().actionGet(); client().admin().indices().prepareRefresh("test").execute().actionGet(); client().prepareIndex("test", "type1", "11").setSource("field1", "2st street boston massachusetts").execute().actionGet(); client().prepareIndex("test", "type1", "12").setSource("field1", "3st street boston massachusetts").execute().actionGet(); client().admin().indices().prepareRefresh("test").execute().actionGet(); SearchResponse searchResponse = client() .prepareSearch() .setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)) .setFrom(0) .setSize(5) .setRescorer(queryRescorer(QueryBuilders.matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)) .setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).execute().actionGet(); assertThat(searchResponse.getHits().getHits().length, equalTo(5)); assertHitCount(searchResponse, 9); assertFirstHit(searchResponse, hasId("2")); assertSecondHit(searchResponse, hasId("6")); assertThirdHit(searchResponse, hasId("3")); searchResponse = client() .prepareSearch() .setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)) .setFrom(0) .setSize(5) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setRescorer(queryRescorer(QueryBuilders.matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)) .setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).execute().actionGet(); assertThat(searchResponse.getHits().getHits().length, equalTo(5)); assertHitCount(searchResponse, 9); assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore())); assertFirstHit(searchResponse, hasId("2")); assertSecondHit(searchResponse, hasId("6")); assertThirdHit(searchResponse, hasId("3")); // Make sure non-zero from works: searchResponse = client() .prepareSearch() .setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)) .setFrom(2) .setSize(5) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setRescorer(queryRescorer(QueryBuilders.matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)) .setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).execute().actionGet(); assertThat(searchResponse.getHits().getHits().length, equalTo(5)); assertHitCount(searchResponse, 9); assertThat(searchResponse.getHits().getMaxScore(), greaterThan(searchResponse.getHits().getHits()[0].getScore())); assertFirstHit(searchResponse, hasId("3")); }
public void testRescorerMadeScoresWorse() throws Exception { Builder builder = Settings.builder(); builder.put("index.analysis.analyzer.synonym.tokenizer", "whitespace"); builder.putArray("index.analysis.analyzer.synonym.filter", "synonym", "lowercase"); builder.put("index.analysis.filter.synonym.type", "synonym"); builder.putArray("index.analysis.filter.synonym.synonyms", "ave => ave, avenue", "street => str, street"); XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") .startObject("field1").field("type", "text").field("analyzer", "whitespace").field("search_analyzer", "synonym") .endObject().endObject().endObject().endObject(); assertAcked(client().admin().indices().prepareCreate("test").addMapping("type1", mapping) .setSettings(builder.put("index.number_of_shards", 1))); client().prepareIndex("test", "type1", "3").setSource("field1", "massachusetts").execute().actionGet(); client().prepareIndex("test", "type1", "6").setSource("field1", "massachusetts avenue lexington massachusetts").execute() .actionGet(); client().admin().indices().prepareRefresh("test").execute().actionGet(); client().prepareIndex("test", "type1", "1").setSource("field1", "lexington massachusetts avenue").execute().actionGet(); client().prepareIndex("test", "type1", "2").setSource("field1", "lexington avenue boston massachusetts road").execute().actionGet(); client().admin().indices().prepareRefresh("test").execute().actionGet(); SearchResponse searchResponse = client() .prepareSearch() .setQuery(QueryBuilders.matchQuery("field1", "massachusetts").operator(Operator.OR)) .setFrom(0) .setSize(5).execute().actionGet(); assertThat(searchResponse.getHits().getHits().length, equalTo(4)); assertHitCount(searchResponse, 4); assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore())); assertFirstHit(searchResponse, hasId("3")); assertSecondHit(searchResponse, hasId("6")); assertThirdHit(searchResponse, hasId("1")); assertFourthHit(searchResponse, hasId("2")); // Now, penalizing rescore (nothing matches the rescore query): searchResponse = client() .prepareSearch() .setQuery(QueryBuilders.matchQuery("field1", "massachusetts").operator(Operator.OR)) .setFrom(0) .setSize(5) .setRescorer(queryRescorer(QueryBuilders.matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)) .setQueryWeight(1.0f).setRescoreQueryWeight(-1f), 3).execute().actionGet(); // 6 and 1 got worse, and then the hit (2) outside the rescore window were sorted ahead: assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore())); assertFirstHit(searchResponse, hasId("3")); assertSecondHit(searchResponse, hasId("2")); assertThirdHit(searchResponse, hasId("6")); assertFourthHit(searchResponse, hasId("1")); }
public void testEquivalence() throws Exception { // no dummy docs since merges can change scores while we run queries. int numDocs = indexRandomNumbers("whitespace", -1, false); final int iters = scaledRandomIntBetween(50, 100); for (int i = 0; i < iters; i++) { int resultSize = numDocs; int rescoreWindow = between(1, 3) * resultSize; String intToEnglish = English.intToEnglish(between(0, numDocs-1)); String query = intToEnglish.split(" ")[0]; SearchResponse rescored = client() .prepareSearch() .setSearchType(SearchType.QUERY_THEN_FETCH) .setPreference("test") // ensure we hit the same shards for tie-breaking .setQuery(QueryBuilders.matchQuery("field1", query).operator(Operator.OR)) .setFrom(0) .setSize(resultSize) .setRescorer(queryRescorer(constantScoreQuery(QueryBuilders.matchPhraseQuery("field1", intToEnglish).slop(3))) .setQueryWeight(1.0f) // no weight - so we basically use the same score as the actual query .setRescoreQueryWeight(0.0f), rescoreWindow) .execute().actionGet(); SearchResponse plain = client().prepareSearch() .setSearchType(SearchType.QUERY_THEN_FETCH) .setPreference("test") // ensure we hit the same shards for tie-breaking .setQuery(QueryBuilders.matchQuery("field1", query).operator(Operator.OR)).setFrom(0).setSize(resultSize) .execute().actionGet(); // check equivalence assertEquivalent(query, plain, rescored); rescored = client() .prepareSearch() .setSearchType(SearchType.QUERY_THEN_FETCH) .setPreference("test") // ensure we hit the same shards for tie-breaking .setQuery(QueryBuilders.matchQuery("field1", query).operator(Operator.OR)) .setFrom(0) .setSize(resultSize) .setRescorer(queryRescorer(constantScoreQuery(matchPhraseQuery("field1", "not in the index").slop(3))) .setQueryWeight(1.0f) .setRescoreQueryWeight(1.0f), rescoreWindow).execute() .actionGet(); // check equivalence assertEquivalent(query, plain, rescored); rescored = client() .prepareSearch() .setSearchType(SearchType.QUERY_THEN_FETCH) .setPreference("test") // ensure we hit the same shards for tie-breaking .setQuery(QueryBuilders.matchQuery("field1", query).operator(Operator.OR)) .setFrom(0) .setSize(resultSize) .setRescorer(queryRescorer(matchPhraseQuery("field1", intToEnglish).slop(0)) .setQueryWeight(1.0f).setRescoreQueryWeight(1.0f), 2 * rescoreWindow).execute().actionGet(); // check equivalence or if the first match differs we check if the phrase is a substring of the top doc assertEquivalentOrSubstringMatch(intToEnglish, plain, rescored); } }
public void testCommonTermsQuery() throws Exception { client().admin().indices().prepareCreate("test") .addMapping("type1", "field1", "type=text,analyzer=whitespace") .setSettings(SETTING_NUMBER_OF_SHARDS, 1).get(); indexRandom(true, client().prepareIndex("test", "type1", "3").setSource("field1", "quick lazy huge brown pidgin", "field2", "the quick lazy huge brown fox jumps over the tree"), client().prepareIndex("test", "type1", "1").setSource("field1", "the quick brown fox"), client().prepareIndex("test", "type1", "2").setSource("field1", "the quick lazy huge brown fox jumps over the tree") ); SearchResponse searchResponse = client().prepareSearch().setQuery(commonTermsQuery("field1", "the quick brown").cutoffFrequency(3).lowFreqOperator(Operator.OR)).get(); assertHitCount(searchResponse, 3L); assertFirstHit(searchResponse, hasId("1")); assertSecondHit(searchResponse, hasId("2")); assertThirdHit(searchResponse, hasId("3")); searchResponse = client().prepareSearch().setQuery(commonTermsQuery("field1", "the quick brown").cutoffFrequency(3).lowFreqOperator(Operator.AND)).get(); assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L)); assertFirstHit(searchResponse, hasId("1")); assertSecondHit(searchResponse, hasId("2")); // Default searchResponse = client().prepareSearch().setQuery(commonTermsQuery("field1", "the quick brown").cutoffFrequency(3)).get(); assertHitCount(searchResponse, 3L); assertFirstHit(searchResponse, hasId("1")); assertSecondHit(searchResponse, hasId("2")); assertThirdHit(searchResponse, hasId("3")); searchResponse = client().prepareSearch().setQuery(commonTermsQuery("field1", "the huge fox").lowFreqMinimumShouldMatch("2")).get(); assertHitCount(searchResponse, 1L); assertFirstHit(searchResponse, hasId("2")); searchResponse = client().prepareSearch().setQuery(commonTermsQuery("field1", "the lazy fox brown").cutoffFrequency(1).highFreqMinimumShouldMatch("3")).get(); assertHitCount(searchResponse, 2L); assertFirstHit(searchResponse, hasId("2")); assertSecondHit(searchResponse, hasId("1")); searchResponse = client().prepareSearch().setQuery(commonTermsQuery("field1", "the lazy fox brown").cutoffFrequency(1).highFreqMinimumShouldMatch("4")).get(); assertHitCount(searchResponse, 1L); assertFirstHit(searchResponse, hasId("2")); // Default searchResponse = client().prepareSearch().setQuery(commonTermsQuery("field1", "the lazy fox brown").cutoffFrequency(1)).get(); assertHitCount(searchResponse, 1L); assertFirstHit(searchResponse, hasId("2")); searchResponse = client().prepareSearch().setQuery(commonTermsQuery("field1", "the quick brown").cutoffFrequency(3).analyzer("stop")).get(); assertHitCount(searchResponse, 3L); // stop drops "the" since its a stopword assertFirstHit(searchResponse, hasId("1")); assertSecondHit(searchResponse, hasId("3")); assertThirdHit(searchResponse, hasId("2")); // try the same with match query searchResponse = client().prepareSearch().setQuery(matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(Operator.AND)).get(); assertHitCount(searchResponse, 2L); assertFirstHit(searchResponse, hasId("1")); assertSecondHit(searchResponse, hasId("2")); searchResponse = client().prepareSearch().setQuery(matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(Operator.OR)).get(); assertHitCount(searchResponse, 3L); assertFirstHit(searchResponse, hasId("1")); assertSecondHit(searchResponse, hasId("2")); assertThirdHit(searchResponse, hasId("3")); searchResponse = client().prepareSearch().setQuery(matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(Operator.AND).analyzer("stop")).get(); assertHitCount(searchResponse, 3L); // stop drops "the" since its a stopword assertFirstHit(searchResponse, hasId("1")); assertSecondHit(searchResponse, hasId("3")); assertThirdHit(searchResponse, hasId("2")); // try the same with multi match query searchResponse = client().prepareSearch().setQuery(multiMatchQuery("the quick brown", "field1", "field2").cutoffFrequency(3).operator(Operator.AND)).get(); assertHitCount(searchResponse, 3L); assertFirstHit(searchResponse, hasId("3")); assertSecondHit(searchResponse, hasId("1")); assertThirdHit(searchResponse, hasId("2")); }
public void testMultiMatchQuery() throws Exception { createIndex("test"); indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value4", "field3", "value3"), client().prepareIndex("test", "type1", "2").setSource("field1", "value2", "field2", "value5", "field3", "value2"), client().prepareIndex("test", "type1", "3").setSource("field1", "value3", "field2", "value6", "field3", "value1") ); MultiMatchQueryBuilder builder = multiMatchQuery("value1 value2 value4", "field1", "field2"); SearchResponse searchResponse = client().prepareSearch().setQuery(builder) .addAggregation(AggregationBuilders.terms("field1").field("field1.keyword")).get(); assertHitCount(searchResponse, 2L); // this uses dismax so scores are equal and the order can be arbitrary assertSearchHits(searchResponse, "1", "2"); builder.useDisMax(false); searchResponse = client().prepareSearch() .setQuery(builder) .get(); assertHitCount(searchResponse, 2L); assertSearchHits(searchResponse, "1", "2"); client().admin().indices().prepareRefresh("test").get(); builder = multiMatchQuery("value1", "field1", "field2") .operator(Operator.AND); // Operator only applies on terms inside a field! Fields are always OR-ed together. searchResponse = client().prepareSearch() .setQuery(builder) .get(); assertHitCount(searchResponse, 1L); assertFirstHit(searchResponse, hasId("1")); refresh(); builder = multiMatchQuery("value1", "field1").field("field3", 1.5f) .operator(Operator.AND); // Operator only applies on terms inside a field! Fields are always OR-ed together. searchResponse = client().prepareSearch().setQuery(builder).get(); assertHitCount(searchResponse, 2L); assertSearchHits(searchResponse, "3", "1"); client().admin().indices().prepareRefresh("test").get(); builder = multiMatchQuery("value1").field("field1").field("field3", 1.5f) .operator(Operator.AND); // Operator only applies on terms inside a field! Fields are always OR-ed together. searchResponse = client().prepareSearch().setQuery(builder).get(); assertHitCount(searchResponse, 2L); assertSearchHits(searchResponse, "3", "1"); // Test lenient client().prepareIndex("test", "type1", "3").setSource("field1", "value7", "field2", "value8", "field4", 5).get(); refresh(); builder = multiMatchQuery("value1", "field1", "field2", "field4"); assertFailures(client().prepareSearch().setQuery(builder), RestStatus.BAD_REQUEST, containsString("NumberFormatException[For input string: \"value1\"]")); builder.lenient(true); searchResponse = client().prepareSearch().setQuery(builder).get(); assertHitCount(searchResponse, 1L); assertFirstHit(searchResponse, hasId("1")); }
public void testGraphQueries() throws Exception { String index = "graph_test_index"; setupIndexWithGraph(index); // phrase SearchResponse searchResponse = client().prepareSearch(index).setQuery( QueryBuilders.queryStringQuery("\"foo two three\"") .defaultField("field") .analyzer("lower_graphsyns")).get(); assertHitCount(searchResponse, 1L); assertSearchHits(searchResponse, "6"); // and searchResponse = client().prepareSearch(index).setQuery( QueryBuilders.queryStringQuery("say what the fudge") .defaultField("field") .splitOnWhitespace(false) .defaultOperator(Operator.AND) .analyzer("lower_graphsyns")).get(); assertHitCount(searchResponse, 1L); assertSearchHits(searchResponse, "1"); // and, split on whitespace means we should not recognize the multi-word synonym searchResponse = client().prepareSearch(index).setQuery( QueryBuilders.queryStringQuery("say what the fudge") .defaultField("field") .splitOnWhitespace(true) .defaultOperator(Operator.AND) .analyzer("lower_graphsyns")).get(); assertNoSearchHits(searchResponse); // or searchResponse = client().prepareSearch(index).setQuery( QueryBuilders.queryStringQuery("three what the fudge foo") .defaultField("field") .splitOnWhitespace(false) .defaultOperator(Operator.OR) .analyzer("lower_graphsyns")).get(); assertHitCount(searchResponse, 6L); assertSearchHits(searchResponse, "1", "2", "3", "4", "5", "6"); // min should match searchResponse = client().prepareSearch(index).setQuery( QueryBuilders.queryStringQuery("three what the fudge foo") .defaultField("field") .splitOnWhitespace(false) .defaultOperator(Operator.OR) .analyzer("lower_graphsyns") .minimumShouldMatch("80%")).get(); assertHitCount(searchResponse, 3L); assertSearchHits(searchResponse, "1", "2", "6"); }
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 testSimpleQueryString() throws ExecutionException, InterruptedException { createIndex("test"); indexRandom(true, false, client().prepareIndex("test", "type1", "1").setSource("body", "foo"), client().prepareIndex("test", "type1", "2").setSource("body", "bar"), client().prepareIndex("test", "type1", "3").setSource("body", "foo bar"), client().prepareIndex("test", "type1", "4").setSource("body", "quux baz eggplant"), client().prepareIndex("test", "type1", "5").setSource("body", "quux baz spaghetti"), client().prepareIndex("test", "type1", "6").setSource("otherbody", "spaghetti")); SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("foo bar")).get(); assertHitCount(searchResponse, 3L); assertSearchHits(searchResponse, "1", "2", "3"); // Tests boost value setting. In this case doc 1 should always be ranked above the other // two matches. searchResponse = client().prepareSearch().setQuery( boolQuery() .should(simpleQueryStringQuery("\"foo bar\"").boost(10.0f)) .should(termQuery("body", "eggplant"))).get(); assertHitCount(searchResponse, 2L); assertFirstHit(searchResponse, hasId("3")); searchResponse = client().prepareSearch().setQuery( simpleQueryStringQuery("foo bar").defaultOperator(Operator.AND)).get(); assertHitCount(searchResponse, 1L); assertFirstHit(searchResponse, hasId("3")); searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("\"quux baz\" +(eggplant | spaghetti)")).get(); assertHitCount(searchResponse, 2L); assertSearchHits(searchResponse, "4", "5"); searchResponse = client().prepareSearch().setQuery( simpleQueryStringQuery("eggplants").analyzer("snowball")).get(); assertHitCount(searchResponse, 1L); assertFirstHit(searchResponse, hasId("4")); searchResponse = client().prepareSearch().setQuery( simpleQueryStringQuery("spaghetti").field("body", 1000.0f).field("otherbody", 2.0f).queryName("myquery")).get(); assertHitCount(searchResponse, 2L); assertFirstHit(searchResponse, hasId("5")); assertSearchHits(searchResponse, "5", "6"); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("myquery")); searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("spaghetti").field("*body")).get(); assertHitCount(searchResponse, 2L); assertSearchHits(searchResponse, "5", "6"); }
public void testSimpleQueryStringFlags() throws ExecutionException, InterruptedException { createIndex("test"); indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("body", "foo"), client().prepareIndex("test", "type1", "2").setSource("body", "bar"), client().prepareIndex("test", "type1", "3").setSource("body", "foo bar"), client().prepareIndex("test", "type1", "4").setSource("body", "quux baz eggplant"), client().prepareIndex("test", "type1", "5").setSource("body", "quux baz spaghetti"), client().prepareIndex("test", "type1", "6").setSource("otherbody", "spaghetti")); SearchResponse searchResponse = client().prepareSearch().setQuery( simpleQueryStringQuery("foo bar").flags(SimpleQueryStringFlag.ALL)).get(); assertHitCount(searchResponse, 3L); assertSearchHits(searchResponse, "1", "2", "3"); searchResponse = client().prepareSearch().setQuery( simpleQueryStringQuery("foo | bar") .defaultOperator(Operator.AND) .flags(SimpleQueryStringFlag.OR)).get(); assertHitCount(searchResponse, 3L); assertSearchHits(searchResponse, "1", "2", "3"); searchResponse = client().prepareSearch().setQuery( simpleQueryStringQuery("foo | bar") .defaultOperator(Operator.AND) .flags(SimpleQueryStringFlag.NONE)).get(); assertHitCount(searchResponse, 1L); assertFirstHit(searchResponse, hasId("3")); searchResponse = client().prepareSearch().setQuery( simpleQueryStringQuery("baz | egg*") .defaultOperator(Operator.AND) .flags(SimpleQueryStringFlag.NONE)).get(); assertHitCount(searchResponse, 0L); searchResponse = client() .prepareSearch() .setSource( new SearchSourceBuilder().query(QueryBuilders.simpleQueryStringQuery("foo|bar").defaultOperator(Operator.AND) .flags(SimpleQueryStringFlag.NONE))).get(); assertHitCount(searchResponse, 1L); searchResponse = client() .prepareSearch() .setQuery( simpleQueryStringQuery("quuz~1 + egg*").flags(SimpleQueryStringFlag.WHITESPACE, SimpleQueryStringFlag.AND, SimpleQueryStringFlag.FUZZY, SimpleQueryStringFlag.PREFIX)).get(); assertHitCount(searchResponse, 1L); assertFirstHit(searchResponse, hasId("4")); }
public void testDocWithAllTypes() throws Exception { String indexBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-query-index.json"); prepareCreate("test").setSource(indexBody, XContentType.JSON).get(); ensureGreen("test"); List<IndexRequestBuilder> reqs = new ArrayList<>(); String docBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-example-document.json"); reqs.add(client().prepareIndex("test", "doc", "1").setSource(docBody, XContentType.JSON)); indexRandom(true, false, reqs); SearchResponse resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("foo")).get(); assertHits(resp.getHits(), "1"); resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("Bar")).get(); assertHits(resp.getHits(), "1"); resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("Baz")).get(); assertHits(resp.getHits(), "1"); resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("sbaz")).get(); assertHits(resp.getHits(), "1"); resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("19")).get(); assertHits(resp.getHits(), "1"); // nested doesn't match because it's hidden resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("1476383971")).get(); assertHits(resp.getHits(), "1"); // bool doesn't match resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("7")).get(); assertHits(resp.getHits(), "1"); resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("23")).get(); assertHits(resp.getHits(), "1"); resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("1293")).get(); assertHits(resp.getHits(), "1"); resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("42")).get(); assertHits(resp.getHits(), "1"); resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("1.7")).get(); assertHits(resp.getHits(), "1"); resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("1.5")).get(); assertHits(resp.getHits(), "1"); resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("12.23")).get(); assertHits(resp.getHits(), "1"); resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("127.0.0.1")).get(); assertHits(resp.getHits(), "1"); // binary doesn't match // suggest doesn't match // geo_point doesn't match // geo_shape doesn't match resp = client().prepareSearch("test").setQuery( simpleQueryStringQuery("foo Bar 19 127.0.0.1").defaultOperator(Operator.AND)).get(); assertHits(resp.getHits(), "1"); }
public void testNgramHighlightingWithBrokenPositions() throws IOException { assertAcked(prepareCreate("test") .addMapping("test", jsonBuilder() .startObject() .startObject("test") .startObject("properties") .startObject("name") .startObject("fields") .startObject("autocomplete") .field("type", "text") .field("analyzer", "autocomplete") .field("search_analyzer", "search_autocomplete") .field("term_vector", "with_positions_offsets") .endObject() .endObject() .field("type", "text") .endObject() .endObject() .endObject() .endObject()) .setSettings(Settings.builder() .put(indexSettings()) .put("analysis.tokenizer.autocomplete.max_gram", 20) .put("analysis.tokenizer.autocomplete.min_gram", 1) .put("analysis.tokenizer.autocomplete.token_chars", "letter,digit") .put("analysis.tokenizer.autocomplete.type", "nGram") .put("analysis.filter.wordDelimiter.type", "word_delimiter") .putArray("analysis.filter.wordDelimiter.type_table", "& => ALPHANUM", "| => ALPHANUM", "! => ALPHANUM", "? => ALPHANUM", ". => ALPHANUM", "- => ALPHANUM", "# => ALPHANUM", "% => ALPHANUM", "+ => ALPHANUM", ", => ALPHANUM", "~ => ALPHANUM", ": => ALPHANUM", "/ => ALPHANUM", "^ => ALPHANUM", "$ => ALPHANUM", "@ => ALPHANUM", ") => ALPHANUM", "( => ALPHANUM", "] => ALPHANUM", "[ => ALPHANUM", "} => ALPHANUM", "{ => ALPHANUM") .put("analysis.filter.wordDelimiter.type.split_on_numerics", false) .put("analysis.filter.wordDelimiter.generate_word_parts", true) .put("analysis.filter.wordDelimiter.generate_number_parts", false) .put("analysis.filter.wordDelimiter.catenate_words", true) .put("analysis.filter.wordDelimiter.catenate_numbers", true) .put("analysis.filter.wordDelimiter.catenate_all", false) .put("analysis.analyzer.autocomplete.tokenizer", "autocomplete") .putArray("analysis.analyzer.autocomplete.filter", "lowercase", "wordDelimiter") .put("analysis.analyzer.search_autocomplete.tokenizer", "whitespace") .putArray("analysis.analyzer.search_autocomplete.filter", "lowercase", "wordDelimiter"))); client().prepareIndex("test", "test", "1") .setSource("name", "ARCOTEL Hotels Deutschland").get(); refresh(); SearchResponse search = client().prepareSearch("test").setTypes("test") .setQuery(matchQuery("name.autocomplete", "deut tel").operator(Operator.OR)) .highlighter(new HighlightBuilder().field("name.autocomplete")).execute().actionGet(); assertHighlight(search, 0, "name.autocomplete", 0, equalTo("ARCO<em>TEL</em> Ho<em>tel</em>s <em>Deut</em>schland")); }
public List<SpiderInfo> findByDomain(String domain, int page, int size) { SearchResponse response = search().setSize(size).setFrom(size*(page-1)) .setQuery(QueryBuilders.matchQuery("domain", domain).operator(Operator.AND)).get(); return hitsToList(response.getHits()); }
public void exportWebpageJsonBySpiderUUID(String uuid, boolean includeRaw, OutputStream out) { exportWebpageJson(QueryBuilders.matchQuery("spiderUUID", uuid).operator(Operator.AND), includeRaw, out); }
public void exportWebpageJsonByDomain(String domain, boolean includeRaw, OutputStream out) { exportWebpageJson(QueryBuilders.matchQuery("domain", domain).operator(Operator.AND), includeRaw, out); }
public long DF(String field, String phrase, Operator operator){ SearchResponse response = this.match(field,phrase,operator); return response.getHits().getTotalHits(); }
public SuggestDeleteResponse deleteByQuery(final String queryString) { return deleteByQuery(QueryBuilders.queryStringQuery(queryString).defaultOperator(Operator.AND)); }