Example usage for org.apache.lucene.search.join ToParentBlockJoinQuery ToParentBlockJoinQuery

List of usage examples for org.apache.lucene.search.join ToParentBlockJoinQuery ToParentBlockJoinQuery

Introduction

In this page you can find the example usage for org.apache.lucene.search.join ToParentBlockJoinQuery ToParentBlockJoinQuery.

Prototype

public ToParentBlockJoinQuery(Query childQuery, BitSetProducer parentsFilter, ScoreMode scoreMode) 

Source Link

Document

Create a ToParentBlockJoinQuery.

Usage

From source file:edu.mayo.cts2.framework.plugin.service.lexevs.service.codesystemversion.LexEvsCodeSystemVersionQueryServiceTestIT.java

License:Open Source License

@Test
public void queryPropertyTest() throws ParseException {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new TermQuery(new Term("isParentDoc", "true")), Occur.MUST_NOT);
    builder.add(new TermQuery(new Term("code", "C99998")), Occur.MUST);
    builder.add(new TermQuery(new Term("propertyName", "Contributing_Source")), Occur.MUST);
    QueryParser propValueParser = new QueryParser("propertyValue",
            sourceAssertedValueSetSearchIndexService.getAnalyzer());
    builder.add(propValueParser.createBooleanQuery("propertyValue", "FDA"), Occur.MUST);
    Query query = builder.build();
    QueryBitSetProducer parentFilter;/*from  ww  w  .ja va  2 s  .com*/
    parentFilter = new QueryBitSetProducer(
            new QueryParser("isParentDoc", new StandardAnalyzer(new CharArraySet(0, true))).parse("true"));
    ToParentBlockJoinQuery blockJoinQuery = new ToParentBlockJoinQuery(query, parentFilter, ScoreMode.Total);

    List<ScoreDoc> docs = sourceAssertedValueSetSearchIndexService.query(null, blockJoinQuery);
    assertNotNull(docs);
    assertTrue(docs.size() > 0);
    ScoreDoc sd = docs.get(0);
    Document doc = sourceAssertedValueSetSearchIndexService.getById(sd.doc);
    assertNotNull(doc);

    boolean fieldFound = false;

    List<IndexableField> fields = doc.getFields();
    for (IndexableField field : fields) {
        if (field.name().equals("entityCode") && field.stringValue().equals("C99998")) {
            fieldFound = true;
        }
    }

    assertTrue(fieldFound);
}

From source file:edu.mayo.cts2.framework.plugin.service.lexevs.service.codesystemversion.LexEvsCodeSystemVersionQueryServiceTestIT.java

License:Open Source License

@Test
public void queryPublishPropertyTest() throws ParseException {

    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new TermQuery(new Term("isParentDoc", "true")), Occur.MUST_NOT);
    builder.add(new TermQuery(new Term("code", "C99999")), Occur.MUST);
    builder.add(new TermQuery(new Term("propertyName", "Publish_Value_Set")), Occur.MUST);
    QueryParser propValueParser = new QueryParser("propertyValue",
            sourceAssertedValueSetSearchIndexService.getAnalyzer());
    builder.add(propValueParser.createBooleanQuery("propertyValue", "Yes"), Occur.MUST);
    Query query = builder.build();
    QueryBitSetProducer parentFilter;//from  w ww  .  j  a  v a2  s. c o  m
    parentFilter = new QueryBitSetProducer(
            new QueryParser("isParentDoc", new StandardAnalyzer(new CharArraySet(0, true))).parse("true"));
    ToParentBlockJoinQuery blockJoinQuery = new ToParentBlockJoinQuery(query, parentFilter, ScoreMode.Total);

    List<ScoreDoc> docs = sourceAssertedValueSetSearchIndexService.query(null, blockJoinQuery);
    assertNotNull(docs);
    assertTrue(docs.size() > 0);
    ScoreDoc sd = docs.get(0);
    Document doc = sourceAssertedValueSetSearchIndexService.getById(sd.doc);
    assertNotNull(doc);

    boolean fieldFound = false;

    List<IndexableField> fields = doc.getFields();
    for (IndexableField field : fields) {
        if (field.name().equals("entityCode") && field.stringValue().equals("C99999")) {
            fieldFound = true;
        }
    }

    assertTrue(fieldFound);
}

