Search in sources :

Example 6 with SearchResult

use of org.xwiki.rest.model.jaxb.SearchResult in project xwiki-platform by xwiki.

the class SOLRSearchSource method search.

@Override
public List<SearchResult> search(String queryString, String defaultWikiName, String wikis, boolean hasProgrammingRights, String orderField, String order, boolean distinct, int number, int start, Boolean withPrettyNames, String className, UriInfo uriInfo) throws Exception {
    List<SearchResult> result = new ArrayList<SearchResult>();
    if (queryString == null) {
        return result;
    }
    /*
         * One of the two must be non-null. If default wiki name is non-null and wikis is null, then it's a local search
         * in a specific wiki. If wiki name is null and wikis is non-null it's a global query on different wikis. If
         * both of them are non-null then the wikis parameter takes the precedence.
         */
    if (defaultWikiName == null && wikis == null) {
        return result;
    }
    Query query = this.queryManager.createQuery(queryString, SolrQueryExecutor.SOLR);
    if (query instanceof SecureQuery) {
        // Show only what the current user has the right to see
        ((SecureQuery) query).checkCurrentUser(true);
    }
    List<String> fq = new ArrayList<String>();
    // We want only documents
    fq.add("{!tag=type}type:(\"DOCUMENT\")");
    // Additional filter for non PR users
    if (!hasProgrammingRights) {
        fq.add("{!tag=hidden}hidden:(false)");
    }
    // Wikis
    if (StringUtils.isNotBlank(wikis)) {
        String[] strings = StringUtils.split(wikis, ',');
        if (strings.length == 1) {
            fq.add("{!tag=wiki}wiki:(\"" + strings[0] + "\")");
        } else if (strings.length > 1) {
            StringBuilder builder = new StringBuilder();
            for (String str : strings) {
                if (builder.length() > 0) {
                    builder.append(" OR ");
                }
                builder.append('\'');
                builder.append(str);
                builder.append('\'');
            }
            fq.add("{!tag=wiki}wiki:(" + builder + ")");
        }
    }
    // TODO: current locale filtering ?
    query.bindValue("fq", fq);
    // Boost
    // FIXME: take it from configuration
    query.bindValue("qf", "title^10.0 name^10.0 doccontent^2.0 objcontent^0.4 filename^0.4 attcontent^0.4 doccontentraw^0.4 " + "author_display^0.08 creator_display^0.08 " + "comment^0.016 attauthor_display^0.016 space^0.016");
    // Order
    if (!StringUtils.isBlank(orderField)) {
        if ("desc".equals(order)) {
            query.bindValue("sort", orderField + " desc");
        } else {
            query.bindValue("sort", orderField + " asc");
        }
    }
    // Limit
    query.setLimit(number).setOffset(start);
    try {
        QueryResponse response = (QueryResponse) query.execute().get(0);
        SolrDocumentList documents = response.getResults();
        for (SolrDocument document : documents) {
            SearchResult searchResult = this.objectFactory.createSearchResult();
            DocumentReference documentReference = this.solrDocumentReferenceResolver.resolve(document);
            searchResult.setPageFullName(this.localEntityReferenceSerializer.serialize(documentReference));
            searchResult.setWiki(documentReference.getWikiReference().getName());
            searchResult.setSpace(this.localEntityReferenceSerializer.serialize(documentReference.getParent()));
            searchResult.setPageName(documentReference.getName());
            searchResult.setVersion((String) document.get(FieldUtils.VERSION));
            searchResult.setType("page");
            searchResult.setId(Utils.getPageId(searchResult.getWiki(), Utils.getSpacesFromSpaceId(searchResult.getSpace()), searchResult.getPageName()));
            searchResult.setScore(((Number) document.get(FieldUtils.SCORE)).floatValue());
            searchResult.setAuthor((String) document.get(FieldUtils.AUTHOR));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime((Date) document.get(FieldUtils.DATE));
            searchResult.setModified(calendar);
            if (withPrettyNames) {
                searchResult.setAuthorName((String) document.get(FieldUtils.AUTHOR_DISPLAY));
            }
            Locale docLocale = LocaleUtils.toLocale((String) document.get(FieldUtils.DOCUMENT_LOCALE));
            Locale locale = LocaleUtils.toLocale((String) document.get(FieldUtils.LOCALE));
            searchResult.setTitle((String) document.getFirstValue(FieldUtils.getFieldName(FieldUtils.TITLE, locale)));
            List<String> spaces = Utils.getSpacesHierarchy(documentReference.getLastSpaceReference());
            String pageUri = null;
            if (Locale.ROOT == docLocale) {
                pageUri = Utils.createURI(uriInfo.getBaseUri(), PageResource.class, searchResult.getWiki(), spaces, searchResult.getPageName()).toString();
            } else {
                searchResult.setLanguage(docLocale.toString());
                pageUri = Utils.createURI(uriInfo.getBaseUri(), PageTranslationResource.class, spaces, searchResult.getPageName(), docLocale).toString();
            }
            Link pageLink = new Link();
            pageLink.setHref(pageUri);
            pageLink.setRel(Relations.PAGE);
            searchResult.getLinks().add(pageLink);
            result.add(searchResult);
        }
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI, XWikiException.ERROR_XWIKI_UNKNOWN, "Error performing solr search", e);
    }
    return result;
}
Also used : Locale(java.util.Locale) Query(org.xwiki.query.Query) SecureQuery(org.xwiki.query.SecureQuery) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) SearchResult(org.xwiki.rest.model.jaxb.SearchResult) SolrDocumentList(org.apache.solr.common.SolrDocumentList) XWikiException(com.xpn.xwiki.XWikiException) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SecureQuery(org.xwiki.query.SecureQuery) DocumentReference(org.xwiki.model.reference.DocumentReference) Link(org.xwiki.rest.model.jaxb.Link) XWikiException(com.xpn.xwiki.XWikiException)

