Search in sources :

Example 1 with CustomScoreProvider

use of org.apache.lucene.queries.CustomScoreProvider in project jackrabbit-oak by apache.

the class LuceneIndexTest method customScoreQuery.

@Test
public void customScoreQuery() throws Exception {
    NodeBuilder nb = newLuceneIndexDefinition(builder.child(INDEX_DEFINITIONS_NAME), "lucene", of(TYPENAME_STRING));
    TestUtil.useV2(nb);
    nb.setProperty(LuceneIndexConstants.PROP_SCORER_PROVIDER, "testScorer");
    NodeState before = builder.getNodeState();
    builder.child("a").setProperty("jcr:createdBy", "bar bar");
    builder.child("b").setProperty("jcr:createdBy", "foo bar");
    NodeState after = builder.getNodeState();
    NodeState indexed = HOOK.processCommit(before, after, CommitInfo.EMPTY);
    tracker = new IndexTracker();
    tracker.update(indexed);
    SimpleScorerFactory factory = new SimpleScorerFactory();
    ScorerProvider provider = new ScorerProvider() {

        String scorerName = "testScorer";

        @Override
        public String getName() {
            return scorerName;
        }

        @Override
        public CustomScoreQuery createCustomScoreQuery(Query query) {
            return new ModifiedCustomScoreQuery(query);
        }

        class ModifiedCustomScoreQuery extends CustomScoreQuery {

            private Query query;

            public ModifiedCustomScoreQuery(Query query) {
                super(query);
                this.query = query;
            }

            @Override
            public CustomScoreProvider getCustomScoreProvider(AtomicReaderContext context) {
                return new CustomScoreProvider(context) {

                    public float customScore(int doc, float subQueryScore, float valSrcScore) {
                        AtomicReader atomicReader = context.reader();
                        try {
                            Document document = atomicReader.document(doc);
                            // boosting docs created by foo
                            String fieldValue = document.get("full:jcr:createdBy");
                            if (fieldValue != null && fieldValue.contains("foo")) {
                                valSrcScore *= 2.0;
                            }
                        } catch (IOException e) {
                            return subQueryScore * valSrcScore;
                        }
                        return subQueryScore * valSrcScore;
                    }
                };
            }
        }
    };
    factory.providers.put(provider.getName(), provider);
    AdvancedQueryIndex queryIndex = new LucenePropertyIndex(tracker, factory);
    FilterImpl filter = createFilter(NT_BASE);
    filter.setFullTextConstraint(new FullTextTerm(null, "bar", false, false, null));
    assertFilter(filter, queryIndex, indexed, asList("/b", "/a"), true);
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) FilterImpl(org.apache.jackrabbit.oak.query.index.FilterImpl) Query(org.apache.lucene.search.Query) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) AtomicReader(org.apache.lucene.index.AtomicReader) IOException(java.io.IOException) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Document(org.apache.lucene.document.Document) AdvancedQueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex) ScorerProvider(org.apache.jackrabbit.oak.plugins.index.lucene.score.ScorerProvider) FullTextTerm(org.apache.jackrabbit.oak.query.fulltext.FullTextTerm) CustomScoreProvider(org.apache.lucene.queries.CustomScoreProvider) AtomicReaderContext(org.apache.lucene.index.AtomicReaderContext) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)1 ScorerProvider (org.apache.jackrabbit.oak.plugins.index.lucene.score.ScorerProvider)1 FullTextTerm (org.apache.jackrabbit.oak.query.fulltext.FullTextTerm)1 FilterImpl (org.apache.jackrabbit.oak.query.index.FilterImpl)1 AdvancedQueryIndex (org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex)1 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)1 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)1 Document (org.apache.lucene.document.Document)1 AtomicReader (org.apache.lucene.index.AtomicReader)1 AtomicReaderContext (org.apache.lucene.index.AtomicReaderContext)1 CustomScoreProvider (org.apache.lucene.queries.CustomScoreProvider)1 CustomScoreQuery (org.apache.lucene.queries.CustomScoreQuery)1 Query (org.apache.lucene.search.Query)1 Test (org.junit.Test)1