From source file:org.apache.solr.handler.dataimport.TestHierarchicalDocBuilder.java

License:Apache License

@Test
public void testThreeLevelHierarchy() throws Exception {
    int parentsNum = 3; //fixed for simplicity of test
    int childrenNum = 0;
    int grandChildrenNum = 0;

    final String parentType = "parent";
    final String childType = "child";
    final String grandChildType = "grand_child";

    List<String> parentIds = createDataIterator("select * from PARENT", parentType, parentType, parentsNum);
    Collections.shuffle(parentIds, random());
    String parentId1 = parentIds.get(0);
    String parentId2 = parentIds.get(1);

    //parent 1 children
    int firstParentChildrenNum = 3; //fixed for simplicity of test
    String select = "select * from CHILD where parent_id='" + parentId1 + "'";
    List<String> childrenIds = createDataIterator(select, childType, "child of first parent",
            firstParentChildrenNum);//from  ww  w.j a v  a  2 s  .c  o  m
    List<String> firstParentChildrenIds = new ArrayList<String>(childrenIds);
    childrenNum += childrenIds.size();

    // grand children of first parent first child
    String childId = childrenIds.get(0);
    String description = "grandchild of first parent, child of " + childId + " child";
    select = "select * from GRANDCHILD where parent_id='" + childId + "'";
    List<String> grandChildrenIds = createDataIterator(select, grandChildType, description, atLeast(2));
    grandChildrenNum += grandChildrenIds.size();

    // grand children of first parent second child
    childId = childrenIds.get(1);
    description = "grandchild of first parent, child of " + childId + " child";
    select = "select * from GRANDCHILD where parent_id='" + childId + "'";
    List<String> grandChildrenIds2 = createDataIterator(select, grandChildType, description, atLeast(2));
    grandChildrenNum += grandChildrenIds2.size();

    grandChildrenIds.addAll(grandChildrenIds2);

    // third children of first parent has no grand children

    // parent 2 children (no grand children)   
    select = "select * from CHILD where parent_id='" + parentId2 + "'";
    childrenIds = createDataIterator(select, childType, "child of second parent", atLeast(2));
    childrenNum += childrenIds.size();

    // parent 3 has no children and grand children

    int totalDocsNum = parentsNum + childrenNum + grandChildrenNum;

    runFullImport(threeLevelHierarchyConfig);

    assertTrue("Update request processor processAdd was not called",
            TestUpdateRequestProcessor.processAddCalled);
    assertTrue("Update request processor processCommit was not callled",
            TestUpdateRequestProcessor.processCommitCalled);
    assertTrue("Update request processor finish was not called", TestUpdateRequestProcessor.finishCalled);

    // very simple asserts to check that we at least have correct num of docs indexed
    assertQ(req("*:*"), "//*[@numFound='" + totalDocsNum + "']");
    assertQ(req("type_s:parent"), "//*[@numFound='" + parentsNum + "']");
    assertQ(req("type_s:child"), "//*[@numFound='" + childrenNum + "']");
    assertQ(req("type_s:grand_child"), "//*[@numFound='" + grandChildrenNum + "']");

    // let's check BlockJoin
    // get first parent by any grand children
    String randomGrandChildId = grandChildrenIds.get(random().nextInt(grandChildrenIds.size()));
    Query query = createToParentQuery(parentType, FIELD_ID, randomGrandChildId);
    assertSearch(query, FIELD_ID, parentId1);

    // get first parent by any children 
    String randomChildId = firstParentChildrenIds.get(random().nextInt(firstParentChildrenIds.size()));
    query = createToParentQuery(parentType, FIELD_ID, randomChildId);
    assertSearch(query, FIELD_ID, parentId1);

    // get parent by children by grand children
    randomGrandChildId = grandChildrenIds.get(random().nextInt(grandChildrenIds.size()));
    ToParentBlockJoinQuery childBlockJoinQuery = createToParentQuery(childType, FIELD_ID, randomGrandChildId);
    ToParentBlockJoinQuery blockJoinQuery = new ToParentBlockJoinQuery(childBlockJoinQuery,
            createParentFilter(parentType), ScoreMode.Avg);
    assertSearch(blockJoinQuery, FIELD_ID, parentId1);
}

From source file:org.apache.solr.handler.dataimport.TestHierarchicalDocBuilder.java

License:Apache License

private ToParentBlockJoinQuery createToParentQuery(String parentType, Query childQuery) {
    ToParentBlockJoinQuery blockJoinQuery = new ToParentBlockJoinQuery(childQuery,
            createParentFilter(parentType), ScoreMode.Avg);

    return blockJoinQuery;
}

From source file:org.apache.solr.search.join.BlockJoinParentQParser.java

License:Apache License

protected Query createQuery(Query parentList, Query query) {
    return new ToParentBlockJoinQuery(query, getFilter(parentList), ScoreMode.None);
}

From source file:org.apache.solr.update.AddBlockUpdateTest.java

License:Apache License

protected ToParentBlockJoinQuery join(final String childTerm) {
    return new ToParentBlockJoinQuery(new TermQuery(new Term(child, childTerm)),
            new TermRangeFilter(parent, null, null, false, false), ScoreMode.None);
}

From source file:org.elasticsearch.index.fielddata.AbstractStringFieldDataTestCase.java

License:Apache License

public void testNestedSorting(MultiValueMode sortMode) throws IOException {
    final String[] values = new String[randomIntBetween(2, 20)];
    for (int i = 0; i < values.length; ++i) {
        values[i] = TestUtil.randomSimpleString(getRandom());
    }//from ww w  .  ja va2 s  . com
    final int numParents = scaledRandomIntBetween(10, 3072);
    List<Document> docs = new ArrayList<>();
    FixedBitSet parents = new FixedBitSet(64);
    for (int i = 0; i < numParents; ++i) {
        docs.clear();
        final int numChildren = randomInt(4);
        for (int j = 0; j < numChildren; ++j) {
            final Document child = new Document();
            final int numValues = randomInt(3);
            for (int k = 0; k < numValues; ++k) {
                final String value = RandomPicks.randomFrom(getRandom(), values);
                addField(child, "text", value);
            }
            docs.add(child);
        }
        final Document parent = new Document();
        parent.add(new StringField("type", "parent", Store.YES));
        final String value = RandomPicks.randomFrom(getRandom(), values);
        if (value != null) {
            addField(parent, "text", value);
        }
        docs.add(parent);
        int bit = parents.prevSetBit(parents.length() - 1) + docs.size();
        parents = FixedBitSet.ensureCapacity(parents, bit);
        parents.set(bit);
        writer.addDocuments(docs);
        if (randomInt(10) == 0) {
            writer.commit();
        }
    }
    DirectoryReader directoryReader = DirectoryReader.open(writer, true);
    directoryReader = ElasticsearchDirectoryReader.wrap(directoryReader, new ShardId(new Index("test"), 0));
    IndexSearcher searcher = new IndexSearcher(directoryReader);
    IndexFieldData<?> fieldData = getForField("text");
    final Object missingValue;
    switch (randomInt(4)) {
    case 0:
        missingValue = "_first";
        break;
    case 1:
        missingValue = "_last";
        break;
    case 2:
        missingValue = new BytesRef(RandomPicks.randomFrom(getRandom(), values));
        break;
    default:
        missingValue = new BytesRef(TestUtil.randomSimpleString(getRandom()));
        break;
    }
    Query parentFilter = new TermQuery(new Term("type", "parent"));
    Query childFilter = Queries.not(parentFilter);
    Nested nested = createNested(searcher, parentFilter, childFilter);
    BytesRefFieldComparatorSource nestedComparatorSource = new BytesRefFieldComparatorSource(fieldData,
            missingValue, sortMode, nested);
    ToParentBlockJoinQuery query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter),
            new QueryBitSetProducer(parentFilter), ScoreMode.None);
    Sort sort = new Sort(new SortField("text", nestedComparatorSource));
    TopFieldDocs topDocs = searcher.search(query, randomIntBetween(1, numParents), sort);
    assertTrue(topDocs.scoreDocs.length > 0);
    BytesRef previous = null;
    for (int i = 0; i < topDocs.scoreDocs.length; ++i) {
        final int docID = topDocs.scoreDocs[i].doc;
        assertTrue("expected " + docID + " to be a parent", parents.get(docID));
        BytesRef cmpValue = null;
        for (int child = parents.prevSetBit(docID - 1) + 1; child < docID; ++child) {
            String[] sVals = searcher.doc(child).getValues("text");
            final BytesRef[] vals;
            if (sVals.length == 0) {
                vals = new BytesRef[0];
            } else {
                vals = new BytesRef[sVals.length];
                for (int j = 0; j < vals.length; ++j) {
                    vals[j] = new BytesRef(sVals[j]);
                }
            }
            for (BytesRef value : vals) {
                if (cmpValue == null) {
                    cmpValue = value;
                } else if (sortMode == MultiValueMode.MIN && value.compareTo(cmpValue) < 0) {
                    cmpValue = value;
                } else if (sortMode == MultiValueMode.MAX && value.compareTo(cmpValue) > 0) {
                    cmpValue = value;
                }
            }
        }
        if (cmpValue == null) {
            if ("_first".equals(missingValue)) {
                cmpValue = new BytesRef();
            } else if ("_last".equals(missingValue) == false) {
                cmpValue = (BytesRef) missingValue;
            }
        }
        if (previous != null && cmpValue != null) {
            assertTrue(previous.utf8ToString() + "   /   " + cmpValue.utf8ToString(),
                    previous.compareTo(cmpValue) <= 0);
        }
        previous = cmpValue;
    }
    searcher.getIndexReader().close();
}

From source file:org.elasticsearch.index.fielddata.AbstractStringFieldDataTests.java

License:Apache License

public void testNestedSorting(SortMode sortMode) throws IOException {
    final String[] values = new String[randomIntBetween(2, 20)];
    for (int i = 0; i < values.length; ++i) {
        values[i] = _TestUtil.randomSimpleString(getRandom());
    }//from ww w  .j  a va 2s.  c  o m
    final int numParents = atLeast(100);
    List<Document> docs = new ArrayList<Document>();
    final OpenBitSet parents = new OpenBitSet();
    for (int i = 0; i < numParents; ++i) {
        docs.clear();
        final int numChildren = randomInt(4);
        for (int j = 0; j < numChildren; ++j) {
            final Document child = new Document();
            final int numValues = randomInt(3);
            for (int k = 0; k < numValues; ++k) {
                final String value = RandomPicks.randomFrom(getRandom(), values);
                child.add(new StringField("text", value, Store.YES));
            }
            docs.add(child);
        }
        final Document parent = new Document();
        parent.add(new StringField("type", "parent", Store.YES));
        final String value = RandomPicks.randomFrom(getRandom(), values);
        if (value != null) {
            parent.add(new StringField("text", value, Store.YES));
        }
        docs.add(parent);
        parents.set(parents.prevSetBit(parents.length() - 1) + docs.size());
        writer.addDocuments(docs);
        if (randomInt(10) == 0) {
            writer.commit();
        }
    }
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, true));
    IndexFieldData<?> fieldData = getForField("text");
    final BytesRef missingValue;
    switch (randomInt(4)) {
    case 0:
        missingValue = new BytesRef();
        break;
    case 1:
        missingValue = BytesRefFieldComparatorSource.MAX_TERM;
        break;
    case 2:
        missingValue = new BytesRef(RandomPicks.randomFrom(getRandom(), values));
        break;
    default:
        missingValue = new BytesRef(_TestUtil.randomSimpleString(getRandom()));
        break;
    }
    BytesRefFieldComparatorSource innerSource = new BytesRefFieldComparatorSource(fieldData, missingValue,
            sortMode);
    Filter parentFilter = new TermFilter(new Term("type", "parent"));
    Filter childFilter = new NotFilter(parentFilter);
    NestedFieldComparatorSource nestedComparatorSource = new NestedFieldComparatorSource(sortMode, innerSource,
            parentFilter, childFilter);
    ToParentBlockJoinQuery query = new ToParentBlockJoinQuery(
            new XFilteredQuery(new MatchAllDocsQuery(), childFilter),
            new FixedBitSetCachingWrapperFilter(parentFilter), ScoreMode.None);
    Sort sort = new Sort(new SortField("text", nestedComparatorSource));
    TopFieldDocs topDocs = searcher.search(query, randomIntBetween(1, numParents), sort);
    assertTrue(topDocs.scoreDocs.length > 0);
    BytesRef previous = null;
    for (int i = 0; i < topDocs.scoreDocs.length; ++i) {
        final int docID = topDocs.scoreDocs[i].doc;
        assertTrue("expected " + docID + " to be a parent", parents.get(docID));
        BytesRef cmpValue = null;
        for (int child = parents.prevSetBit(docID - 1) + 1; child < docID; ++child) {
            String[] vals = searcher.doc(child).getValues("text");
            if (vals.length == 0) {
                vals = new String[] { missingValue.utf8ToString() };
            }
            for (String value : vals) {
                final BytesRef bytesValue = new BytesRef(value);
                if (cmpValue == null) {
                    cmpValue = bytesValue;
                } else if (sortMode == SortMode.MIN && bytesValue.compareTo(cmpValue) < 0) {
                    cmpValue = bytesValue;
                } else if (sortMode == SortMode.MAX && bytesValue.compareTo(cmpValue) > 0) {
                    cmpValue = bytesValue;
                }
            }
        }
        if (cmpValue == null) {
            cmpValue = missingValue;
        }
        if (previous != null) {
            assertNotNull(cmpValue);
            assertTrue(previous.utf8ToString() + "   /   " + cmpValue.utf8ToString(),
                    previous.compareTo(cmpValue) <= 0);
        }
        previous = cmpValue;
    }
    searcher.getIndexReader().close();
}

