diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java index 601ec7946a4..a2eeedb6c01 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java @@ -162,15 +162,15 @@ public interface FieldMapper { 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. */ - boolean useFieldQueryWithQueryString(); + boolean useTermQueryWithQueryString(); /** * 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 null. diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonAllFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonAllFieldMapper.java index 64f5f332dec..02c2aecd8f7 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonAllFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonAllFieldMapper.java @@ -110,6 +110,10 @@ public class JsonAllFieldMapper extends JsonFieldMapper implements AllFiel 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 { if (!enabled) { return null; @@ -152,10 +156,6 @@ public class JsonAllFieldMapper extends JsonFieldMapper implements AllFiel return null; } - @Override public String indexedValue(String value) { - return null; - } - @Override public String indexedValue(Void value) { return null; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonFieldMapper.java index cc4d4b38647..11fe7db48d6 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonFieldMapper.java @@ -319,11 +319,11 @@ public abstract class JsonFieldMapper implements FieldMapper, JsonMapper { return value.toString(); } - @Override public boolean useFieldQueryWithQueryString() { + @Override public boolean useTermQueryWithQueryString() { return false; } - @Override public Query fieldQuery(String value) { + @Override public Query termQuery(String value) { return new TermQuery(new Term(names.indexName(), indexedValue(value))); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonNumberFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonNumberFieldMapper.java index 41e1db5b9ae..2a700ff0c71 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonNumberFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonNumberFieldMapper.java @@ -115,7 +115,7 @@ public abstract class JsonNumberFieldMapper extends JsonFieldM /** * Use the field query created here when matching on numbers. */ - @Override public boolean useFieldQueryWithQueryString() { + @Override public boolean useTermQueryWithQueryString() { return true; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/TermJsonQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/TermJsonQueryParser.java index 1949b558686..c677284ece4 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/TermJsonQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/TermJsonQueryParser.java @@ -93,7 +93,7 @@ public class TermJsonQueryParser extends AbstractIndexComponent implements JsonQ MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName); if (smartNameFieldMappers != null) { if (smartNameFieldMappers.hasMapper()) { - query = smartNameFieldMappers.mapper().fieldQuery(value); + query = smartNameFieldMappers.mapper().termQuery(value); } } if (query == null) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/support/MapperQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/support/MapperQueryParser.java index 402ea2ed0a5..1f43d652472 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/support/MapperQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/support/MapperQueryParser.java @@ -82,8 +82,8 @@ public class MapperQueryParser extends QueryParser { currentMapper = fieldMappers.fieldMappers().mapper(); if (currentMapper != null) { Query query; - if (currentMapper.useFieldQueryWithQueryString()) { - query = currentMapper.fieldQuery(queryText); + if (currentMapper.useTermQueryWithQueryString()) { + query = currentMapper.termQuery(queryText); } else { query = super.getFieldQuery(currentMapper.names().indexName(), queryText); } diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlightSearchTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlightSearchTests.java index 15b9c6098f5..dce7a16f0da 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlightSearchTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlightSearchTests.java @@ -76,7 +76,7 @@ public class HighlightSearchTests extends AbstractNodesTests { @Test public void testSimpleHighlighting() throws Exception { SearchSourceBuilder source = searchSource() - .query(termQuery("multi", "test")) + .query(termQuery("_all", "test")) .from(0).size(60).explain(true) .highlight(highlight().field("_all").order("score").preTags("").postTags("")); @@ -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("").postTags("")); + + 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 { client.index(Requests.indexRequest("test").type("type1").id(id).source(source(id, nameValue, age))).actionGet(); }