Match query with "operator":"and" via the Java API

This is in the Elasticsearch documentation. I want to do this via the java API.

GET /_search
{
    "query": {
        "match" : {
            "message" : {
                "query" : "this is a test",
                "operator" : "and"
            }
        }
    }
}

I found some things online, like the use of .defaultOperator on a query_string query.

// Build RuleBased contextual query
BoolQueryBuilder qb = QueryBuilders.boolQuery();
String queryString = query.getQueryText();

// Human readable type of querying
qb.should(QueryBuilders.simpleQueryStringQuery(query.getQueryText())
  .field(RuleNormalizer.RuleField.NAME.field() + "." + IndexField.SEARCH_WORDS_SUFFIX, 20f)
  .field(RuleNormalizer.RuleField.HTML_DESCRIPTION.field() + "." + IndexField.SEARCH_WORDS_SUFFIX, 3f)
  .defaultOperator(SimpleQueryStringBuilder.Operator.AND)
  ).boost(20f);

I expected to see something similar with .operator(Operator.AND) in a match query.

I found this, with MatchQueryBuilder and MultiMatchQueryBuilder

public QueryBuilder build(final String queryString) {
	BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
	String queryParam = queryString;
	if (!queryString.startsWith("http")
			&& !queryString.matches("[\\d\\-,]+")) {
		// multi-val queries only for URIs or GND IDs:
		queryParam = queryString.replace(',', ' ');
	}
	for (String q : queryParam.split(",")) {
		final MultiMatchQueryBuilder subjectLabelQuery =
				multiMatchQuery(q, fields().get(0), fields().get(1))
						.operator(Operator.AND);
		final String query =
				q.startsWith("http://") ? q : "http://d-nb.info/gnd/" + q;
		final MatchQueryBuilder subjectIdQuery =
				matchQuery(fields().get(2) + ".@id", query).operator(Operator.AND);
		boolQuery = boolQuery.should(
				boolQuery().should(subjectLabelQuery).should(subjectIdQuery));
	}
	return boolQuery;
}

I tried this:

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.DisMaxQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.Operator;

disMaxQueryBuilder.add(QueryBuilders.matchQuery(field, term).operator(Operator.AND));

.
The import of org.elasticsearch.index.query.Operator fails, unable to find the class Operator.

I'm using elasticsearch-2.1.1.jar.

What am I missing? Thanks in advance!!

Maybe I actually need this

import org.elasticsearch.index.query.MatchQueryBuilder; 
import org.elasticsearch.index.query.MatchQueryBuilder.Operator; 

I can't find exactly what I expect to find.
Note: fixed version typo above: using 2.1.1

You are two major versions behind what is released so this is all fairly historic to me. It looks like operator is inside of MatchQueryBuilder in that version. So something like import org.elasticsearch.index.queryMatchQueryBuilder.Operator is the way to go I think.

Typo. I'm on 2.1.1
I'm making progress with MatchQueryBuilder.Operator. Thx. I'll update with the final solution.

The following works. Only the MatchQueryBuilder had the .operator method.

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.DisMaxQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.MatchQueryBuilder;

    for (String s : fieldSets) {
      MatchQueryBuilder mqbs = QueryBuilders.matchQuery(s, term).operator(MatchQueryBuilder.Operator.AND); 
      disMaxQueryBuilder.add(mqbs);
      qb.filter(disMaxQueryBuilder);      
    }

Thanks! Problem Solved!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.