use of org.opensearch.indices.TermsLookup in project OpenSearch by opensearch-project.
the class PercolatorFieldMapperTests method testQueryWithRewrite.
public void testQueryWithRewrite() throws Exception {
addQueryFieldMappings();
client().prepareIndex("remote").setId("1").setSource("field", "value").get();
QueryBuilder queryBuilder = termsLookupQuery("field", new TermsLookup("remote", "1", "field"));
ParsedDocument doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()), XContentType.JSON));
BytesRef qbSource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
QueryShardContext shardContext = indexService.newQueryShardContext(randomInt(20), null, () -> {
throw new UnsupportedOperationException();
}, null);
PlainActionFuture<QueryBuilder> future = new PlainActionFuture<>();
Rewriteable.rewriteAndFetch(queryBuilder, shardContext, future);
assertQueryBuilder(qbSource, future.get());
}
use of org.opensearch.indices.TermsLookup in project OpenSearch by opensearch-project.
the class CCSDuelIT method testTermsLookup.
public void testTermsLookup() throws Exception {
assumeMultiClusterSetup();
IndexRequest indexRequest = new IndexRequest("lookup_index");
indexRequest.id("id");
indexRequest.source("tags", new String[] { "java", "sql", "html", "jax-ws" });
indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
assertEquals(201, indexResponse.status().getStatus());
SearchRequest searchRequest = initSearchRequest();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
TermsQueryBuilder termsQueryBuilder = new TermsQueryBuilder("tags", new TermsLookup("lookup_index", "id", "tags"));
sourceBuilder.query(termsQueryBuilder);
searchRequest.source(sourceBuilder);
duelSearch(searchRequest, CCSDuelIT::assertHits);
}
use of org.opensearch.indices.TermsLookup in project OpenSearch by opensearch-project.
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").setId("1").setSource("terms", new String[] { "1", "3" }), client().prepareIndex("lookup").setId("2").setSource("terms", new String[] { "2" }), client().prepareIndex("lookup").setId("3").setSource("terms", new String[] { "2", "4" }), client().prepareIndex("lookup").setId("4").setSource("other", "value"), client().prepareIndex("lookup2").setId("1").setSource(XContentFactory.jsonBuilder().startObject().startArray("arr").startObject().field("term", "1").endObject().startObject().field("term", "3").endObject().endArray().endObject()), client().prepareIndex("lookup2").setId("2").setSource(XContentFactory.jsonBuilder().startObject().startArray("arr").startObject().field("term", "2").endObject().endArray().endObject()), client().prepareIndex("lookup2").setId("3").setSource(XContentFactory.jsonBuilder().startObject().startArray("arr").startObject().field("term", "2").endObject().startObject().field("term", "4").endObject().endArray().endObject()), client().prepareIndex("lookup3").setId("1").setSource("terms", new String[] { "1", "3" }), client().prepareIndex("test").setId("1").setSource("term", "1"), client().prepareIndex("test").setId("2").setSource("term", "2"), client().prepareIndex("test").setId("3").setSource("term", "3"), client().prepareIndex("test").setId("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.opensearch.indices.TermsLookup in project OpenSearch by opensearch-project.
the class TermsQueryBuilderTests method randomTermsLookup.
private TermsLookup randomTermsLookup() {
// Randomly choose between a typeless terms lookup and one with an explicit type to make sure we are
TermsLookup lookup = maybeIncludeType && randomBoolean() ? new TermsLookup(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10), termsPath) : new TermsLookup(randomAlphaOfLength(10), randomAlphaOfLength(10), termsPath);
// testing both cases.
lookup.routing(randomBoolean() ? randomAlphaOfLength(10) : null);
return lookup;
}
use of org.opensearch.indices.TermsLookup in project OpenSearch by opensearch-project.
the class TermsQueryBuilder method fetch.
private void fetch(TermsLookup termsLookup, Client client, ActionListener<List<Object>> actionListener) {
GetRequest getRequest = new GetRequest(termsLookup.index(), termsLookup.id());
getRequest.preference("_local").routing(termsLookup.routing());
client.get(getRequest, ActionListener.delegateFailure(actionListener, (delegatedListener, getResponse) -> {
List<Object> terms = new ArrayList<>();
if (getResponse.isSourceEmpty() == false) {
// extract terms only if the doc source exists
List<Object> extractedValues = XContentMapValues.extractRawValues(termsLookup.path(), getResponse.getSourceAsMap());
terms.addAll(extractedValues);
}
delegatedListener.onResponse(terms);
}));
}
Aggregations