Query DSL: fix support for _name in some queries

Some of our Java api builders had wrong logic when it comes to serializing the query in json format, resulting in missing fields like _name. Also, regexp parser was ignoring the _name field.

Closes #11694
This commit is contained in:
javanna 2015-06-16 10:41:57 +02:00 committed by Luca Cavanna
parent cdd13253de
commit 0a526be344
7 changed files with 156 additions and 52 deletions

View File

@ -105,9 +105,6 @@ public class FuzzyQueryBuilder extends MultiTermQueryBuilder implements Boostabl
@Override @Override
public void doXContent(XContentBuilder builder, Params params) throws IOException { public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(FuzzyQueryParser.NAME); builder.startObject(FuzzyQueryParser.NAME);
if (boost == -1 && fuzziness == null && prefixLength == null && queryName != null) {
builder.field(name, value);
} else {
builder.startObject(name); builder.startObject(name);
builder.field("value", value); builder.field("value", value);
if (boost != -1) { if (boost != -1) {
@ -132,7 +129,6 @@ public class FuzzyQueryBuilder extends MultiTermQueryBuilder implements Boostabl
builder.field("_name", queryName); builder.field("_name", queryName);
} }
builder.endObject(); builder.endObject();
}
builder.endObject(); builder.endObject();
} }
} }

View File

@ -75,7 +75,7 @@ public class PrefixQueryBuilder extends MultiTermQueryBuilder implements Boostab
@Override @Override
public void doXContent(XContentBuilder builder, Params params) throws IOException { public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(PrefixQueryParser.NAME); builder.startObject(PrefixQueryParser.NAME);
if (boost == -1 && rewrite == null && queryName != null) { if (boost == -1 && rewrite == null && queryName == null) {
builder.field(name, prefix); builder.field(name, prefix);
} else { } else {
builder.startObject(name); builder.startObject(name);

View File

@ -98,9 +98,6 @@ public class RegexpQueryBuilder extends MultiTermQueryBuilder implements Boostab
@Override @Override
public void doXContent(XContentBuilder builder, Params params) throws IOException { public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(RegexpQueryParser.NAME); builder.startObject(RegexpQueryParser.NAME);
if (boost == -1 && rewrite == null && queryName != null) {
builder.field(name, regexp);
} else {
builder.startObject(name); builder.startObject(name);
builder.field("value", regexp); builder.field("value", regexp);
if (flags != -1) { if (flags != -1) {
@ -116,10 +113,9 @@ public class RegexpQueryBuilder extends MultiTermQueryBuilder implements Boostab
builder.field("rewrite", rewrite); builder.field("rewrite", rewrite);
} }
if (queryName != null) { if (queryName != null) {
builder.field("name", queryName); builder.field("_name", queryName);
} }
builder.endObject(); builder.endObject();
}
builder.endObject(); builder.endObject();
} }
} }

View File

@ -27,7 +27,6 @@ import org.apache.lucene.util.automaton.Operations;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.support.QueryParsers; import org.elasticsearch.index.query.support.QueryParsers;
@ -87,6 +86,8 @@ public class RegexpQueryParser implements QueryParser {
maxDeterminizedStates = parser.intValue(); maxDeterminizedStates = parser.intValue();
} else if ("flags_value".equals(currentFieldName)) { } else if ("flags_value".equals(currentFieldName)) {
flagsValue = parser.intValue(); flagsValue = parser.intValue();
} else if ("_name".equals(currentFieldName)) {
queryName = parser.text();
} else { } else {
throw new QueryParsingException(parseContext, "[regexp] query does not support [" + currentFieldName + "]"); throw new QueryParsingException(parseContext, "[regexp] query does not support [" + currentFieldName + "]");
} }

View File

@ -62,7 +62,7 @@ public class SpanFirstQueryBuilder extends SpanQueryBuilder implements Boostable
builder.field("boost", boost); builder.field("boost", boost);
} }
if (queryName != null) { if (queryName != null) {
builder.field("name", queryName); builder.field("_name", queryName);
} }
builder.endObject(); builder.endObject();
} }

View File

