|
10 | 10 | package org.elasticsearch.index.query; |
11 | 11 |
|
12 | 12 | import org.apache.lucene.index.Term; |
13 | | -import org.apache.lucene.queries.spans.SpanQuery; |
14 | 13 | import org.apache.lucene.queries.spans.SpanTermQuery; |
15 | 14 | import org.apache.lucene.search.BooleanClause; |
16 | 15 | import org.apache.lucene.search.Query; |
|
19 | 18 | import org.elasticsearch.TransportVersions; |
20 | 19 | import org.elasticsearch.common.ParsingException; |
21 | 20 | import org.elasticsearch.common.io.stream.StreamInput; |
22 | | -import org.elasticsearch.common.lucene.BytesRefs; |
23 | 21 | import org.elasticsearch.index.mapper.MappedFieldType; |
| 22 | +import org.elasticsearch.lucene.queries.SpanMatchNoDocsQuery; |
24 | 23 | import org.elasticsearch.xcontent.ParseField; |
25 | 24 | import org.elasticsearch.xcontent.XContentParser; |
26 | 25 |
|
@@ -76,40 +75,37 @@ public SpanTermQueryBuilder(StreamInput in) throws IOException { |
76 | 75 | } |
77 | 76 |
|
78 | 77 | @Override |
79 | | - protected SpanQuery doToQuery(SearchExecutionContext context) throws IOException { |
| 78 | + protected Query doToQuery(SearchExecutionContext context) throws IOException { |
80 | 79 | MappedFieldType mapper = context.getFieldType(fieldName); |
81 | | - Term term; |
82 | 80 | if (mapper == null) { |
83 | | - term = new Term(fieldName, BytesRefs.toBytesRef(value)); |
84 | | - } else { |
85 | | - if (mapper.getTextSearchInfo().hasPositions() == false) { |
86 | | - throw new IllegalArgumentException( |
87 | | - "Span term query requires position data, but field " + fieldName + " was indexed without position data" |
88 | | - ); |
89 | | - } |
90 | | - Query termQuery = mapper.termQuery(value, context); |
91 | | - List<Term> termsList = new ArrayList<>(); |
92 | | - termQuery.visit(new QueryVisitor() { |
93 | | - @Override |
94 | | - public QueryVisitor getSubVisitor(BooleanClause.Occur occur, Query parent) { |
95 | | - if (occur == BooleanClause.Occur.MUST || occur == BooleanClause.Occur.FILTER) { |
96 | | - return this; |
97 | | - } |
98 | | - return EMPTY_VISITOR; |
| 81 | + return new SpanMatchNoDocsQuery(fieldName, "unmapped field: " + fieldName); |
| 82 | + } |
| 83 | + if (mapper.getTextSearchInfo().hasPositions() == false) { |
| 84 | + throw new IllegalArgumentException( |
| 85 | + "Span term query requires position data, but field " + fieldName + " was indexed without position data" |
| 86 | + ); |
| 87 | + } |
| 88 | + Query termQuery = mapper.termQuery(value, context); |
| 89 | + List<Term> termsList = new ArrayList<>(); |
| 90 | + termQuery.visit(new QueryVisitor() { |
| 91 | + @Override |
| 92 | + public QueryVisitor getSubVisitor(BooleanClause.Occur occur, Query parent) { |
| 93 | + if (occur == BooleanClause.Occur.MUST || occur == BooleanClause.Occur.FILTER) { |
| 94 | + return this; |
99 | 95 | } |
| 96 | + return EMPTY_VISITOR; |
| 97 | + } |
100 | 98 |
|
101 | | - @Override |
102 | | - public void consumeTerms(Query query, Term... terms) { |
103 | | - termsList.addAll(Arrays.asList(terms)); |
104 | | - } |
105 | | - }); |
106 | | - if (termsList.size() != 1) { |
107 | | - // This is for safety, but we have called mapper.termQuery above: we really should get one and only one term from the query? |
108 | | - throw new IllegalArgumentException("Cannot extract a term from a query of type " + termQuery.getClass() + ": " + termQuery); |
| 99 | + @Override |
| 100 | + public void consumeTerms(Query query, Term... terms) { |
| 101 | + termsList.addAll(Arrays.asList(terms)); |
109 | 102 | } |
110 | | - term = termsList.get(0); |
| 103 | + }); |
| 104 | + if (termsList.size() != 1) { |
| 105 | + // This is for safety, but we have called mapper.termQuery above: we really should get one and only one term from the query? |
| 106 | + throw new IllegalArgumentException("Cannot extract a term from a query of type " + termQuery.getClass() + ": " + termQuery); |
111 | 107 | } |
112 | | - return new SpanTermQuery(term); |
| 108 | + return new SpanTermQuery(termsList.get(0)); |
113 | 109 | } |
114 | 110 |
|
115 | 111 | public static SpanTermQueryBuilder fromXContent(XContentParser parser) throws IOException, ParsingException { |
|
0 commit comments