<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.xpn.xwiki.plugin.lucene;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.api.Document;

/**
 * Result of a search. The Plugin will return a collection of these for display on the search page.
 * 
 * @version $Id$
 */
public class SearchResult
{
    private String id;

    private float score;

    private String title;

    private String name;

    private String wiki;

    private String space;

    private String fullName;

    private String url;

    private String filename;

    private String[] objects;

    private String type;

    private String author;

    private String language;

    private Date date;

    private Date creationDate;

    private String creator;

    private static final Log LOG = LogFactory.getLog(SearchResult.class);

    /**
     * @todo add fallback for unknown index field names (read values into a map accessible from search results page)
     *       This would be useful for integration of external indexes where the field names dont match ours.
     * @todo TODO: to be more flexible make a factory to construct different kinds of searchresults, esp. for external
     *       indexes and custom implementations of searchresults
     */
    public SearchResult(org.apache.lucene.document.Document doc, float score, com.xpn.xwiki.api.XWiki xwiki)
    {
        this.score = score;
        this.id = doc.get(IndexFields.DOCUMENT_ID);
        this.title = doc.get(IndexFields.DOCUMENT_TITLE);
        this.name = doc.get(IndexFields.DOCUMENT_NAME);
        this.space = doc.get(IndexFields.DOCUMENT_WEB);
        this.wiki = doc.get(IndexFields.DOCUMENT_WIKI);
        this.fullName = doc.get(IndexFields.DOCUMENT_FULLNAME);
        this.type = doc.get(IndexFields.DOCUMENT_TYPE);
        this.author = doc.get(IndexFields.DOCUMENT_AUTHOR);
        this.creator = doc.get(IndexFields.DOCUMENT_CREATOR);
        this.language = doc.get(IndexFields.DOCUMENT_LANGUAGE);
        this.date = IndexFields.stringToDate(doc.get(IndexFields.DOCUMENT_DATE));
        this.creationDate = IndexFields.stringToDate(doc.get(IndexFields.DOCUMENT_CREATIONDATE));
        if (LucenePlugin.DOCTYPE_ATTACHMENT.equals(this.type)) {
            this.filename = doc.get(IndexFields.FILENAME);
            Document document;
            final String fullDocName =
                new StringBuffer(this.wiki).append(":").append(this.space).append(".").append(this.name).toString();
            try {
                document = xwiki.getDocument(fullDocName);
                this.url = document.getAttachmentURL(this.filename, "download");
            } catch (XWikiException e) {
                LOG.error("error retrieving url for attachment " + this.filename + " of document " + fullDocName, e);
            }
        } else {
            this.objects = doc.getValues("object");
        }
    }

    /**
     * @return the document id as indexed
     */
    public String getId()
    {
        return this.id;
    }

    /**
     * @return Returns the name of the user who last modified the document.
     */
    public String getAuthor()
    {
        return this.author;
    }

    /**
     * @return Returns the date of last modification.
     */
    public Date getDate()
    {<commit_msg>XPLUCENE-61: NullPointerExcept when searching an existing attachment with no right to view<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.xpn.xwiki.plugin.lucene;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.api.Document;

/**
 * Result of a search. The Plugin will return a collection of these for display on the search page.
 * 
 * @version $Id$
 */
public class SearchResult
{
    private String id;

    private float score;

    private String title;

    private String name;

    private String wiki;

    private String space;

    private String fullName;

    private String url;

    private String filename;

    private String[] objects;

    private String type;

    private String author;

    private String language;

    private Date date;

    private Date creationDate;

    private String creator;

    private static final Log LOG = LogFactory.getLog(SearchResult.class);

    /**
     * @todo add fallback for unknown index field names (read values into a map accessible from search results page)
     *       This would be useful for integration of external indexes where the field names dont match ours.
     * @todo TODO: to be more flexible make a factory to construct different kinds of searchresults, esp. for external
     *       indexes and custom implementations of searchresults
     */
    public SearchResult(org.apache.lucene.document.Document doc, float score, com.xpn.xwiki.api.XWiki xwiki)
    {
        this.score = score;
        this.id = doc.get(IndexFields.DOCUMENT_ID);
        this.title = doc.get(IndexFields.DOCUMENT_TITLE);
        this.name = doc.get(IndexFields.DOCUMENT_NAME);
        this.space = doc.get(IndexFields.DOCUMENT_SPACE);
        this.wiki = doc.get(IndexFields.DOCUMENT_WIKI);
        this.fullName = doc.get(IndexFields.DOCUMENT_FULLNAME);
        this.type = doc.get(IndexFields.DOCUMENT_TYPE);
        this.author = doc.get(IndexFields.DOCUMENT_AUTHOR);
        this.creator = doc.get(IndexFields.DOCUMENT_CREATOR);
        this.language = doc.get(IndexFields.DOCUMENT_LANGUAGE);
        this.date = IndexFields.stringToDate(doc.get(IndexFields.DOCUMENT_DATE));
        this.creationDate = IndexFields.stringToDate(doc.get(IndexFields.DOCUMENT_CREATIONDATE));
        if (LucenePlugin.DOCTYPE_ATTACHMENT.equals(this.type)) {
            this.filename = doc.get(IndexFields.FILENAME);
            Document document;
            final String fullDocName =
                new StringBuffer(this.wiki).append(":").append(this.space).append(".").append(this.name).toString();
            try {
                document = xwiki.getDocument(fullDocName);
                if (document != null) {
                    this.url = document.getAttachmentURL(this.filename, "download");
                }
            } catch (XWikiException e) {
                LOG.error("error retrieving url for attachment [" + this.filename + "] of document [" + fullDocName + "]", e);
            }
        } else {
            this.objects = doc.getValues("object");
        }
    }

    /**
     * @return the document id as indexed
     */
    public String getId()
    {
        return this.id;
    }

    /**
     * @return Returns the name of the user who last modified the document.
     */
    public String getAuthor()
    {
        return this.author;
    }

    /**
     * @return Returns the date of last modification.
     */
    public Date getDate()
    {
{'fraction_non_alphanumeric': 0.08706180811808117, 'fraction_numerical': 0.0032287822878228783, 'mean_word_length': 2.7594278283485045, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7253338', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2302, 'n_words': 788}