Search in sources :

Example 1 with TermsLookup

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());
}
Also used : ParsedDocument(org.opensearch.index.mapper.ParsedDocument) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) SourceToParse(org.opensearch.index.mapper.SourceToParse) QueryShardContext(org.opensearch.index.query.QueryShardContext) TermsLookup(org.opensearch.indices.TermsLookup) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) HasChildQueryBuilder(org.opensearch.join.query.HasChildQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) ScriptQueryBuilder(org.opensearch.index.query.ScriptQueryBuilder) DisMaxQueryBuilder(org.opensearch.index.query.DisMaxQueryBuilder) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) HasParentQueryBuilder(org.opensearch.join.query.HasParentQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) BytesRef(org.apache.lucene.util.BytesRef)

Example 2 with TermsLookup

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);
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) IndexResponse(org.opensearch.action.index.IndexResponse) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) TermsLookup(org.opensearch.indices.TermsLookup) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 3 with TermsLookup

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);
}
Also used : TermsLookup(org.opensearch.indices.TermsLookup) Matchers.containsString(org.hamcrest.Matchers.containsString) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with TermsLookup

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;
}
Also used : TermsLookup(org.opensearch.indices.TermsLookup)

Example 5 with TermsLookup

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);
    }));
}
Also used : Query(org.apache.lucene.search.Query) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) BytesReference(org.opensearch.common.bytes.BytesReference) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) AbstractList(java.util.AbstractList) StreamOutput(org.opensearch.common.io.stream.StreamOutput) Supplier(java.util.function.Supplier) Strings(org.opensearch.common.Strings) ArrayList(java.util.ArrayList) XContentParser(org.opensearch.common.xcontent.XContentParser) HashSet(java.util.HashSet) ActionListener(org.opensearch.action.ActionListener) ParsingException(org.opensearch.common.ParsingException) StreamInput(org.opensearch.common.io.stream.StreamInput) Client(org.opensearch.client.Client) SetOnce(org.apache.lucene.util.SetOnce) TermsLookup(org.opensearch.indices.TermsLookup) CharBuffer(java.nio.CharBuffer) BytesRef(org.apache.lucene.util.BytesRef) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) GetRequest(org.opensearch.action.get.GetRequest) Set(java.util.Set) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) ConstantFieldType(org.opensearch.index.mapper.ConstantFieldType) Collectors(java.util.stream.Collectors) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Objects(java.util.Objects) List(java.util.List) IndexSettings(org.opensearch.index.IndexSettings) XContentMapValues(org.opensearch.common.xcontent.support.XContentMapValues) Collections(java.util.Collections) GetRequest(org.opensearch.action.get.GetRequest) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TermsLookup (org.opensearch.indices.TermsLookup)7 BytesRef (org.apache.lucene.util.BytesRef)2 ParsingException (org.opensearch.common.ParsingException)2 XContentParser (org.opensearch.common.xcontent.XContentParser)2 IOException (java.io.IOException)1 CharBuffer (java.nio.CharBuffer)1 AbstractList (java.util.AbstractList)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 Query (org.apache.lucene.search.Query)1