Search in sources :

Example 1 with IdsQueryBuilder

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

the class ChildQuerySearchIT method testHasChildInnerQueryType.

public void testHasChildInnerQueryType() {
    assertAcked(prepareCreate("test").addMapping("parent-type").addMapping("child-type", "_parent", "type=parent-type"));
    client().prepareIndex("test", "child-type", "child-id").setParent("parent-id").setSource("{}", XContentType.JSON).get();
    client().prepareIndex("test", "parent-type", "parent-id").setSource("{}", XContentType.JSON).get();
    refresh();
    //make sure that when we explicitly set a type, the inner query is executed in the context of the child type instead
    SearchResponse searchResponse = client().prepareSearch("test").setTypes("parent-type").setQuery(QueryBuilders.hasChildQuery("child-type", new IdsQueryBuilder().addIds("child-id"), ScoreMode.None)).get();
    assertSearchHits(searchResponse, "parent-id");
}
Also used : IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with IdsQueryBuilder

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

the class HighlightBuilderTests method setRandomCommonOptions.

@SuppressWarnings({ "rawtypes" })
private static void setRandomCommonOptions(AbstractHighlighterBuilder highlightBuilder) {
    if (randomBoolean()) {
        // need to set this together, otherwise parsing will complain
        highlightBuilder.preTags(randomStringArray(0, 3));
        highlightBuilder.postTags(randomStringArray(0, 3));
    }
    if (randomBoolean()) {
        highlightBuilder.fragmentSize(randomIntBetween(0, 100));
    }
    if (randomBoolean()) {
        highlightBuilder.numOfFragments(randomIntBetween(0, 10));
    }
    if (randomBoolean()) {
        highlightBuilder.highlighterType(randomAsciiOfLengthBetween(1, 10));
    }
    if (randomBoolean()) {
        highlightBuilder.fragmenter(randomAsciiOfLengthBetween(1, 10));
    }
    if (randomBoolean()) {
        QueryBuilder highlightQuery;
        switch(randomInt(2)) {
            case 0:
                highlightQuery = new MatchAllQueryBuilder();
                break;
            case 1:
                highlightQuery = new IdsQueryBuilder();
                break;
            default:
            case 2:
                highlightQuery = new TermQueryBuilder(randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10));
                break;
        }
        highlightQuery.boost((float) randomDoubleBetween(0, 10, false));
        highlightBuilder.highlightQuery(highlightQuery);
    }
    if (randomBoolean()) {
        if (randomBoolean()) {
            highlightBuilder.order(randomFrom(Order.values()));
        } else {
            // also test the string setter
            highlightBuilder.order(randomFrom(Order.values()).toString());
        }
    }
    if (randomBoolean()) {
        highlightBuilder.highlightFilter(randomBoolean());
    }
    if (randomBoolean()) {
        highlightBuilder.forceSource(randomBoolean());
    }
    if (randomBoolean()) {
        if (randomBoolean()) {
            highlightBuilder.boundaryScannerType(randomFrom(BoundaryScannerType.values()));
        } else {
            // also test the string setter
            highlightBuilder.boundaryScannerType(randomFrom(BoundaryScannerType.values()).toString());
        }
    }
    if (randomBoolean()) {
        highlightBuilder.boundaryMaxScan(randomIntBetween(0, 10));
    }
    if (randomBoolean()) {
        highlightBuilder.boundaryChars(randomAsciiOfLengthBetween(1, 10).toCharArray());
    }
    if (randomBoolean()) {
        highlightBuilder.boundaryScannerLocale(randomLocale(random()).toLanguageTag());
    }
    if (randomBoolean()) {
        highlightBuilder.noMatchSize(randomIntBetween(0, 10));
    }
    if (randomBoolean()) {
        highlightBuilder.phraseLimit(randomIntBetween(0, 10));
    }
    if (randomBoolean()) {
        int items = randomIntBetween(0, 5);
        Map<String, Object> options = new HashMap<>(items);
        for (int i = 0; i < items; i++) {
            Object value = null;
            switch(randomInt(2)) {
                case 0:
                    value = randomAsciiOfLengthBetween(1, 10);
                    break;
                case 1:
                    value = new Integer(randomInt(1000));
                    break;
                case 2:
                    value = new Boolean(randomBoolean());
                    break;
            }
            options.put(randomAsciiOfLengthBetween(1, 10), value);
        }
    }
    if (randomBoolean()) {
        highlightBuilder.requireFieldMatch(randomBoolean());
    }
}
Also used : IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) HashMap(java.util.HashMap) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 3 with IdsQueryBuilder

use of org.elasticsearch.index.query.IdsQueryBuilder 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 4 with IdsQueryBuilder

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

the class HighlighterSearchIT method testHighlightNoMatchSizeWithMultivaluedFields.

