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:
parent
cdd13253de
commit
0a526be344
|
@ -105,34 +105,30 @@ 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.startObject(name);
|
||||||
builder.field(name, value);
|
builder.field("value", value);
|
||||||
} else {
|
if (boost != -1) {
|
||||||
builder.startObject(name);
|
builder.field("boost", boost);
|
||||||
builder.field("value", value);
|
|
||||||
if (boost != -1) {
|
|
||||||
builder.field("boost", boost);
|
|
||||||
}
|
|
||||||
if (transpositions != null) {
|
|
||||||
builder.field("transpositions", transpositions);
|
|
||||||
}
|
|
||||||
if (fuzziness != null) {
|
|
||||||
fuzziness.toXContent(builder, params);
|
|
||||||
}
|
|
||||||
if (prefixLength != null) {
|
|
||||||
builder.field("prefix_length", prefixLength);
|
|
||||||
}
|
|
||||||
if (maxExpansions != null) {
|
|
||||||
builder.field("max_expansions", maxExpansions);
|
|
||||||
}
|
|
||||||
if (rewrite != null) {
|
|
||||||
builder.field("rewrite", rewrite);
|
|
||||||
}
|
|
||||||
if (queryName != null) {
|
|
||||||
builder.field("_name", queryName);
|
|
||||||
}
|
|
||||||
builder.endObject();
|
|
||||||
}
|
}
|
||||||
|
if (transpositions != null) {
|
||||||
|
builder.field("transpositions", transpositions);
|
||||||
|
}
|
||||||
|
if (fuzziness != null) {
|
||||||
|
fuzziness.toXContent(builder, params);
|
||||||
|
}
|
||||||
|
if (prefixLength != null) {
|
||||||
|
builder.field("prefix_length", prefixLength);
|
||||||
|
}
|
||||||
|
if (maxExpansions != null) {
|
||||||
|
builder.field("max_expansions", maxExpansions);
|
||||||
|
}
|
||||||
|
if (rewrite != null) {
|
||||||
|
builder.field("rewrite", rewrite);
|
||||||
|
}
|
||||||
|
if (queryName != null) {
|
||||||
|
builder.field("_name", queryName);
|
||||||
|
}
|
||||||
|
builder.endObject();
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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);
|
||||||
|
|
|
@ -98,28 +98,24 @@ 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.startObject(name);
|
||||||
builder.field(name, regexp);
|
builder.field("value", regexp);
|
||||||
} else {
|
if (flags != -1) {
|
||||||
builder.startObject(name);
|
builder.field("flags_value", flags);
|
||||||
builder.field("value", regexp);
|
|
||||||
if (flags != -1) {
|
|
||||||
builder.field("flags_value", flags);
|
|
||||||
}
|
|
||||||
if (maxDetermizedStatesSet) {
|
|
||||||
builder.field("max_determinized_states", maxDeterminizedStates);
|
|
||||||
}
|
|
||||||
if (boost != -1) {
|
|
||||||
builder.field("boost", boost);
|
|
||||||
}
|
|
||||||
if (rewrite != null) {
|
|
||||||
builder.field("rewrite", rewrite);
|
|
||||||
}
|
|
||||||
if (queryName != null) {
|
|
||||||
builder.field("name", queryName);
|
|
||||||
}
|
|
||||||
builder.endObject();
|
|
||||||
}
|
}
|
||||||
|
if (maxDetermizedStatesSet) {
|
||||||
|
builder.field("max_determinized_states", maxDeterminizedStates);
|
||||||
|
}
|
||||||
|
if (boost != -1) {
|
||||||
|
builder.field("boost", boost);
|
||||||
|
}
|
||||||
|
if (rewrite != null) {
|
||||||
|
builder.field("rewrite", rewrite);
|
||||||
|
}
|
||||||
|
if (queryName != null) {
|
||||||
|
builder.field("_name", queryName);
|
||||||
|
}
|
||||||
|
builder.endObject();
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 + "]");
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue