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