Example 7 with SearchResult

use of org.xwiki.rest.model.jaxb.SearchResult in project xwiki-platform by xwiki.

the class SpacesResourceTest method testSearch.

@Test
public void testSearch() throws Exception {
    DocumentReference reference = new DocumentReference(getWiki(), getTestClassName(), getTestMethodName());
    this.testUtils.rest().delete(reference);
    this.testUtils.rest().savePage(reference, "content " + getTestMethodName(), "title " + getTestMethodName());
    GetMethod getMethod = executeGet(String.format("%s?q=somethingthatcannotpossiblyexist", buildURI(SpaceSearchResource.class, getWiki(), Arrays.asList(getTestClassName()))));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(0, searchResults.getSearchResults().size());
    getMethod = executeGet(String.format("%s?q=%s", buildURI(SpaceSearchResource.class, getWiki(), Arrays.asList(getTestClassName())), getTestMethodName()));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    int resultSize = searchResults.getSearchResults().size();
    Assert.assertTrue("Found " + resultSize + " result", resultSize == 1);
    for (SearchResult searchResult : searchResults.getSearchResults()) {
        checkLinks(searchResult);
    }
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) SearchResult(org.xwiki.rest.model.jaxb.SearchResult) SearchResults(org.xwiki.rest.model.jaxb.SearchResults) DocumentReference(org.xwiki.model.reference.DocumentReference) SpaceSearchResource(org.xwiki.rest.resources.spaces.SpaceSearchResource) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 8 with SearchResult

use of org.xwiki.rest.model.jaxb.SearchResult in project xwiki-platform by xwiki.

the class WikisResourceTest method testSearchWikis.

