Search in sources :

Example 1 with TermsLookup

use of org.elasticsearch.indices.TermsLookup in project elasticsearch by elastic.

the class SearchQueryIT method testTermsLookupFilter.

public void testTermsLookupFilter() throws Exception {
    assertAcked(prepareCreate("lookup").addMapping("type", "terms", "type=text", "other", "type=text"));
    assertAcked(prepareCreate("lookup2").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("arr").startObject("properties").startObject("term").field("type", "text").endObject().endObject().endObject().endObject().endObject().endObject()));
    assertAcked(prepareCreate("lookup3").addMapping("type", "_source", "enabled=false", "terms", "type=text"));
    assertAcked(prepareCreate("test").addMapping("type", "term", "type=text"));
    indexRandom(true, client().prepareIndex("lookup", "type", "1").setSource("terms", new String[] { "1", "3" }), client().prepareIndex("lookup", "type", "2").setSource("terms", new String[] { "2" }), client().prepareIndex("lookup", "type", "3").setSource("terms", new String[] { "2", "4" }), client().prepareIndex("lookup", "type", "4").setSource("other", "value"), client().prepareIndex("lookup2", "type", "1").setSource(XContentFactory.jsonBuilder().startObject().startArray("arr").startObject().field("term", "1").endObject().startObject().field("term", "3").endObject().endArray().endObject()), client().prepareIndex("lookup2", "type", "2").setSource(XContentFactory.jsonBuilder().startObject().startArray("arr").startObject().field("term", "2").endObject().endArray().endObject()), client().prepareIndex("lookup2", "type", "3").setSource(XContentFactory.jsonBuilder().startObject().startArray("arr").startObject().field("term", "2").endObject().startObject().field("term", "4").endObject().endArray().endObject()), client().prepareIndex("lookup3", "type", "1").setSource("terms", new String[] { "1", "3" }), client().prepareIndex("test", "type", "1").setSource("term", "1"), client().prepareIndex("test", "type", "2").setSource("term", "2"), client().prepareIndex("test", "type", "3").setSource("term", "3"), client().prepareIndex("test", "type", "4").setSource("term", "4"));
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(termsLookupQuery("term", new TermsLookup("lookup", "type", "1", "terms"))).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "3");
    // same as above, just on the _id...
    searchResponse = client().prepareSearch("test").setQuery(termsLookupQuery("_id", new TermsLookup("lookup", "type", "1", "terms"))).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "3");
    // another search with same parameters...
    searchResponse = client().prepareSearch("test").setQuery(termsLookupQuery("term", new TermsLookup("lookup", "type", "1", "terms"))).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "3");
    searchResponse = client().prepareSearch("test").setQuery(termsLookupQuery("term", new TermsLookup("lookup", "type", "2", "terms"))).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("2"));
    searchResponse = client().prepareSearch("test").setQuery(termsLookupQuery("term", new TermsLookup("lookup", "type", "3", "terms"))).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "2", "4");
    searchResponse = client().prepareSearch("test").setQuery(termsLookupQuery("term", new TermsLookup("lookup", "type", "4", "terms"))).get();
    assertHitCount(searchResponse, 0L);
    searchResponse = client().prepareSearch("test").setQuery(termsLookupQuery("term", new TermsLookup("lookup2", "type", "1", "arr.term"))).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "1", "3");
    searchResponse = client().prepareSearch("test").setQuery(termsLookupQuery("term", new TermsLookup("lookup2", "type", "2", "arr.term"))).get();
    assertHitCount(searchResponse, 1L);
    assertFirstHit(searchResponse, hasId("2"));
    searchResponse = client().prepareSearch("test").setQuery(termsLookupQuery("term", new TermsLookup("lookup2", "type", "3", "arr.term"))).get();
    assertHitCount(searchResponse, 2L);
    assertSearchHits(searchResponse, "2", "4");
    searchResponse = client().prepareSearch("test").setQuery(termsLookupQuery("not_exists", new TermsLookup("lookup2", "type", "3", "arr.term"))).get();
    assertHitCount(searchResponse, 0L);
    // index "lookup" type "type" id "missing" document does not exist: ignore the lookup terms
    searchResponse = client().prepareSearch("test").setQuery(termsLookupQuery("term", new TermsLookup("lookup", "type", "missing", "terms"))).get();
    assertHitCount(searchResponse, 0L);
    // index "lookup3" type "type" has the source disabled: ignore the lookup terms
    searchResponse = client().prepareSearch("test").setQuery(termsLookupQuery("term", new TermsLookup("lookup3", "type", "1", "terms"))).get();
    assertHitCount(searchResponse, 0L);
}
Also used : TermsLookup(org.elasticsearch.indices.TermsLookup) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with TermsLookup

