query on _all field with term/prefix queries fail (by resulting on query execution exception) since the correct term query is not used for it

This commit is contained in:
kimchy 2010-04-25 18:43:53 +03:00
parent bf6cead984
commit 4ab298ce00
7 changed files with 34 additions and 14 deletions

View File

@ -162,15 +162,15 @@ public interface FieldMapper<T> {
String indexedValue(T value); String indexedValue(T value);
/** /**
* Should the field query {@link #fieldQuery(String)} be used when detecting this * Should the field query {@link #termQuery(String)} be used when detecting this
* field in query string. * field in query string.
*/ */
boolean useFieldQueryWithQueryString(); boolean useTermQueryWithQueryString();
/** /**
* A field query for the specified value. * A field query for the specified value.
*/ */
Query fieldQuery(String value); Query termQuery(String value);
/** /**
* A term query to use when parsing a query string. Can return <tt>null</tt>. * A term query to use when parsing a query string. Can return <tt>null</tt>.

View File

@ -110,6 +110,10 @@ public class JsonAllFieldMapper extends JsonFieldMapper<Void> implements AllFiel
return new AllTermQuery(term); return new AllTermQuery(term);
} }
@Override public Query termQuery(String value) {
return new AllTermQuery(new Term(names.indexName(), value));
}
@Override protected Field parseCreateField(JsonParseContext jsonContext) throws IOException { @Override protected Field parseCreateField(JsonParseContext jsonContext) throws IOException {
if (!enabled) { if (!enabled) {
return null; return null;
@ -152,10 +156,6 @@ public class JsonAllFieldMapper extends JsonFieldMapper<Void> implements AllFiel
return null; return null;
} }
@Override public String indexedValue(String value) {
return null;
}
@Override public String indexedValue(Void value) { @Override public String indexedValue(Void value) {
return null; return null;
} }

View File

@ -319,11 +319,11 @@ public abstract class JsonFieldMapper<T> implements FieldMapper<T>, JsonMapper {
return value.toString(); return value.toString();
} }
@Override public boolean useFieldQueryWithQueryString() { @Override public boolean useTermQueryWithQueryString() {
return false; return false;
} }
@Override public Query fieldQuery(String value) { @Override public Query termQuery(String value) {
return new TermQuery(new Term(names.indexName(), indexedValue(value))); return new TermQuery(new Term(names.indexName(), indexedValue(value)));
} }

View File

@ -115,7 +115,7 @@ public abstract class JsonNumberFieldMapper<T extends Number> extends JsonFieldM
/** /**
* Use the field query created here when matching on numbers. * Use the field query created here when matching on numbers.
*/ */
@Override public boolean useFieldQueryWithQueryString() { @Override public boolean useTermQueryWithQueryString() {
return true; return true;
} }

View File

@ -93,7 +93,7 @@ public class TermJsonQueryParser extends AbstractIndexComponent implements JsonQ
MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName); MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName);
if (smartNameFieldMappers != null) { if (smartNameFieldMappers != null) {
if (smartNameFieldMappers.hasMapper()) { if (smartNameFieldMappers.hasMapper()) {
query = smartNameFieldMappers.mapper().fieldQuery(value); query = smartNameFieldMappers.mapper().termQuery(value);
} }
} }
if (query == null) { if (query == null) {

View File

@ -82,8 +82,8 @@ public class MapperQueryParser extends QueryParser {
currentMapper = fieldMappers.fieldMappers().mapper(); currentMapper = fieldMappers.fieldMappers().mapper();
if (currentMapper != null) { if (currentMapper != null) {
Query query; Query query;
if (currentMapper.useFieldQueryWithQueryString()) { if (currentMapper.useTermQueryWithQueryString()) {
query = currentMapper.fieldQuery(queryText); query = currentMapper.termQuery(queryText);
} else { } else {
query = super.getFieldQuery(currentMapper.names().indexName(), queryText); query = super.getFieldQuery(currentMapper.names().indexName(), queryText);
} }

View File

@ -76,7 +76,7 @@ public class HighlightSearchTests extends AbstractNodesTests {
@Test public void testSimpleHighlighting() throws Exception { @Test public void testSimpleHighlighting() throws Exception {
SearchSourceBuilder source = searchSource() SearchSourceBuilder source = searchSource()
.query(termQuery("multi", "test")) .query(termQuery("_all", "test"))
.from(0).size(60).explain(true) .from(0).size(60).explain(true)
.highlight(highlight().field("_all").order("score").preTags("<xxx>").postTags("</xxx>")); .highlight(highlight().field("_all").order("score").preTags("<xxx>").postTags("</xxx>"));
@ -123,6 +123,26 @@ public class HighlightSearchTests extends AbstractNodesTests {
} }
} }
@Test public void testPrefixHighlightingOnAllField() throws Exception {
SearchSourceBuilder source = searchSource()
.query(prefixQuery("_all", "te"))
.from(0).size(60).explain(true)
.highlight(highlight().field("_all").order("score").preTags("<xxx>").postTags("</xxx>"));
SearchResponse searchResponse = client.search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH).scroll(timeValueMinutes(10))).actionGet();
assertThat("Failures " + Arrays.toString(searchResponse.shardFailures()), searchResponse.shardFailures().length, equalTo(0));
assertThat(searchResponse.hits().totalHits(), equalTo(100l));
assertThat(searchResponse.hits().hits().length, equalTo(60));
for (int i = 0; i < 60; i++) {
SearchHit hit = searchResponse.hits().hits()[i];
// System.out.println(hit.target() + ": " + hit.explanation());
// assertThat("id[" + hit.id() + "]", hit.id(), equalTo(Integer.toString(100 - i - 1)));
// System.out.println(hit.shard() + ": " + hit.highlightFields());
assertThat(hit.highlightFields().size(), equalTo(1));
assertThat(hit.highlightFields().get("_all").fragments().length, greaterThan(0));
}
}
private void index(Client client, String id, String nameValue, int age) throws IOException { private void index(Client client, String id, String nameValue, int age) throws IOException {
client.index(Requests.indexRequest("test").type("type1").id(id).source(source(id, nameValue, age))).actionGet(); client.index(Requests.indexRequest("test").type("type1").id(id).source(source(id, nameValue, age))).actionGet();
} }