27
27
import org .apache .lucene .util .BytesRef ;
28
28
import org .elasticsearch .common .inject .Inject ;
29
29
import org .elasticsearch .common .lucene .BytesRefs ;
30
- import org .elasticsearch .common .lucene .search .*;
30
+ import org .elasticsearch .common .lucene .search .AndFilter ;
31
+ import org .elasticsearch .common .lucene .search .OrFilter ;
32
+ import org .elasticsearch .common .lucene .search .TermFilter ;
33
+ import org .elasticsearch .common .lucene .search .XBooleanFilter ;
31
34
import org .elasticsearch .common .xcontent .XContentParser ;
32
35
import org .elasticsearch .index .cache .filter .support .CacheKeyFilter ;
33
36
import org .elasticsearch .index .mapper .FieldMapper ;
34
37
import org .elasticsearch .index .mapper .MapperService ;
38
+ import org .elasticsearch .indices .cache .filter .terms .IndicesTermsFilterCache ;
39
+ import org .elasticsearch .indices .cache .filter .terms .TermsLookup ;
35
40
36
41
import java .io .IOException ;
37
42
import java .util .List ;
@@ -45,6 +50,8 @@ public class TermsFilterParser implements FilterParser {
45
50
46
51
public static final String NAME = "terms" ;
47
52
53
+ private IndicesTermsFilterCache termsFilterCache ;
54
+
48
55
@ Inject
49
56
public TermsFilterParser () {
50
57
}
@@ -54,6 +61,11 @@ public String[] names() {
54
61
return new String []{NAME , "in" };
55
62
}
56
63
64
+ @ Inject (optional = true )
65
+ public void setIndicesTermsFilterCache (IndicesTermsFilterCache termsFilterCache ) {
66
+ this .termsFilterCache = termsFilterCache ;
67
+ }
68
+
57
69
@ Override
58
70
public Filter parse (QueryParseContext parseContext ) throws IOException , QueryParsingException {
59
71
XContentParser parser = parseContext .parser ();
@@ -62,6 +74,12 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
62
74
Boolean cache = null ;
63
75
String filterName = null ;
64
76
String currentFieldName = null ;
77
+
78
+ String lookupIndex = parseContext .index ().name ();
79
+ String lookupType = null ;
80
+ String lookupId = null ;
81
+ String lookupPath = null ;
82
+
65
83
CacheKeyFilter .Key cacheKey = null ;
66
84
XContentParser .Token token ;
67
85
String execution = "plain" ;
@@ -80,6 +98,34 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
80
98
}
81
99
terms .add (value );
82
100
}
101
+ } else if (token == XContentParser .Token .START_OBJECT ) {
102
+ fieldName = currentFieldName ;
103
+ while ((token = parser .nextToken ()) != XContentParser .Token .END_OBJECT ) {
104
+ if (token == XContentParser .Token .FIELD_NAME ) {
105
+ currentFieldName = parser .currentName ();
106
+ } else if (token .isValue ()) {
107
+ if ("index" .equals (currentFieldName )) {
108
+ lookupIndex = parser .text ();
109
+ } else if ("type" .equals (currentFieldName )) {
110
+ lookupType = parser .text ();
111
+ } else if ("id" .equals (currentFieldName )) {
112
+ lookupId = parser .text ();
113
+ } else if ("path" .equals (currentFieldName )) {
114
+ lookupPath = parser .text ();
115
+ } else {
116
+ throw new QueryParsingException (parseContext .index (), "[terms] filter does not support [" + currentFieldName + "] within lookup element" );
117
+ }
118
+ }
119
+ }
120
+ if (lookupType == null ) {
121
+ throw new QueryParsingException (parseContext .index (), "[terms] filter lookup element requires specifying the type" );
122
+ }
123
+ if (lookupId == null ) {
124
+ throw new QueryParsingException (parseContext .index (), "[terms] filter lookup element requires specifying the id" );
125
+ }
126
+ if (lookupPath == null ) {
127
+ throw new QueryParsingException (parseContext .index (), "[terms] filter lookup element requires specifying the path" );
128
+ }
83
129
} else if (token .isValue ()) {
84
130
if ("execution" .equals (currentFieldName )) {
85
131
execution = parser .text ();
@@ -113,6 +159,17 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
113
159
}
114
160
}
115
161
162
+ if (lookupId != null ) {
163
+ // external lookup, use it
164
+ TermsLookup termsLookup = new TermsLookup (fieldMapper , lookupIndex , lookupType , lookupId , lookupPath );
165
+ if (cacheKey == null ) {
166
+ cacheKey = new CacheKeyFilter .Key (termsLookup .toString ());
167
+ }
168
+ Filter filter = termsFilterCache .lookupTermsFilter (cacheKey , termsLookup );
169
+ filter = parseContext .cacheFilter (filter , null ); // cacheKey is passed as null, so we don't double cache the key
170
+ return filter ;
171
+ }
172
+
116
173
try {
117
174
Filter filter ;
118
175
if ("plain" .equals (execution )) {
0 commit comments