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;
}
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();
}
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());
}
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);
}
}
Aggregations