Search in sources :

Example 36 with QueryBuilder

use of org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.

the class InnerHitsIT method testDontExplode.

public void testDontExplode() throws Exception {
    assertAcked(prepareCreate("index1").addMapping("child", "_parent", "type=parent"));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("index1", "parent", "1").setSource("{}", XContentType.JSON));
    requests.add(client().prepareIndex("index1", "child", "1").setParent("1").setSource("field", "value1"));
    indexRandom(true, requests);
    QueryBuilder query = hasChildQuery("child", matchQuery("field", "value1"), ScoreMode.None).innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1), false);
    SearchResponse response = client().prepareSearch("index1").setQuery(query).addSort("_uid", SortOrder.ASC).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertAcked(prepareCreate("index2").addMapping("type", "nested", "type=nested"));
    client().prepareIndex("index2", "type", "1").setSource(jsonBuilder().startObject().startArray("nested").startObject().field("field", "value1").endObject().endArray().endObject()).setRefreshPolicy(IMMEDIATE).get();
    query = nestedQuery("nested", matchQuery("nested.field", "value1"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1), false);
    response = client().prepareSearch("index2").setQuery(query).addSort("_uid", SortOrder.ASC).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 37 with QueryBuilder

use of org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.

the class HighlighterSearchIT method checkMatchedFieldsCase.