From source file:org.elasticsearch.index.query.NestedFilterParser.java

License:Apache License

@Override
public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    Query query = null;//from  w w w  .  j a  va2 s .c  om
    boolean queryFound = false;
    Filter filter = null;
    boolean filterFound = false;
    float boost = 1.0f;
    boolean join = true;
    String path = null;
    boolean cache = false;
    CacheKeyFilter.Key cacheKey = null;
    String filterName = null;

    // we need a late binding filter so we can inject a parent nested filter inner nested queries
    NestedQueryParser.LateBindingParentFilter currentParentFilterContext = NestedQueryParser.parentFilterContext
            .get();

    NestedQueryParser.LateBindingParentFilter usAsParentFilter = new NestedQueryParser.LateBindingParentFilter();
    NestedQueryParser.parentFilterContext.set(usAsParentFilter);

    try {
        String currentFieldName = null;
        XContentParser.Token token;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token == XContentParser.Token.START_OBJECT) {
                if ("query".equals(currentFieldName)) {
                    queryFound = true;
                    query = parseContext.parseInnerQuery();
                } else if ("filter".equals(currentFieldName)) {
                    filterFound = true;
                    filter = parseContext.parseInnerFilter();
                } else {
                    throw new QueryParsingException(parseContext.index(),
                            "[nested] filter does not support [" + currentFieldName + "]");
                }
            } else if (token.isValue()) {
                if ("join".equals(currentFieldName)) {
                    join = parser.booleanValue();
                } else if ("path".equals(currentFieldName)) {
                    path = parser.text();
                } else if ("boost".equals(currentFieldName)) {
                    boost = parser.floatValue();
                } else if ("_scope".equals(currentFieldName)) {
                    throw new QueryParsingException(parseContext.index(),
                            "the [_scope] support in [nested] filter has been removed, use nested filter as a facet_filter in the relevant facet");
                } else if ("_name".equals(currentFieldName)) {
                    filterName = parser.text();
                } else if ("_cache".equals(currentFieldName)) {
                    cache = parser.booleanValue();
                } else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) {
                    cacheKey = new CacheKeyFilter.Key(parser.text());
                } else {
                    throw new QueryParsingException(parseContext.index(),
                            "[nested] filter does not support [" + currentFieldName + "]");
                }
            }
        }
        if (!queryFound && !filterFound) {
            throw new QueryParsingException(parseContext.index(),
                    "[nested] requires either 'query' or 'filter' field");
        }
        if (path == null) {
            throw new QueryParsingException(parseContext.index(), "[nested] requires 'path' field");
        }

        if (query == null && filter == null) {
            return null;
        }

        if (filter != null) {
            query = new XConstantScoreQuery(filter);
        }

        query.setBoost(boost);

        MapperService.SmartNameObjectMapper mapper = parseContext.smartObjectMapper(path);
        if (mapper == null) {
            throw new QueryParsingException(parseContext.index(),
                    "[nested] failed to find nested object under path [" + path + "]");
        }
        ObjectMapper objectMapper = mapper.mapper();
        if (objectMapper == null) {
            throw new QueryParsingException(parseContext.index(),
                    "[nested] failed to find nested object under path [" + path + "]");
        }
        if (!objectMapper.nested().isNested()) {
            throw new QueryParsingException(parseContext.index(),
                    "[nested] nested object under path [" + path + "] is not of nested type");
        }

        Filter childFilter = parseContext.cacheFilter(objectMapper.nestedTypeFilter(), null);
        usAsParentFilter.filter = childFilter;
        // wrap the child query to only work on the nested path type
        query = new XFilteredQuery(query, childFilter);

        Filter parentFilter = currentParentFilterContext;
        if (parentFilter == null) {
            parentFilter = NonNestedDocsFilter.INSTANCE;
            // don't do special parent filtering, since we might have same nested mapping on two different types
            //if (mapper.hasDocMapper()) {
            //    // filter based on the type...
            //    parentFilter = mapper.docMapper().typeFilter();
            //}
            parentFilter = parseContext.cacheFilter(parentFilter, null);
        }

        Filter nestedFilter;
        if (join) {
            ToParentBlockJoinQuery joinQuery = new ToParentBlockJoinQuery(query, parentFilter, ScoreMode.None);
            nestedFilter = new QueryWrapperFilter(joinQuery);
        } else {
            nestedFilter = new QueryWrapperFilter(query);
        }

        if (cache) {
            nestedFilter = parseContext.cacheFilter(nestedFilter, cacheKey);
        }
        if (filterName != null) {
            parseContext.addNamedFilter(filterName, nestedFilter);
        }
        return nestedFilter;
    } finally {
        // restore the thread local one...
        NestedQueryParser.parentFilterContext.set(currentParentFilterContext);
    }
}