use of org.elasticsearch.indices.TermsLookup in project elasticsearch by elastic.

the class ContextAndHeaderTransportIT method testThatTermsLookupGetRequestContainsContextAndHeaders.

public void testThatTermsLookupGetRequestContainsContextAndHeaders() throws Exception {
    transportClient().prepareIndex(lookupIndex, "type", "1").setSource(jsonBuilder().startObject().array("followers", "foo", "bar", "baz").endObject()).get();
    transportClient().prepareIndex(queryIndex, "type", "1").setSource(jsonBuilder().startObject().field("username", "foo").endObject()).get();
    transportClient().admin().indices().prepareRefresh(queryIndex, lookupIndex).get();
    TermsLookup termsLookup = new TermsLookup(lookupIndex, "type", "1", "followers");
    TermsQueryBuilder termsLookupFilterBuilder = QueryBuilders.termsLookupQuery("username", termsLookup);
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).must(termsLookupFilterBuilder);
    SearchResponse searchResponse = transportClient().prepareSearch(queryIndex).setQuery(queryBuilder).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 1);
    assertGetRequestsContainHeaders();
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) TermsLookup(org.elasticsearch.indices.TermsLookup) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with TermsLookup

use of org.elasticsearch.indices.TermsLookup in project elasticsearch by elastic.

the class TermsLookupTests method testTermsLookup.

public void testTermsLookup() {
    String index = randomAsciiOfLengthBetween(1, 10);
    String type = randomAsciiOfLengthBetween(1, 10);
    String id = randomAsciiOfLengthBetween(1, 10);
    String path = randomAsciiOfLengthBetween(1, 10);
    String routing = randomAsciiOfLengthBetween(1, 10);
    TermsLookup termsLookup = new TermsLookup(index, type, id, path);
    termsLookup.routing(routing);
    assertEquals(index, termsLookup.index());
    assertEquals(type, termsLookup.type());
    assertEquals(id, termsLookup.id());
    assertEquals(path, termsLookup.path());
    assertEquals(routing, termsLookup.routing());
}
Also used : TermsLookup(org.elasticsearch.indices.TermsLookup) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 4 with TermsLookup

use of org.elasticsearch.indices.TermsLookup in project elasticsearch by elastic.

the class TermsLookupTests method testSerialization.

public void testSerialization() throws IOException {
    TermsLookup termsLookup = randomTermsLookup();
    try (BytesStreamOutput output = new BytesStreamOutput()) {
        termsLookup.writeTo(output);
        try (StreamInput in = output.bytes().streamInput()) {
            TermsLookup deserializedLookup = new TermsLookup(in);
            assertEquals(deserializedLookup, termsLookup);
            assertEquals(deserializedLookup.hashCode(), termsLookup.hashCode());
            assertNotSame(deserializedLookup, termsLookup);
        }
    }
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) TermsLookup(org.elasticsearch.indices.TermsLookup) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 5 with TermsLookup

use of org.elasticsearch.indices.TermsLookup in project elasticsearch by elastic.

the class TermsLookupTests method testIllegalArguments.

public void testIllegalArguments() {
    String type = randomAsciiOfLength(5);
    String id = randomAsciiOfLength(5);
    String path = randomAsciiOfLength(5);
    switch(randomIntBetween(0, 2)) {
        case 0:
            type = null;
            break;
        case 1:
            id = null;
            break;
        case 2:
            path = null;
            break;
    }
    try {
        new TermsLookup(null, type, id, path);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("[terms] query lookup element requires specifying"));
    }
}
Also used : TermsLookup(org.elasticsearch.indices.TermsLookup) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

TermsLookup (org.elasticsearch.indices.TermsLookup)8 Matchers.containsString (org.hamcrest.Matchers.containsString)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)2 BytesRef (org.apache.lucene.util.BytesRef)1 ParsingException (org.elasticsearch.common.ParsingException)1 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)1 StreamInput (org.elasticsearch.common.io.stream.StreamInput)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)1 BoostingQueryBuilder (org.elasticsearch.index.query.BoostingQueryBuilder)1 ConstantScoreQueryBuilder (org.elasticsearch.index.query.ConstantScoreQueryBuilder)1 HasChildQueryBuilder (org.elasticsearch.index.query.HasChildQueryBuilder)1 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)1 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)1 TermsQueryBuilder (org.elasticsearch.index.query.TermsQueryBuilder)1 FunctionScoreQueryBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder)1 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)1