Java Code Examples for org.elasticsearch.index.shard.ShardUtils

The following examples show how to use org.elasticsearch.index.shard.ShardUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Elasticsearch   Source File:    License: Apache License 2.0 5 votes vote down vote up
private BitSet getAndLoadIfNotPresent(final Query query, final LeafReaderContext context) throws IOException, ExecutionException {
    final Object coreCacheReader = context.reader().getCoreCacheKey();
    final ShardId shardId = ShardUtils.extractShardId(context.reader());
    if (shardId != null // can't require it because of the percolator
            && index.getName().equals(shardId.getIndex()) == false) {
        // insanity
        throw new IllegalStateException("Trying to load bit set for index [" + shardId.getIndex()
                + "] with cache of index [" + index.getName() + "]");
    Cache<Query, Value> filterToFbs = loadedFilters.get(coreCacheReader, new Callable<Cache<Query, Value>>() {
        public Cache<Query, Value> call() throws Exception {
            return CacheBuilder.newBuilder().build();
    return filterToFbs.get(query,new Callable<Value>() {
        public Value call() throws Exception {
            final IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(context);
            final IndexSearcher searcher = new IndexSearcher(topLevelContext);
            final Weight weight = searcher.createNormalizedWeight(query, false);
            final Scorer s = weight.scorer(context);
            final BitSet bitSet;
            if (s == null) {
                bitSet = null;
            } else {
                bitSet = BitSet.of(s.iterator(), context.reader().maxDoc());

            Value value = new Value(bitSet, shardId);
            listener.onCache(shardId, value.bitset);
            return value;
Example 2
protected DirectoryReader dlsFlsWrap(final DirectoryReader reader, boolean isAdmin) throws IOException {

    final ShardId shardId = ShardUtils.extractShardId(reader);

    Set<String> flsFields = null;
    Set<String> maskedFields = null;
    Query dlsQuery = null;

    if(!isAdmin) {

        final Map<String, Set<String>> allowedFlsFields = (Map<String, Set<String>>) HeaderHelper.deserializeSafeFromHeader(threadContext,
        final Map<String, Set<String>> queries = (Map<String, Set<String>>) HeaderHelper.deserializeSafeFromHeader(threadContext,
        final Map<String, Set<String>> maskedFieldsMap = (Map<String, Set<String>>) HeaderHelper.deserializeSafeFromHeader(threadContext,

        final String flsEval = OpenDistroSecurityUtils.evalMap(allowedFlsFields, index.getName());
        final String dlsEval = OpenDistroSecurityUtils.evalMap(queries, index.getName());
        final String maskedEval = OpenDistroSecurityUtils.evalMap(maskedFieldsMap, index.getName());

        if (flsEval != null) {
            flsFields = new HashSet<>(metaFields);

        if (dlsEval != null) {
            final Set<String> unparsedDlsQueries = queries.get(dlsEval);
            if(unparsedDlsQueries != null && !unparsedDlsQueries.isEmpty()) {
                //disable reader optimizations
                dlsQuery = DlsQueryParser.parse(unparsedDlsQueries, this.indexService.newQueryShardContext(shardId.getId(), null, nowInMillis, null)
                        , this.indexService.xContentRegistry());

        if (maskedEval != null) {
            maskedFields = new HashSet<>();

    return new DlsFlsFilterLeafReader.DlsFlsDirectoryReader(reader, flsFields, dlsQuery,
            indexService, threadContext, clusterService, complianceConfig, auditlog, maskedFields, shardId);