3030import org .testng .annotations .BeforeMethod ;
3131import org .testng .annotations .Test ;
3232
33+ import java .util .concurrent .atomic .AtomicInteger ;
34+
3335import static org .elasticsearch .client .Requests .refreshRequest ;
3436import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
3537import static org .elasticsearch .index .query .FilterBuilders .scriptFilter ;
36- import static org .elasticsearch .index .query .QueryBuilders .filteredQuery ;
37- import static org .elasticsearch .index .query .QueryBuilders .matchAllQuery ;
38+ import static org .elasticsearch .index .query .QueryBuilders .*;
3839import static org .hamcrest .MatcherAssert .assertThat ;
3940import static org .hamcrest .Matchers .equalTo ;
4041
@@ -68,6 +69,11 @@ protected Client getClient() {
6869
6970 @ Test
7071 public void testCustomScriptBoost () throws Exception {
72+ try {
73+ client .admin ().indices ().prepareDelete ("test" ).execute ().actionGet ();
74+ } catch (Exception ex ) {
75+ //
76+ }
7177 client .admin ().indices ().prepareCreate ("test" ).execute ().actionGet ();
7278 client .prepareIndex ("test" , "type1" , "1" )
7379 .setSource (jsonBuilder ().startObject ().field ("test" , "value beck" ).field ("num1" , 1.0f ).endObject ())
@@ -121,4 +127,65 @@ public void testCustomScriptBoost() throws Exception {
121127 assertThat (response .hits ().getAt (2 ).id (), equalTo ("3" ));
122128 assertThat ((Double ) response .hits ().getAt (2 ).fields ().get ("sNum1" ).values ().get (0 ), equalTo (3.0 ));
123129 }
130+
131+ private static AtomicInteger scriptCounter = new AtomicInteger (0 );
132+
133+ public static int incrementScriptCounter () {
134+ return scriptCounter .incrementAndGet ();
135+ }
136+
137+ @ Test
138+ public void testCustomScriptCache () throws Exception {
139+ try {
140+ client .admin ().indices ().prepareDelete ("test" ).execute ().actionGet ();
141+ } catch (Exception ex ) {
142+ //
143+ }
144+ client .admin ().indices ().prepareCreate ("test" ).execute ().actionGet ();
145+ client .prepareIndex ("test" , "type1" , "1" ).setSource (jsonBuilder ().startObject ().field ("test" , "1" ).field ("num" , 1.0f ).endObject ()).execute ().actionGet ();
146+ client .admin ().indices ().prepareFlush ().execute ().actionGet ();
147+ client .prepareIndex ("test" , "type1" , "2" ).setSource (jsonBuilder ().startObject ().field ("test" , "2" ).field ("num" , 2.0f ).endObject ()).execute ().actionGet ();
148+ client .admin ().indices ().prepareFlush ().execute ().actionGet ();
149+ client .prepareIndex ("test" , "type1" , "3" ).setSource (jsonBuilder ().startObject ().field ("test" , "3" ).field ("num" , 3.0f ).endObject ()).execute ().actionGet ();
150+ client .admin ().indices ().prepareFlush ().execute ().actionGet ();
151+ client .admin ().indices ().refresh (refreshRequest ()).actionGet ();
152+
153+ String script = "org.elasticsearch.test.integration.search.scriptfilter.ScriptFilterSearchTests.incrementScriptCounter() > 0" ;
154+
155+ scriptCounter .set (0 );
156+ logger .info ("running script filter the first time" );
157+ SearchResponse response = client .prepareSearch ()
158+ .setQuery (filteredQuery (termQuery ("test" , "1" ), scriptFilter (script ).cache (true )))
159+ .execute ().actionGet ();
160+
161+ assertThat (response .hits ().totalHits (), equalTo (1l ));
162+ assertThat (scriptCounter .get (), equalTo (3 ));
163+
164+ scriptCounter .set (0 );
165+ logger .info ("running script filter the second time" );
166+ response = client .prepareSearch ()
167+ .setQuery (filteredQuery (termQuery ("test" , "2" ), scriptFilter (script ).cache (true )))
168+ .execute ().actionGet ();
169+
170+ assertThat (response .hits ().totalHits (), equalTo (1l ));
171+ assertThat (scriptCounter .get (), equalTo (0 ));
172+
173+ scriptCounter .set (0 );
174+ logger .info ("running script filter with new parameters" );
175+ response = client .prepareSearch ()
176+ .setQuery (filteredQuery (termQuery ("test" , "1" ), scriptFilter (script ).addParam ("param1" , "1" ).cache (true )))
177+ .execute ().actionGet ();
178+
179+ assertThat (response .hits ().totalHits (), equalTo (1l ));
180+ assertThat (scriptCounter .get (), equalTo (3 ));
181+
182+ scriptCounter .set (0 );
183+ logger .info ("running script filter with same parameters" );
184+ response = client .prepareSearch ()
185+ .setQuery (filteredQuery (matchAllQuery (), scriptFilter (script ).addParam ("param1" , "1" ).cache (true )))
186+ .execute ().actionGet ();
187+
188+ assertThat (response .hits ().totalHits (), equalTo (3l ));
189+ assertThat (scriptCounter .get (), equalTo (0 ));
190+ }
124191}
0 commit comments