Search in sources :

Example 1 with LuceneQuery

use of com.celements.search.lucene.query.LuceneQuery in project celements-blog by celements.

the class ArticleLuceneQueryBuilder method build.

@Override
public LuceneQuery build(ArticleLoadParameter param) throws XWikiException {
    LuceneQuery query = null;
    if (param.getBlogDocRef() != null) {
        QueryRestrictionGroup blogOrSubsGrp = searchService.createRestrictionGroup(Type.OR);
        blogOrSubsGrp.add(getBlogRestriction(param));
        blogOrSubsGrp.add(getSubsRestrictions(param));
        if (!blogOrSubsGrp.isEmpty()) {
            WikiReference wikiRef = param.getBlogDocRef().getWikiReference();
            query = searchService.createQuery();
            query.setWiki(wikiRef);
            query.add(getBlogSearchTermRestriction(param));
            DocumentReference articleClassRef = getBlogClasses().getArticleClassRef(wikiRef.getName());
            query.add(searchService.createObjectRestriction(articleClassRef));
            if (StringUtils.isNotBlank(param.getLanguage())) {
                query.add(searchService.createFieldRestriction(articleClassRef, BlogClasses.PROPERTY_ARTICLE_LANG, "\"" + param.getLanguage() + "\""));
            }
            query.add(blogOrSubsGrp);
        }
    }
    LOGGER.info("Built '" + query + "' for '" + param + "'");
    return query;
}
Also used : LuceneQuery(com.celements.search.lucene.query.LuceneQuery) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) QueryRestrictionGroup(com.celements.search.lucene.query.QueryRestrictionGroup)

Example 2 with LuceneQuery

use of com.celements.search.lucene.query.LuceneQuery in project celements-blog by celements.

the class ArticleEngineLuceneTest method testGetArticles_LSE.

@Test
public void testGetArticles_LSE() throws Exception {
    ArticleLoadParameter param = new ArticleLoadParameter();
    List<String> sortFields = Arrays.asList("field1", "field2");
    param.setSortFields(sortFields);
    String language = "de";
    param.setLanguage(language);
    int offset = 5;
    param.setOffset(offset);
    int limit = 10;
    param.setLimit(limit);
    LuceneQuery query = new LuceneQuery(Arrays.asList(LucenePlugin.DOCTYPE_WIKIPAGE));
    LuceneSearchResult resultMock = createMockAndAddToDefault(LuceneSearchResult.class);
    Throwable cause = createMockAndAddToDefault(LuceneSearchException.class);
    expect(queryBuilderMock.build(same(param))).andReturn(query).once();
    expect(searchServiceMock.searchWithoutChecks(same(query), eq(sortFields), eq(Arrays.asList("default", language)))).andReturn(resultMock).once();
    expect(resultMock.setOffset(eq(offset))).andReturn(resultMock).once();
    expect(resultMock.setLimit(eq(limit))).andReturn(resultMock).once();
    expect(resultMock.getResults()).andThrow(cause).once();
    replayDefault();
    try {
        engine.getArticles(param);
        fail("expecting ALE");
    } catch (ArticleLoadException ale) {
        assertSame(cause, ale.getCause());
    }
    verifyDefault();
}
Also used : LuceneQuery(com.celements.search.lucene.query.LuceneQuery) LuceneSearchResult(com.celements.search.lucene.LuceneSearchResult) AbstractComponentTest(com.celements.common.test.AbstractComponentTest) Test(org.junit.Test)

Example 3 with LuceneQuery

use of com.celements.search.lucene.query.LuceneQuery in project celements-blog by celements.

the class ArticleEngineLuceneTest method testGetArticles.

