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

**URL:** <https://discuss.elastic.co/t/match-query-with-operator-and-via-the-java-api/67863>\
**Category:** Elasticsearch\
**Created:** [December 2, 2016, 1:54pm UTC](https://discuss.elastic.co/t/match-query-with-operator-and-via-the-java-api/67863 "2016-12-02T13:54:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenamon](https://avatars.discourse-cdn.com/v4/letter/j/bbce88/32.png) [@jenamon](https://discuss.elastic.co/u/jenamon)\
**Post date:** [December 2, 2016, 1:54pm UTC](https://discuss.elastic.co/t/match-query-with-operator-and-via-the-java-api/67863/1 "2016-12-02T13:54:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [December 2, 2016, 2:30pm UTC](https://discuss.elastic.co/t/match-query-with-operator-and-via-the-java-api/67863/2 "2016-12-02T14:30:27Z")

</div>

> [@jenamon](#):
>
> What am I missing?

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.

---

<div class="post-metadata">

**Author:** ![jenamon](https://avatars.discourse-cdn.com/v4/letter/j/bbce88/32.png) [@jenamon](https://discuss.elastic.co/u/jenamon)\
**Post date:** [December 2, 2016, 2:31pm UTC](https://discuss.elastic.co/t/match-query-with-operator-and-via-the-java-api/67863/3 "2016-12-02T14:31:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jenamon](https://avatars.discourse-cdn.com/v4/letter/j/bbce88/32.png) [@jenamon](https://discuss.elastic.co/u/jenamon)\
**Post date:** [December 2, 2016, 9:01pm UTC](https://discuss.elastic.co/t/match-query-with-operator-and-via-the-java-api/67863/4 "2016-12-02T21:01:09Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 30, 2016, 9:01pm UTC](https://discuss.elastic.co/t/match-query-with-operator-and-via-the-java-api/67863/5 "2016-12-30T21:01:24Z")

</div>

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