Skip to content

Commit

Permalink
add query parser cache to cache common query strings (+settings) with…
Browse files Browse the repository at this point in the history
… the parsed queries
  • Loading branch information
kimchy committed Sep 12, 2010
1 parent 77a7938 commit 4822517
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.action.support.DefaultShardOperationFailedException;
import org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException;
import org.elasticsearch.action.support.broadcast.TransportBroadcastOperationAction;
import org.elasticsearch.cache.query.parser.QueryParserCache;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.routing.GroupShardsIterator;
Expand All @@ -48,9 +49,12 @@
*/
public class TransportClearIndicesCacheAction extends TransportBroadcastOperationAction<ClearIndicesCacheRequest, ClearIndicesCacheResponse, ShardClearIndicesCacheRequest, ShardClearIndicesCacheResponse> {

private final QueryParserCache queryParserCache;

@Inject public TransportClearIndicesCacheAction(Settings settings, ThreadPool threadPool, ClusterService clusterService,
TransportService transportService, IndicesService indicesService) {
TransportService transportService, IndicesService indicesService, QueryParserCache queryParserCache) {
super(settings, threadPool, clusterService, transportService, indicesService);
this.queryParserCache = queryParserCache;
}

@Override protected String transportAction() {
Expand Down Expand Up @@ -106,6 +110,7 @@ public class TransportClearIndicesCacheAction extends TransportBroadcastOperatio
@Override protected ShardClearIndicesCacheResponse shardOperation(ShardClearIndicesCacheRequest request) throws ElasticSearchException {
// TODO we can optimize to go to a single node where the index exists
IndexCache cache = indicesService.indexServiceSafe(request.index()).cache();
queryParserCache.clear();
if (request.filterCache()) {
cache.filter().clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@

package org.elasticsearch.cache.query.parser;

import org.apache.lucene.queryParser.QueryParserSettings;
import org.apache.lucene.search.Query;

/**
* @author kimchy (shay.banon)
*/
public interface QueryParserCache {

Query get(String queryString);
Query get(QueryParserSettings queryString);

void put(String queryString, Query query);
void put(QueryParserSettings queryString, Query query);

void clear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.cache.query.parser.none;

import org.apache.lucene.queryParser.QueryParserSettings;
import org.apache.lucene.search.Query;
import org.elasticsearch.cache.query.parser.QueryParserCache;
import org.elasticsearch.common.inject.Inject;
Expand All @@ -31,10 +32,13 @@ public class NoneQueryParserCache implements QueryParserCache {
@Inject public NoneQueryParserCache() {
}

@Override public Query get(String queryString) {
@Override public Query get(QueryParserSettings queryString) {
return null;
}

@Override public void put(String queryString, Query query) {
@Override public void put(QueryParserSettings queryString, Query query) {
}

@Override public void clear() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.cache.query.parser.soft;

import org.apache.lucene.queryParser.QueryParserSettings;
import org.apache.lucene.search.Query;
import org.elasticsearch.cache.query.parser.support.AbstractJvmQueryParserCache;
import org.elasticsearch.common.collect.MapMaker;
Expand All @@ -31,6 +32,6 @@
public class SoftQueryParserCache extends AbstractJvmQueryParserCache {

@Inject public SoftQueryParserCache(Settings settings) {
super(settings, new MapMaker().softValues().<String, Query>makeMap());
super(settings, new MapMaker().softValues().<QueryParserSettings, Query>makeMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.cache.query.parser.support;

import org.apache.lucene.queryParser.QueryParserSettings;
import org.apache.lucene.search.Query;
import org.elasticsearch.cache.query.parser.QueryParserCache;
import org.elasticsearch.common.component.AbstractComponent;
Expand All @@ -31,18 +32,22 @@
*/
public class AbstractJvmQueryParserCache extends AbstractComponent implements QueryParserCache {

final ConcurrentMap<String, Query> cache;
final ConcurrentMap<QueryParserSettings, Query> cache;

protected AbstractJvmQueryParserCache(Settings settings, ConcurrentMap<String, Query> cache) {
protected AbstractJvmQueryParserCache(Settings settings, ConcurrentMap<QueryParserSettings, Query> cache) {
super(settings);
this.cache = cache;
}

@Override public Query get(String queryString) {
@Override public void clear() {
cache.clear();
}

@Override public Query get(QueryParserSettings queryString) {
return cache.get(queryString);
}

@Override public void put(String queryString, Query query) {
@Override public void put(QueryParserSettings queryString, Query query) {
cache.put(queryString, query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.cache.query.parser.weak;

import org.apache.lucene.queryParser.QueryParserSettings;
import org.apache.lucene.search.Query;
import org.elasticsearch.cache.query.parser.support.AbstractJvmQueryParserCache;
import org.elasticsearch.common.collect.MapMaker;
Expand All @@ -31,6 +32,6 @@
public class WeakQueryParserCache extends AbstractJvmQueryParserCache {

@Inject public WeakQueryParserCache(Settings settings) {
super(settings, new MapMaker().weakValues().<String, Query>makeMap());
super(settings, new MapMaker().weakValues().<QueryParserSettings, Query>makeMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.QueryParserSettings;
import org.apache.lucene.search.Query;
import org.elasticsearch.cache.query.parser.QueryParserCache;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
Expand All @@ -46,9 +47,12 @@ public class FieldQueryParser extends AbstractIndexComponent implements XContent

private final AnalysisService analysisService;

@Inject public FieldQueryParser(Index index, @IndexSettings Settings settings, AnalysisService analysisService) {
private final QueryParserCache queryParserCache;

@Inject public FieldQueryParser(Index index, @IndexSettings Settings settings, AnalysisService analysisService, QueryParserCache queryParserCache) {
super(index, settings);
this.analysisService = analysisService;
this.queryParserCache = queryParserCache;
}

@Override public String[] names() {
Expand Down Expand Up @@ -122,12 +126,19 @@ public class FieldQueryParser extends AbstractIndexComponent implements XContent
qpSettings.queryString(QueryParser.escape(qpSettings.queryString()));
}

Query query = queryParserCache.get(qpSettings);
if (query != null) {
return query;
}

MapperQueryParser queryParser = parseContext.queryParser(qpSettings);

try {
Query query = queryParser.parse(qpSettings.queryString());
query = queryParser.parse(qpSettings.queryString());
query.setBoost(qpSettings.boost());
return optimizeQuery(fixNegativeQueryIfNeeded(query));
query = optimizeQuery(fixNegativeQueryIfNeeded(query));
queryParserCache.put(qpSettings, query);
return query;
} catch (ParseException e) {
throw new QueryParsingException(index, "Failed to parse query [" + qpSettings.queryString() + "]", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.elasticsearch.cache.query.parser.QueryParserCache;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.inject.Inject;
Expand All @@ -50,9 +51,12 @@ public class QueryStringQueryParser extends AbstractIndexComponent implements XC

private final AnalysisService analysisService;

@Inject public QueryStringQueryParser(Index index, @IndexSettings Settings settings, AnalysisService analysisService) {
private final QueryParserCache queryParserCache;

@Inject public QueryStringQueryParser(Index index, @IndexSettings Settings settings, AnalysisService analysisService, QueryParserCache queryParserCache) {
super(index, settings);
this.analysisService = analysisService;
this.queryParserCache = queryParserCache;
}

@Override public String[] names() {
Expand Down Expand Up @@ -150,6 +154,11 @@ public class QueryStringQueryParser extends AbstractIndexComponent implements XC
qpSettings.queryString(QueryParser.escape(qpSettings.queryString()));
}

Query query = queryParserCache.get(qpSettings);
if (query != null) {
return query;
}

MapperQueryParser queryParser;
if (qpSettings.fields() != null) {
if (qpSettings.fields().size() == 1) {
Expand All @@ -163,10 +172,13 @@ public class QueryStringQueryParser extends AbstractIndexComponent implements XC
queryParser = parseContext.queryParser(qpSettings);
}


try {
Query query = queryParser.parse(qpSettings.queryString());
query = queryParser.parse(qpSettings.queryString());
query.setBoost(qpSettings.boost());
return optimizeQuery(fixNegativeQueryIfNeeded(query));
query = optimizeQuery(fixNegativeQueryIfNeeded(query));
queryParserCache.put(qpSettings, query);
return query;
} catch (ParseException e) {
throw new QueryParsingException(index, "Failed to parse query [" + qpSettings.queryString() + "]", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.lucene.search.*;
import org.apache.lucene.search.spans.*;
import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.cache.query.parser.QueryParserCacheModule;
import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.inject.ModulesBuilder;
import org.elasticsearch.common.lucene.geo.GeoBoundingBoxFilter;
Expand Down Expand Up @@ -77,6 +78,7 @@ public class SimpleIndexQueryParserTests {
Index index = new Index("test");
Injector injector = new ModulesBuilder().add(
new SettingsModule(settings),
new QueryParserCacheModule(settings),
new ScriptModule(),
new MapperServiceModule(),
new IndexSettingsModule(settings),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.index.query.xcontent.guice;

import org.elasticsearch.cache.query.parser.QueryParserCacheModule;
import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.inject.ModulesBuilder;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class IndexQueryParserModuleTests {
Index index = new Index("test");
Injector injector = new ModulesBuilder().add(
new SettingsModule(settings),
new QueryParserCacheModule(settings),
new ScriptModule(),
new IndexSettingsModule(settings),
new IndexCacheModule(settings),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.index.query.xcontent.plugin;

import org.elasticsearch.cache.query.parser.QueryParserCacheModule;
import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.inject.ModulesBuilder;
import org.elasticsearch.common.settings.ImmutableSettings;
Expand Down Expand Up @@ -63,6 +64,7 @@ public class IndexQueryParserPluginTests {
Injector injector = new ModulesBuilder().add(
new SettingsModule(settings),
new ScriptModule(),
new QueryParserCacheModule(settings),
new IndexSettingsModule(settings),
new IndexCacheModule(settings),
new AnalysisModule(settings),
Expand Down

0 comments on commit 4822517

Please sign in to comment.