private void checkMatchedFieldsCase(boolean requireFieldMatch) throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("foo").field("type", "text").field("term_vector", "with_positions_offsets").field("store", true).field("analyzer", "english").startObject("fields").startObject("plain").field("type", "text").field("term_vector", "with_positions_offsets").field("analyzer", "standard").endObject().endObject().endObject().startObject("bar").field("type", "text").field("term_vector", "with_positions_offsets").field("store", true).field("analyzer", "english").startObject("fields").startObject("plain").field("type", "text").field("term_vector", "with_positions_offsets").field("analyzer", "standard").endObject().endObject().endObject().endObject().endObject().endObject()));
    ensureGreen();
    index("test", "type1", "1", "foo", "running with scissors");
    index("test", "type1", "2", "foo", "cat cat junk junk junk junk junk junk junk cats junk junk", "bar", "cat cat junk junk junk junk junk junk junk cats junk junk");
    index("test", "type1", "3", "foo", "weird", "bar", "result");
    refresh();
    Field fooField = new Field("foo").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
    SearchRequestBuilder req = client().prepareSearch("test").highlighter(new HighlightBuilder().field(fooField));
    // First check highlighting without any matched fields set
    SearchResponse resp = req.setQuery(queryStringQuery("running scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    // And that matching a subfield doesn't automatically highlight it
    resp = req.setQuery(queryStringQuery("foo.plain:running scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("running with <em>scissors</em>"));
    // Add the subfield to the list of matched fields but don't match it.  Everything should still work
    // like before we added it.
    fooField = new Field("foo").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
    fooField.matchedFields("foo", "foo.plain");
    req = client().prepareSearch("test").highlighter(new HighlightBuilder().field(fooField));
    resp = req.setQuery(queryStringQuery("running scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    // Now make half the matches come from the stored field and half from just a matched field.
    resp = req.setQuery(queryStringQuery("foo.plain:running scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    // Now remove the stored field from the matched field list.  That should work too.
    fooField = new Field("foo").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
    fooField.matchedFields("foo.plain");
    req = client().prepareSearch("test").highlighter(new HighlightBuilder().field(fooField));
    resp = req.setQuery(queryStringQuery("foo.plain:running scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with scissors"));
    // Now make sure boosted fields don't blow up when matched fields is both the subfield and stored field.
    fooField = new Field("foo").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
    fooField.matchedFields("foo", "foo.plain");
    req = client().prepareSearch("test").highlighter(new HighlightBuilder().field(fooField));
    resp = req.setQuery(queryStringQuery("foo.plain:running^5 scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    // Now just all matches are against the matched field.  This still returns highlighting.
    resp = req.setQuery(queryStringQuery("foo.plain:running foo.plain:scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    // And all matched field via the queryString's field parameter, just in case
    resp = req.setQuery(queryStringQuery("running scissors").field("foo.plain")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    // Finding the same string two ways is ok too
    resp = req.setQuery(queryStringQuery("run foo.plain:running^5 scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    // But we use the best found score when sorting fragments
    resp = req.setQuery(queryStringQuery("cats foo.plain:cats^5").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
    // which can also be written by searching on the subfield
    resp = req.setQuery(queryStringQuery("cats").field("foo").field("foo.plain", 5)).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
    // Speaking of two fields, you can have two fields, only one of which has matchedFields enabled
    QueryBuilder twoFieldsQuery = queryStringQuery("cats").field("foo").field("foo.plain", 5).field("bar").field("bar.plain", 5);
    Field barField = new Field("bar").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
    resp = req.setQuery(twoFieldsQuery).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
    assertHighlight(resp, 0, "bar", 0, equalTo("<em>cat</em> <em>cat</em> junk junk junk junk"));
    // And you can enable matchedField highlighting on both
    barField.matchedFields("bar", "bar.plain");
    resp = req.setQuery(twoFieldsQuery).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
    assertHighlight(resp, 0, "bar", 0, equalTo("junk junk <em>cats</em> junk junk"));
    // Setting a matchedField that isn't searched/doesn't exist is simply ignored.
    barField.matchedFields("bar", "candy");
    resp = req.setQuery(twoFieldsQuery).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
    assertHighlight(resp, 0, "bar", 0, equalTo("<em>cat</em> <em>cat</em> junk junk junk junk"));
    // If the stored field doesn't have a value it doesn't matter what you match, you get nothing.
    barField.matchedFields("bar", "foo.plain");
    resp = req.setQuery(queryStringQuery("running scissors").field("foo.plain").field("bar")).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    assertThat(resp.getHits().getAt(0).getHighlightFields(), not(hasKey("bar")));
    // If the stored field is found but the matched field isn't then you don't get a result either.
    fooField.matchedFields("bar.plain");
    resp = req.setQuery(queryStringQuery("running scissors").field("foo").field("foo.plain").field("bar").field("bar.plain")).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
    assertThat(resp.getHits().getAt(0).getHighlightFields(), not(hasKey("foo")));
    // But if you add the stored field to the list of matched fields then you'll get a result again
    fooField.matchedFields("foo", "bar.plain");
    resp = req.setQuery(queryStringQuery("running scissors").field("foo").field("foo.plain").field("bar").field("bar.plain")).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    assertThat(resp.getHits().getAt(0).getHighlightFields(), not(hasKey("bar")));
    // You _can_ highlight fields that aren't subfields of one another.
    resp = req.setQuery(queryStringQuery("weird").field("foo").field("foo.plain").field("bar").field("bar.plain")).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>weird</em>"));
    assertHighlight(resp, 0, "bar", 0, equalTo("<em>resul</em>t"));
    assertFailures(req.setQuery(queryStringQuery("result").field("foo").field("foo.plain").field("bar").field("bar.plain")), RestStatus.INTERNAL_SERVER_ERROR, containsString("IndexOutOfBoundsException"));
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 38 with QueryBuilder

use of org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.

the class QueryProfilerIT method testNoProfile.

/**
     * This test makes sure no profile results are returned when profiling is disabled
     */
public void testNoProfile() throws Exception {
    createIndex("test");
    ensureGreen();
    int numDocs = randomIntBetween(100, 150);
    IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < numDocs; i++) {
        docs[i] = client().prepareIndex("test", "type1", String.valueOf(i)).setSource("field1", English.intToEnglish(i), "field2", i);
    }
    indexRandom(true, docs);
    refresh();
    QueryBuilder q = QueryBuilders.rangeQuery("field2").from(0).to(5);
    logger.info("Query: {}", q);
    SearchResponse resp = client().prepareSearch().setQuery(q).setProfile(false).execute().actionGet();
    assertThat("Profile response element should be an empty map", resp.getProfileResults().size(), equalTo(0));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) RandomQueryGenerator.randomQueryBuilder(org.elasticsearch.search.profile.query.RandomQueryGenerator.randomQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 39 with QueryBuilder

use of org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.

the class RandomQueryGenerator method randomIDsQuery.

private static QueryBuilder randomIDsQuery() {
    QueryBuilder q = QueryBuilders.idsQuery();
    int numIDs = randomInt(100);
    for (int i = 0; i < numIDs; i++) {
        ((IdsQueryBuilder) q).addIds(String.valueOf(randomInt()));
    }
    if (randomBoolean()) {
        ((IdsQueryBuilder) q).boost(randomFloat());
    }
    return q;
}
Also used : IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) DisMaxQueryBuilder(org.elasticsearch.index.query.DisMaxQueryBuilder) CommonTermsQueryBuilder(org.elasticsearch.index.query.CommonTermsQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) FuzzyQueryBuilder(org.elasticsearch.index.query.FuzzyQueryBuilder)

Example 40 with QueryBuilder

use of org.elasticsearch.index.query.QueryBuilder in project elasticsearch by elastic.

the class RandomQueryGenerator method randomCommonTermsQuery.

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;
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) DisMaxQueryBuilder(org.elasticsearch.index.query.DisMaxQueryBuilder) CommonTermsQueryBuilder(org.elasticsearch.index.query.CommonTermsQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) FuzzyQueryBuilder(org.elasticsearch.index.query.FuzzyQueryBuilder) CommonTermsQueryBuilder(org.elasticsearch.index.query.CommonTermsQueryBuilder)

Aggregations

QueryBuilder (org.elasticsearch.index.query.QueryBuilder)344 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)233 Test (org.testng.annotations.Test)156 DynamicEntity (org.molgenis.data.support.DynamicEntity)137 SearchResponse (org.elasticsearch.action.search.SearchResponse)52 QueryImpl (org.molgenis.data.support.QueryImpl)36 ArrayList (java.util.ArrayList)28 Map (java.util.Map)28 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)27 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)26 HashMap (java.util.HashMap)25 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)22 SearchHit (org.elasticsearch.search.SearchHit)20 IOException (java.io.IOException)19 AbstractQueryBuilder (org.elasticsearch.index.query.AbstractQueryBuilder)19 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)17 Test (org.junit.Test)16 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15 List (java.util.List)14 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)14