public void testHighlightNoMatchSizeWithMultivaluedFields() throws IOException {
    assertAcked(prepareCreate("test").addMapping("type1", "text", "type=text," + randomStoreField() + "term_vector=with_positions_offsets,index_options=offsets"));
    ensureGreen();
    String text1 = "I am pretty long so some of me should get cut off. We'll see how that goes.";
    String text2 = "I am short";
    index("test", "type1", "1", "text", new String[] { text1, text2 });
    refresh();
    // The no match fragment should come from the first value of a multi-valued field
    HighlightBuilder.Field field = new HighlightBuilder.Field("text").fragmentSize(21).numOfFragments(1).highlighterType("plain").noMatchSize(21);
    SearchResponse response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
    assertHighlight(response, 0, "text", 0, 1, equalTo("I am pretty long so"));
    field.highlighterType("fvh");
    response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
    assertHighlight(response, 0, "text", 0, 1, equalTo("I am pretty long so some"));
    field.highlighterType("unified");
    response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
    assertHighlight(response, 0, "text", 0, 1, equalTo("I am pretty long so some"));
    // 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("I am pretty long so some of me should get cut off."));
    // And noMatchSize returns nothing when the first entry is empty string!
    index("test", "type1", "2", "text", new String[] { "", text2 });
    refresh();
    IdsQueryBuilder idsQueryBuilder = QueryBuilders.idsQuery("type1").addIds("2");
    field.highlighterType("plain");
    response = client().prepareSearch("test").setQuery(idsQueryBuilder).highlighter(new HighlightBuilder().field(field)).get();
    assertNotHighlighted(response, 0, "text");
    field.highlighterType("fvh");
    response = client().prepareSearch("test").setQuery(idsQueryBuilder).highlighter(new HighlightBuilder().field(field)).get();
    assertNotHighlighted(response, 0, "text");
    field.highlighterType("postings");
    response = client().prepareSearch("test").setQuery(idsQueryBuilder).highlighter(new HighlightBuilder().field(field)).get();
    assertNotHighlighted(response, 0, "text");
    // except for the unified highlighter which starts from the first string with actual content
    field.highlighterType("unified");
    response = client().prepareSearch("test").setQuery(idsQueryBuilder).highlighter(new HighlightBuilder().field(field)).get();
    assertHighlight(response, 0, "text", 0, 1, equalTo("I am short"));
    // But if the field was actually empty then you should get no highlighting field
    index("test", "type1", "3", "text", new String[] {});
    refresh();
    idsQueryBuilder = QueryBuilders.idsQuery("type1").addIds("3");
    field.highlighterType("plain");
    response = client().prepareSearch("test").setQuery(idsQueryBuilder).highlighter(new HighlightBuilder().field(field)).get();
    assertNotHighlighted(response, 0, "text");
    field.highlighterType("fvh");
    response = client().prepareSearch("test").setQuery(idsQueryBuilder).highlighter(new HighlightBuilder().field(field)).get();
    assertNotHighlighted(response, 0, "text");
    field.highlighterType("postings");
    response = client().prepareSearch("test").setQuery(idsQueryBuilder).highlighter(new HighlightBuilder().field(field)).get();
    assertNotHighlighted(response, 0, "text");
    field.highlighterType("unified");
    response = client().prepareSearch("test").setQuery(idsQueryBuilder).highlighter(new HighlightBuilder().field(field)).get();
    assertNotHighlighted(response, 0, "text");
    // Same for if the field doesn't even exist on the document
    index("test", "type1", "4");
    refresh();
    idsQueryBuilder = QueryBuilders.idsQuery("type1").addIds("4");
    field.highlighterType("plain");
    response = client().prepareSearch("test").setQuery(idsQueryBuilder).highlighter(new HighlightBuilder().field(field)).get();
    assertNotHighlighted(response, 0, "text");
    field.highlighterType("fvh");
    response = client().prepareSearch("test").setQuery(idsQueryBuilder).highlighter(new HighlightBuilder().field(field)).get();
    assertNotHighlighted(response, 0, "text");
    field.highlighterType("unified");
    response = client().prepareSearch("test").setQuery(idsQueryBuilder).highlighter(new HighlightBuilder().field(field)).get();
    assertNotHighlighted(response, 0, "postings");
    // Again same if the field isn't mapped
    field = new HighlightBuilder.Field("unmapped").highlighterType("plain").noMatchSize(21);
    response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
    assertNotHighlighted(response, 0, "text");
    field.highlighterType("fvh");
    response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
    assertNotHighlighted(response, 0, "text");
    field.highlighterType("postings");
    response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
    assertNotHighlighted(response, 0, "text");
    field.highlighterType("unified");
    response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
    assertNotHighlighted(response, 0, "text");
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with IdsQueryBuilder

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

the class ChildQuerySearchIT method testHasParentInnerQueryType.

public void testHasParentInnerQueryType() {
    assertAcked(prepareCreate("test").addMapping("parent-type").addMapping("child-type", "_parent", "type=parent-type"));
    client().prepareIndex("test", "child-type", "child-id").setParent("parent-id").setSource("{}", XContentType.JSON).get();
    client().prepareIndex("test", "parent-type", "parent-id").setSource("{}", XContentType.JSON).get();
    refresh();
    //make sure that when we explicitly set a type, the inner query is executed in the context of the parent type instead
    SearchResponse searchResponse = client().prepareSearch("test").setTypes("child-type").setQuery(QueryBuilders.hasParentQuery("parent-type", new IdsQueryBuilder().addIds("parent-id"), false)).get();
    assertSearchHits(searchResponse, "child-id");
}
Also used : IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

IdsQueryBuilder (org.elasticsearch.index.query.IdsQueryBuilder)10 SearchResponse (org.elasticsearch.action.search.SearchResponse)7 HashMap (java.util.HashMap)4 SearchHit (org.elasticsearch.search.SearchHit)4 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)3 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)3 SearchHits (org.elasticsearch.search.SearchHits)3 SearchResult (fi.otavanopisto.muikku.search.SearchResult)2 UnknownHostException (java.net.UnknownHostException)2 HashSet (java.util.HashSet)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)2 Splitter (com.google.common.base.Splitter)1 Iterables (com.google.common.collect.Iterables)1 EnvironmentRoleArchetype (fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype)1 WorkspaceAccess (fi.otavanopisto.muikku.model.workspace.WorkspaceAccess)1 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)1