@Test
public void testSearchWikis() throws Exception {
    this.testUtils.rest().delete(reference);
    this.testUtils.rest().savePage(reference, "content" + this.pageName, "title" + this.pageName);
    GetMethod getMethod = executeGet(String.format("%s?q=content" + this.pageName, buildURI(WikiSearchResource.class, getWiki())));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    int resultSize = searchResults.getSearchResults().size();
    assertEquals(1, resultSize);
    for (SearchResult searchResult : searchResults.getSearchResults()) {
        checkLinks(searchResult);
    }
    getMethod = executeGet(String.format("%s?q=" + this.pageName + "&scope=name", buildURI(WikiSearchResource.class, getWiki())));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    resultSize = searchResults.getSearchResults().size();
    assertEquals(1, resultSize);
    for (SearchResult searchResult : searchResults.getSearchResults()) {
        checkLinks(searchResult);
    }
    // Search in titles
    getMethod = executeGet(String.format("%s?q=title" + this.pageName + "&scope=title", buildURI(WikiSearchResource.class, getWiki())));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    resultSize = searchResults.getSearchResults().size();
    assertEquals(1, resultSize);
    for (SearchResult searchResult : searchResults.getSearchResults()) {
        checkLinks(searchResult);
    }
    // Search for space names
    getMethod = executeGet(String.format("%s?q=" + this.spaces.get(0) + "&scope=spaces", buildURI(WikiSearchResource.class, getWiki())));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    resultSize = searchResults.getSearchResults().size();
    assertEquals(1, resultSize);
    for (SearchResult searchResult : searchResults.getSearchResults()) {
        checkLinks(searchResult);
    }
}
Also used : WikiSearchResource(org.xwiki.rest.resources.wikis.WikiSearchResource) GetMethod(org.apache.commons.httpclient.methods.GetMethod) SearchResult(org.xwiki.rest.model.jaxb.SearchResult) SearchResults(org.xwiki.rest.model.jaxb.SearchResults) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 9 with SearchResult

use of org.xwiki.rest.model.jaxb.SearchResult in project xwiki-platform by xwiki.

the class WikiManagerRestTest method testMultiwikiSearch.