From source file:org.elasticsearch.index.query.NestedQueryBuilder.java

License:Apache License

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    ObjectMapper nestedObjectMapper = context.getObjectMapper(path);
    if (nestedObjectMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {//from www  .  j  a  v  a 2  s .  c o m
            throw new IllegalStateException(
                    "[" + NAME + "] failed to find nested object under path [" + path + "]");
        }
    }
    if (!nestedObjectMapper.nested().isNested()) {
        throw new IllegalStateException(
                "[" + NAME + "] nested object under path [" + path + "] is not of nested type");
    }
    final BitSetProducer parentFilter;
    final Query childFilter;
    final Query innerQuery;
    ObjectMapper objectMapper = context.nestedScope().getObjectMapper();
    if (objectMapper == null) {
        parentFilter = context.bitsetFilter(Queries.newNonNestedFilter());
    } else {
        parentFilter = context.bitsetFilter(objectMapper.nestedTypeFilter());
    }
    childFilter = nestedObjectMapper.nestedTypeFilter();
    try {
        context.nestedScope().nextLevel(nestedObjectMapper);
        innerQuery = this.query.toQuery(context);
    } finally {
        context.nestedScope().previousLevel();
    }
    return new ToParentBlockJoinQuery(Queries.filtered(innerQuery, childFilter), parentFilter, scoreMode);
}