Add support for the `_name` parameter to the simple_query_string query

This commit is contained in:
Lee Hinman 2014-07-23 15:01:02 +02:00
parent eaac8141cc
commit 5aa2d0cf61
3 changed files with 23 additions and 2 deletions

View File

@ -35,6 +35,7 @@ public class SimpleQueryStringBuilder extends BaseQueryBuilder {
private String analyzer;
private Operator operator;
private final String queryText;
private String queryName;
private int flags = -1;
private Boolean lowercaseExpandedTerms;
private Boolean lenient;
@ -71,6 +72,14 @@ public class SimpleQueryStringBuilder extends BaseQueryBuilder {
return this;
}
/**
* Specify a name for the query
*/
public SimpleQueryStringBuilder queryName(String name) {
this.queryName = name;
return this;
}
/**
* Specify an analyzer to use for the query
*/
@ -163,6 +172,10 @@ public class SimpleQueryStringBuilder extends BaseQueryBuilder {
builder.field("locale", locale.toString());
}
if (queryName != null) {
builder.field("_name", queryName);
}
builder.endObject();
}
}

View File

@ -87,6 +87,7 @@ public class SimpleQueryStringParser implements QueryParser {
String currentFieldName = null;
String queryBody = null;
String queryName = null;
String field = null;
Map<String, Float> fieldsAndWeights = null;
BooleanClause.Occur defaultOperator = null;
@ -177,6 +178,8 @@ public class SimpleQueryStringParser implements QueryParser {
sqsSettings.lowercaseExpandedTerms(parser.booleanValue());
} else if ("lenient".equals(currentFieldName)) {
sqsSettings.lenient(parser.booleanValue());
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else {
throw new QueryParsingException(parseContext.index(), "[" + NAME + "] unsupported field [" + parser.currentName() + "]");
}
@ -212,6 +215,10 @@ public class SimpleQueryStringParser implements QueryParser {
sqp.setDefaultOperator(defaultOperator);
}
return sqp.parse(queryBody);
Query query = sqp.parse(queryBody);
if (queryName != null) {
parseContext.addNamedQuery(queryName, query);
}
return query;
}
}

View File

@ -2085,10 +2085,11 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
assertFirstHit(searchResponse, hasId("4"));
searchResponse = client().prepareSearch().setQuery(
simpleQueryString("spaghetti").field("body", 10.0f).field("otherbody", 2.0f)).get();
simpleQueryString("spaghetti").field("body", 10.0f).field("otherbody", 2.0f).queryName("myquery")).get();
assertHitCount(searchResponse, 2l);
assertFirstHit(searchResponse, hasId("5"));
assertSearchHits(searchResponse, "5", "6");
assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("myquery"));
}
@Test