// FIXME: Test is disabled for the moment. It works if tested against MySQL but with HSQLDB it seems that the
// Lucene plugin is not triggered. Anyway this should be better to rewrite it, if possible, as a unit test.
@Ignore("This test doesn't seem to work correctly with HSQLDB but it actually works if run against MySQL.")
@Test
public void testMultiwikiSearch() throws Exception {
    String WIKI1_ID = "w1";
    String WIKI2_ID = "w2";
    String PAGE_SPACE = "Main";
    String PAGE_NAME = "Test";
    String PAGE1_STRING = "foo";
    String PAGE2_STRING = "bar";
    Wiki wiki = objectFactory.createWiki();
    wiki.setId(WIKI1_ID);
    PostMethod postMethod = executePost(getFullUri(WikiManagerREST.class), "superadmin", "pass", wiki);
    Assert.assertEquals(HttpStatus.SC_CREATED, postMethod.getStatusCode());
    wiki = objectFactory.createWiki();
    wiki.setId(WIKI2_ID);
    postMethod = executePost(getFullUri(WikiManagerREST.class), "superadmin", "pass", wiki);
    Assert.assertEquals(HttpStatus.SC_CREATED, postMethod.getStatusCode());
    /* Store the page */
    Page page1 = objectFactory.createPage();
    page1.setTitle(PAGE1_STRING);
    page1.setContent(PAGE1_STRING);
    PutMethod putMethod = executePut(getUriBuilder(PageResource.class).build(WIKI1_ID, PAGE_SPACE, PAGE_NAME).toString(), "superadmin", "pass", page1);
    Assert.assertEquals(HttpStatus.SC_CREATED, putMethod.getStatusCode());
    page1 = (Page) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
    /* Retrieve the page to check that it exists */
    GetMethod getMethod = executeGet(getUriBuilder(PageResource.class).build(WIKI1_ID, PAGE_SPACE, PAGE_NAME).toString());
    Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
    Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(WIKI1_ID, page.getWiki());
    Assert.assertEquals(PAGE_SPACE, page.getSpace());
    Assert.assertEquals(PAGE_NAME, page.getName());
    Assert.assertEquals(PAGE1_STRING, page.getTitle());
    Assert.assertEquals(PAGE1_STRING, page.getContent());
    Assert.assertEquals(page1.getCreated(), page.getCreated());
    Assert.assertEquals(page1.getModified(), page.getModified());
    /* Store the page */
    Page page2 = objectFactory.createPage();
    page2.setTitle(PAGE2_STRING);
    page2.setContent(PAGE2_STRING);
    putMethod = executePut(getUriBuilder(PageResource.class).build(WIKI2_ID, PAGE_SPACE, PAGE_NAME).toString(), "superadmin", "pass", page2);
    Assert.assertEquals(HttpStatus.SC_CREATED, putMethod.getStatusCode());
    page2 = (Page) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
    /* Retrieve the page to check that it exists */
    getMethod = executeGet(getUriBuilder(PageResource.class).build(WIKI2_ID, PAGE_SPACE, PAGE_NAME).toString());
    Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
    page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(WIKI2_ID, page.getWiki());
    Assert.assertEquals(PAGE_SPACE, page.getSpace());
    Assert.assertEquals(PAGE_NAME, page.getName());
    Assert.assertEquals(PAGE2_STRING, page.getTitle());
    Assert.assertEquals(PAGE2_STRING, page.getContent());
    Assert.assertEquals(page2.getCreated(), page.getCreated());
    Assert.assertEquals(page2.getModified(), page.getModified());
    /* Wait a bit that the Lucene Indexer indexes the pages. */
    Thread.sleep(5000);
    getMethod = executeGet(URIUtil.encodeQuery(String.format("%s?q=\"%s\"&wikis=w1,w2", getFullUri(WikisSearchQueryResource.class), PAGE_NAME)));
    Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
    SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(2, searchResults.getSearchResults().size());
    for (SearchResult searchResult : searchResults.getSearchResults()) {
        Page pageToBeCheckedAgainst = null;
        if (searchResult.getWiki().equals(WIKI1_ID)) {
            pageToBeCheckedAgainst = page1;
        } else {
            pageToBeCheckedAgainst = page2;
        }
        Assert.assertEquals(pageToBeCheckedAgainst.getWiki(), searchResult.getWiki());
        Assert.assertEquals(pageToBeCheckedAgainst.getTitle(), searchResult.getTitle());
        Assert.assertEquals(pageToBeCheckedAgainst.getAuthor(), searchResult.getAuthor());
        Assert.assertEquals(pageToBeCheckedAgainst.getModified(), searchResult.getModified());
        Assert.assertEquals(pageToBeCheckedAgainst.getVersion(), searchResult.getVersion());
    }
}
Also used : PageResource(org.xwiki.rest.resources.pages.PageResource) PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Wiki(org.xwiki.rest.model.jaxb.Wiki) WikiManagerREST(org.xwiki.wiki.rest.WikiManagerREST) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Page(org.xwiki.rest.model.jaxb.Page) SearchResult(org.xwiki.rest.model.jaxb.SearchResult) SearchResults(org.xwiki.rest.model.jaxb.SearchResults) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

SearchResult (org.xwiki.rest.model.jaxb.SearchResult)9 ArrayList (java.util.ArrayList)5 Link (org.xwiki.rest.model.jaxb.Link)5 Document (com.xpn.xwiki.api.Document)4 XWiki (com.xpn.xwiki.api.XWiki)4 Calendar (java.util.Calendar)4 GetMethod (org.apache.commons.httpclient.methods.GetMethod)3 Test (org.junit.Test)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 SearchResults (org.xwiki.rest.model.jaxb.SearchResults)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 Formatter (java.util.Formatter)2 Query (org.xwiki.query.Query)2 PageResource (org.xwiki.rest.resources.pages.PageResource)2 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)2 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 Locale (java.util.Locale)1 PostMethod (org.apache.commons.httpclient.methods.PostMethod)1