use of org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class FunctionScoreQueryBuilderTests method testParseSingleFunction.
public void testParseSingleFunction() throws IOException {
String functionScoreQuery = "{\n" + " \"function_score\":{\n" + " \"query\":{\n" + " \"term\":{\n" + " \"field1\":\"value1\"\n" + " }\n" + " },\n" + " \"gauss\": {\n" + " \"field_name\": {\n" + " \"origin\":0.5,\n" + " \"scale\":0.6\n" + " }\n" + " },\n" + " \"boost\" : 3,\n" + " \"score_mode\" : \"avg\",\n" + " \"boost_mode\" : \"replace\",\n" + " \"max_boost\" : 10\n" + " }\n" + "}";
QueryBuilder queryBuilder = parseQuery(functionScoreQuery);
/*
* given that we copy part of the decay functions as bytes, we test that fromXContent and toXContent both work no matter what the
* initial format was
*/
for (int i = 0; i <= XContentType.values().length; i++) {
assertThat(queryBuilder, instanceOf(FunctionScoreQueryBuilder.class));
FunctionScoreQueryBuilder functionScoreQueryBuilder = (FunctionScoreQueryBuilder) queryBuilder;
assertThat(functionScoreQueryBuilder.query(), instanceOf(TermQueryBuilder.class));
TermQueryBuilder termQueryBuilder = (TermQueryBuilder) functionScoreQueryBuilder.query();
assertThat(termQueryBuilder.fieldName(), equalTo("field1"));
assertThat(termQueryBuilder.value(), equalTo("value1"));
assertThat(functionScoreQueryBuilder.filterFunctionBuilders().length, equalTo(1));
assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getFilter(), instanceOf(MatchAllQueryBuilder.class));
assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction(), instanceOf(GaussDecayFunctionBuilder.class));
GaussDecayFunctionBuilder gaussDecayFunctionBuilder = (GaussDecayFunctionBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction();
assertThat(gaussDecayFunctionBuilder.getFieldName(), equalTo("field_name"));
assertThat(gaussDecayFunctionBuilder.getWeight(), nullValue());
assertThat(functionScoreQueryBuilder.boost(), equalTo(3f));
assertThat(functionScoreQueryBuilder.scoreMode(), equalTo(FiltersFunctionScoreQuery.ScoreMode.AVG));
assertThat(functionScoreQueryBuilder.boostMode(), equalTo(CombineFunction.REPLACE));
assertThat(functionScoreQueryBuilder.maxBoost(), equalTo(10f));
if (i < XContentType.values().length) {
BytesReference bytes = ((AbstractQueryBuilder) queryBuilder).buildAsBytes(XContentType.values()[i]);
try (XContentParser parser = createParser(XContentType.values()[i].xContent(), bytes)) {
queryBuilder = parseQuery(parser);
}
}
}
}
use of org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class AliasFilter method reparseFilter.
private QueryBuilder reparseFilter(QueryRewriteContext context) {
if (reparseAliases) {
// we are processing a filter received from a 5.0 node - we need to reparse this on the executing node
final IndexMetaData indexMetaData = context.getIndexSettings().getIndexMetaData();
/* Being static, parseAliasFilter doesn't have access to whatever guts it needs to parse a query. Instead of passing in a bunch
* of dependencies we pass in a function that can perform the parsing. */
CheckedFunction<byte[], QueryBuilder, IOException> filterParser = bytes -> {
try (XContentParser parser = XContentFactory.xContent(bytes).createParser(context.getXContentRegistry(), bytes)) {
return context.newParseContext(parser).parseInnerQueryBuilder();
}
};
return ShardSearchRequest.parseAliasFilter(filterParser, indexMetaData, aliases);
}
return filter;
}
use of org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class InnerHitsIT method testMatchesQueriesParentChildInnerHits.
public void testMatchesQueriesParentChildInnerHits() throws Exception {
assertAcked(prepareCreate("index").addMapping("child", "_parent", "type=parent"));
List<IndexRequestBuilder> requests = new ArrayList<>();
requests.add(client().prepareIndex("index", "parent", "1").setSource("{}", XContentType.JSON));
requests.add(client().prepareIndex("index", "child", "1").setParent("1").setSource("field", "value1"));
requests.add(client().prepareIndex("index", "child", "2").setParent("1").setSource("field", "value2"));
requests.add(client().prepareIndex("index", "parent", "2").setSource("{}", XContentType.JSON));
requests.add(client().prepareIndex("index", "child", "3").setParent("2").setSource("field", "value1"));
indexRandom(true, requests);
SearchResponse response = client().prepareSearch("index").setQuery(hasChildQuery("child", matchQuery("field", "value1").queryName("_name1"), ScoreMode.None).innerHit(new InnerHitBuilder(), false)).addSort("_uid", SortOrder.ASC).get();
assertHitCount(response, 2);
assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
assertThat(response.getHits().getAt(0).getInnerHits().get("child").getTotalHits(), equalTo(1L));
assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries().length, equalTo(1));
assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name1"));
assertThat(response.getHits().getAt(1).getId(), equalTo("2"));
assertThat(response.getHits().getAt(1).getInnerHits().get("child").getTotalHits(), equalTo(1L));
assertThat(response.getHits().getAt(1).getInnerHits().get("child").getAt(0).getMatchedQueries().length, equalTo(1));
assertThat(response.getHits().getAt(1).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name1"));
QueryBuilder query = hasChildQuery("child", matchQuery("field", "value2").queryName("_name2"), ScoreMode.None).innerHit(new InnerHitBuilder(), false);
response = client().prepareSearch("index").setQuery(query).addSort("_uid", SortOrder.ASC).get();
assertHitCount(response, 1);
assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
assertThat(response.getHits().getAt(0).getInnerHits().get("child").getTotalHits(), equalTo(1L));
assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries().length, equalTo(1));
assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name2"));
}
use of org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class InnerHitsIT method testMatchesQueriesNestedInnerHits.
public void testMatchesQueriesNestedInnerHits() throws Exception {
XContentBuilder builder = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("nested1").field("type", "nested").startObject("properties").startObject("n_field1").field("type", "keyword").endObject().endObject().endObject().startObject("field1").field("type", "long").endObject().endObject().endObject().endObject();
assertAcked(prepareCreate("test").addMapping("type1", builder));
ensureGreen();
List<IndexRequestBuilder> requests = new ArrayList<>();
int numDocs = randomIntBetween(2, 35);
requests.add(client().prepareIndex("test", "type1", "0").setSource(jsonBuilder().startObject().field("field1", 0).startArray("nested1").startObject().field("n_field1", "n_value1_1").field("n_field2", "n_value2_1").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_2").endObject().endArray().endObject()));
requests.add(client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("field1", 1).startArray("nested1").startObject().field("n_field1", "n_value1_8").field("n_field2", "n_value2_5").endObject().startObject().field("n_field1", "n_value1_3").field("n_field2", "n_value2_1").endObject().endArray().endObject()));
for (int i = 2; i < numDocs; i++) {
requests.add(client().prepareIndex("test", "type1", String.valueOf(i)).setSource(jsonBuilder().startObject().field("field1", i).startArray("nested1").startObject().field("n_field1", "n_value1_8").field("n_field2", "n_value2_5").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_2").endObject().endArray().endObject()));
}
indexRandom(true, requests);
waitForRelocation(ClusterHealthStatus.GREEN);
QueryBuilder query = boolQuery().should(termQuery("nested1.n_field1", "n_value1_1").queryName("test1")).should(termQuery("nested1.n_field1", "n_value1_3").queryName("test2")).should(termQuery("nested1.n_field2", "n_value2_2").queryName("test3"));
query = nestedQuery("nested1", query, ScoreMode.Avg).innerHit(new InnerHitBuilder().addSort(new FieldSortBuilder("nested1.n_field1").order(SortOrder.ASC)), false);
SearchResponse searchResponse = client().prepareSearch("test").setQuery(query).setSize(numDocs).addSort("field1", SortOrder.ASC).get();
assertNoFailures(searchResponse);
assertAllSuccessful(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo((long) numDocs));
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("0"));
assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getTotalHits(), equalTo(2L));
assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test1"));
assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(1).getMatchedQueries().length, equalTo(1));
assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(1).getMatchedQueries()[0], equalTo("test3"));
assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("1"));
assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getTotalHits(), equalTo(1L));
assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test2"));
for (int i = 2; i < numDocs; i++) {
assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(String.valueOf(i)));
assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getTotalHits(), equalTo(1L));
assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test3"));
}
}
use of org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.
the class MatchedQueriesIT method testMatchedWithWrapperQuery.
public void testMatchedWithWrapperQuery() throws Exception {
createIndex("test");
ensureGreen();
client().prepareIndex("test", "type1", "1").setSource("content", "Lorem ipsum dolor sit amet").get();
refresh();
QueryBuilder[] queries = new QueryBuilder[] { wrapperQuery(matchQuery("content", "amet").queryName("abc").buildAsBytes().utf8ToString()), constantScoreQuery(wrapperQuery(termQuery("content", "amet").queryName("abc").buildAsBytes().utf8ToString())) };
for (QueryBuilder query : queries) {
SearchResponse searchResponse = client().prepareSearch().setQuery(query).get();
assertHitCount(searchResponse, 1L);
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("abc"));
}
}
Aggregations