@Test
public void testGetArticles() throws Exception {
    ArticleLoadParameter param = new ArticleLoadParameter();
    List<String> sortFields = Arrays.asList("field1", "field2");
    param.setSortFields(sortFields);
    String language = "de";
    param.setLanguage(language);
    int offset = 5;
    param.setOffset(offset);
    int limit = 10;
    param.setLimit(limit);
    LuceneQuery query = new LuceneQuery(Arrays.asList(LucenePlugin.DOCTYPE_WIKIPAGE));
    LuceneSearchResult resultMock = createMockAndAddToDefault(LuceneSearchResult.class);
    DocumentReference artDocRef = new DocumentReference("wiki", "blogSpace", "article");
    XWikiDocument artDoc = new XWikiDocument(artDocRef);
    artDoc.setSyntax(Syntax.XWIKI_1_0);
    AttachmentReference attRef = new AttachmentReference("file", artDocRef);
    expect(queryBuilderMock.build(same(param))).andReturn(query).once();
    expect(searchServiceMock.searchWithoutChecks(same(query), eq(sortFields), eq(Arrays.asList("default", language)))).andReturn(resultMock).once();
    expect(resultMock.setOffset(eq(offset))).andReturn(resultMock).once();
    expect(resultMock.setLimit(eq(limit))).andReturn(resultMock).once();
    expect(getWikiMock().getDocument(eq(artDocRef), same(getContext()))).andReturn(artDoc).once();
    expect(resultMock.getResults()).andReturn(Arrays.<EntityReference>asList(artDocRef, attRef)).once();
    replayDefault();
    List<Article> ret = engine.getArticles(param);
    verifyDefault();
    assertNotNull(ret);
    // empty because of EmptyArticleException
    assertEquals(0, ret.size());
}
Also used : LuceneQuery(com.celements.search.lucene.query.LuceneQuery) AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) EntityReference(org.xwiki.model.reference.EntityReference) LuceneSearchResult(com.celements.search.lucene.LuceneSearchResult) DocumentReference(org.xwiki.model.reference.DocumentReference) AbstractComponentTest(com.celements.common.test.AbstractComponentTest) Test(org.junit.Test)

Example 4 with LuceneQuery

use of com.celements.search.lucene.query.LuceneQuery in project celements-blog by celements.

the class ArticleEngineLucene method getArticles.

@Override
public List<Article> getArticles(ArticleLoadParameter param) throws ArticleLoadException {
    try {
        List<Article> articles = new ArrayList<>();
        LuceneQuery query = queryBuilder.build(param);
        if (query != null) {
            LuceneSearchResult result = searchService.searchWithoutChecks(query, param.getSortFields(), Arrays.asList("default", param.getLanguage()));
            result.setOffset(param.getOffset()).setLimit(param.getLimit());
            for (EntityReference ref : result.getResults()) {
                if (ref instanceof DocumentReference) {
                    XWikiDocument doc = getContext().getWiki().getDocument((DocumentReference) ref, getContext());
                    try {
                        articles.add(new Article(doc, getContext()));
                    } catch (EmptyArticleException exc) {
                        LOGGER.warn("getArticles: empty article '{}'", exc, doc);
                    }
                } else {
                    LOGGER.warn("getArticles: not expecting Attachment as search result '{}' " + "for search '{}'", ref, param);
                }
            }
        } else {
            LOGGER.warn("got null from query builder for '" + param + "'");
        }
        return articles;
    } catch (LuceneSearchException lse) {
        throw new ArticleLoadException(lse);
    } catch (XWikiException xwe) {
        throw new ArticleLoadException(xwe);
    }
}
Also used : LuceneQuery(com.celements.search.lucene.query.LuceneQuery) EmptyArticleException(com.celements.blog.plugin.EmptyArticleException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) EntityReference(org.xwiki.model.reference.EntityReference) LuceneSearchException(com.celements.search.lucene.LuceneSearchException) LuceneSearchResult(com.celements.search.lucene.LuceneSearchResult) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

LuceneQuery (com.celements.search.lucene.query.LuceneQuery)4 LuceneSearchResult (com.celements.search.lucene.LuceneSearchResult)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 AbstractComponentTest (com.celements.common.test.AbstractComponentTest)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 Test (org.junit.Test)2 EntityReference (org.xwiki.model.reference.EntityReference)2 EmptyArticleException (com.celements.blog.plugin.EmptyArticleException)1 LuceneSearchException (com.celements.search.lucene.LuceneSearchException)1 QueryRestrictionGroup (com.celements.search.lucene.query.QueryRestrictionGroup)1 XWikiException (com.xpn.xwiki.XWikiException)1 ArrayList (java.util.ArrayList)1 AttachmentReference (org.xwiki.model.reference.AttachmentReference)1 WikiReference (org.xwiki.model.reference.WikiReference)1