Search in sources :

Example 6 with TermsLookup

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

the class TermsQueryBuilder method fromXContent.

public static TermsQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    List<Object> values = null;
    TermsLookup termsLookup = null;
    String queryName = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
        // skip
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (fieldName != null) {
                throw new ParsingException(parser.getTokenLocation(), "[" + TermsQueryBuilder.NAME + "] query does not support multiple fields");
            }
            fieldName = currentFieldName;
            values = parseValues(parser);
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (fieldName != null) {
                throw new ParsingException(parser.getTokenLocation(), "[" + TermsQueryBuilder.NAME + "] query does not support more than one field. " + "Already got: [" + fieldName + "] but also found [" + currentFieldName + "]");
            }
            fieldName = currentFieldName;
            termsLookup = TermsLookup.parseTermsLookup(parser);
        } else if (token.isValue()) {
            if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + TermsQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[" + TermsQueryBuilder.NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
        }
    }
    if (fieldName == null) {
        throw new ParsingException(parser.getTokenLocation(), "[" + TermsQueryBuilder.NAME + "] query requires a field name, " + "followed by array of terms or a document lookup specification");
    }
    return new TermsQueryBuilder(fieldName, values, termsLookup).boost(boost).queryName(queryName);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) TermsLookup(org.elasticsearch.indices.TermsLookup) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 7 with TermsLookup

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

the class TermsQueryBuilder method doRewrite.

@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
    if (this.termsLookup != null) {
        TermsLookup termsLookup = new TermsLookup(this.termsLookup);
        if (termsLookup.index() == null) {
            // TODO this should go away?
            if (queryRewriteContext.getIndexSettings() != null) {
                termsLookup.index(queryRewriteContext.getIndexSettings().getIndex().getName());
            } else {
                // can't rewrite until we have index scope on the shard
                return this;
            }
        }
        List<Object> values = fetch(termsLookup, queryRewriteContext.getClient());
        return new TermsQueryBuilder(this.fieldName, values);
    }
    return this;
}
Also used : TermsLookup(org.elasticsearch.indices.TermsLookup)

Example 8 with TermsLookup

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

the class PercolatorFieldMapperTests method testQueryWithRewrite.

public void testQueryWithRewrite() throws Exception {
    addQueryMapping();
    client().prepareIndex("remote", "type", "1").setSource("field", "value").get();
    QueryBuilder queryBuilder = termsLookupQuery("field", new TermsLookup("remote", "type", "1", "field"));
    ParsedDocument doc = mapperService.documentMapper(typeName).parse("test", typeName, "1", XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject().bytes());
    BytesRef qbSource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
    assertQueryBuilder(qbSource, queryBuilder.rewrite(indexService.newQueryShardContext(randomInt(20), null, () -> {
        throw new UnsupportedOperationException();
    })));
}
Also used : ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) TermsLookup(org.elasticsearch.indices.TermsLookup) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ConstantScoreQueryBuilder(org.elasticsearch.index.query.ConstantScoreQueryBuilder) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoostingQueryBuilder(org.elasticsearch.index.query.BoostingQueryBuilder) HasChildQueryBuilder(org.elasticsearch.index.query.HasChildQueryBuilder) BytesRef(org.apache.lucene.util.BytesRef)

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