@ -85,7 +85,7 @@ public class WildcardQueryBuilder extends MultiTermQueryBuilder implements Boost
@Override @Override
public void doXContent(XContentBuilder builder, Params params) throws IOException { public void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(WildcardQueryParser.NAME); builder.startObject(WildcardQueryParser.NAME);
if (boost == -1 && rewrite == null && queryName != null) { if (boost == -1 && rewrite == null && queryName == null) {
builder.field(name, wildcard); builder.field(name, wildcard);
} else { } else {
builder.startObject(name); builder.startObject(name);

View File

@ -21,6 +21,7 @@ package org.elasticsearch.search.matchedqueries;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHit;
import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test; import org.junit.Test;
@ -204,6 +205,116 @@ public class MatchedQueriesTests extends ElasticsearchIntegrationTest {
} }
} }
@Test
public void testRegExpQuerySupportsName() {
createIndex("test1");
ensureGreen();
client().prepareIndex("test1", "type1", "1").setSource("title", "title1").get();
refresh();
SearchResponse searchResponse = client().prepareSearch()
.setQuery(QueryBuilders.regexpQuery("title", "title1").queryName("regex")).get();
assertHitCount(searchResponse, 1l);
for (SearchHit hit : searchResponse.getHits()) {
if (hit.id().equals("1")) {
assertThat(hit.matchedQueries().length, equalTo(1));
assertThat(hit.matchedQueries(), hasItemInArray("regex"));
} else {
fail("Unexpected document returned with id " + hit.id());
}
}
}
@Test
public void testPrefixQuerySupportsName() {
createIndex("test1");
ensureGreen();
client().prepareIndex("test1", "type1", "1").setSource("title", "title1").get();
refresh();
SearchResponse searchResponse = client().prepareSearch()
.setQuery(QueryBuilders.prefixQuery("title", "title").queryName("prefix")).get();
assertHitCount(searchResponse, 1l);
for (SearchHit hit : searchResponse.getHits()) {
if (hit.id().equals("1")) {
assertThat(hit.matchedQueries().length, equalTo(1));
assertThat(hit.matchedQueries(), hasItemInArray("prefix"));
} else {
fail("Unexpected document returned with id " + hit.id());
}
}
}
@Test
public void testFuzzyQuerySupportsName() {
createIndex("test1");
ensureGreen();
client().prepareIndex("test1", "type1", "1").setSource("title", "title1").get();
refresh();
SearchResponse searchResponse = client().prepareSearch()
.setQuery(QueryBuilders.fuzzyQuery("title", "titel1").queryName("fuzzy")).get();
assertHitCount(searchResponse, 1l);
for (SearchHit hit : searchResponse.getHits()) {
if (hit.id().equals("1")) {
assertThat(hit.matchedQueries().length, equalTo(1));
assertThat(hit.matchedQueries(), hasItemInArray("fuzzy"));
} else {
fail("Unexpected document returned with id " + hit.id());
}
}
}
@Test
public void testWildcardQuerySupportsName() {
createIndex("test1");
ensureGreen();
client().prepareIndex("test1", "type1", "1").setSource("title", "title1").get();
refresh();
SearchResponse searchResponse = client().prepareSearch()
.setQuery(QueryBuilders.wildcardQuery("title", "titl*").queryName("wildcard")).get();
assertHitCount(searchResponse, 1l);
for (SearchHit hit : searchResponse.getHits()) {
if (hit.id().equals("1")) {
assertThat(hit.matchedQueries().length, equalTo(1));
assertThat(hit.matchedQueries(), hasItemInArray("wildcard"));
} else {
fail("Unexpected document returned with id " + hit.id());
}
}
}
@Test
public void testSpanFirstQuerySupportsName() {
createIndex("test1");
ensureGreen();
client().prepareIndex("test1", "type1", "1").setSource("title", "title1 title2").get();
refresh();
SearchResponse searchResponse = client().prepareSearch()
.setQuery(QueryBuilders.spanFirstQuery(QueryBuilders.spanTermQuery("title", "title1"), 10).queryName("span")).get();
assertHitCount(searchResponse, 1l);
for (SearchHit hit : searchResponse.getHits()) {
if (hit.id().equals("1")) {
assertThat(hit.matchedQueries().length, equalTo(1));
assertThat(hit.matchedQueries(), hasItemInArray("span"));
} else {
fail("Unexpected document returned with id " + hit.id());
}
}
}
/** /**
* Test case for issue #4361: https://github.com/elasticsearch/elasticsearch/issues/4361 * Test case for issue #4361: https://github.com/elasticsearch/elasticsearch/issues/4361
*/ */