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);
}
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();
}
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());
}
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);
}
}
}
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"));
}
}
Aggregations