r1688 - in xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/test/java/org/gelc/xwiki/plugins: . lucene
congruent
congruent at users.forge.objectweb.org
Wed Dec 6 22:41:53 CET 2006
Author: congruent
Date: 2006-12-06 22:41:53 +0100 (Wed, 06 Dec 2006)
New Revision: 1688
Added:
xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/test/java/org/gelc/xwiki/plugins/lucene/
xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/test/java/org/gelc/xwiki/plugins/lucene/LucenePluginTest.java
xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/test/java/org/gelc/xwiki/plugins/lucene/LuceneTest.java
Log:
CURRIKI-10 Support XWiki Objects as searchable fields in Lucene Search
add the tests
Added: xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/test/java/org/gelc/xwiki/plugins/lucene/LucenePluginTest.java
===================================================================
--- xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/test/java/org/gelc/xwiki/plugins/lucene/LucenePluginTest.java 2006-12-06 16:50:25 UTC (rev 1687)
+++ xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/test/java/org/gelc/xwiki/plugins/lucene/LucenePluginTest.java 2006-12-06 21:41:53 UTC (rev 1688)
@@ -0,0 +1,435 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * 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.
+ *
+ * @author lokesh (N.Lokeswara Reddy) Congruent Solutions Pvt.Ltd.
+ */
+package net.jkraemer.xwiki.plugins.lucene;
+
+import com.xpn.xwiki.test.HibernateTestCase;
+import com.xpn.xwiki.doc.XWikiAttachment;
+import com.xpn.xwiki.doc.XWikiDocument;
+import com.xpn.xwiki.web.XWikiServletURLFactory;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.objects.classes.BaseClass;
+import com.xpn.xwiki.objects.BaseObject;
+import com.xpn.xwiki.api.XWiki;
+
+import java.net.URL;
+import java.util.*;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.search.*;
+import org.apache.lucene.queryParser.ParseException;
+import org.apache.lucene.queryParser.QueryParser;
+
+public class LucenePluginTest extends HibernateTestCase {
+
+ String author = "XWiki.LudovicDubost";
+ String parent = "Main.WebHome";
+ String parent1 = "Main.Lokesh";
+
+ String indexDir;
+
+ /**
+ * This is to set up the plugin and required configurable items
+ *
+ * @throws Exception
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ // Setting the configuration properties to the XWiki Config
+ getXWikiConfig().setProperty("xwiki.plugins.lucene.indexdir", "C:\\luceneindex\\");
+ getXWikiConfig().setProperty("xwiki.plugins.lucene.analyzer", "org.apache.lucene.analysis.standard.StandardAnalyzer");
+ getXWikiConfig().setProperty("xwiki.plugins.lucene.indexinterval", "2");
+ indexDir = getXWikiConfig().getProperty("xwiki.plugins.lucene.indexdir");
+
+ XWikiDocument doc = xwiki.getDocument("XWiki.ArticleClass", context);
+
+ doc.setContent("Here is some text");
+ BaseClass bclass = doc.getxWikiClass();
+ bclass.addTextField("title", "Title", 30);
+ bclass.addTextAreaField("content", "Content", 80, 10);
+ bclass.addTextAreaField("extract", "Extract", 80, 5);
+ bclass.addDateField("date", "Creation Date","ddmmyyyy");
+ bclass.addStaticListField("category", "Category", 5, true, "categoryone|categorytwo");
+ bclass.addStaticListField("complexlist", "Complex List", 5, true, "1=valueone|2=valuetwo");
+ getXWiki().saveDocument(doc, context);
+ }
+
+ /**
+ * @param query
+ * @param virtualWikiNames comma separated list of virtual wiki names
+ * @param languages comma separated list of language codes to search in, may be
+ * null to search all languages
+ * @throws ParseException
+ */
+ public Query buildQuery(String query, String virtualWikiNames, String languages) throws ParseException {
+ // build a query like this: <user query string> AND <wikiNamesQuery> AND
+ // <languageQuery>
+ BooleanQuery bQuery = new BooleanQuery();
+ QueryParser qParser = new QueryParser(IndexFields.FULLTEXT, new StandardAnalyzer());
+ qParser.setDefaultOperator(QueryParser.Operator.AND);
+ Query parsedQuery = qParser.parse(query);
+ bQuery.add(parsedQuery, BooleanClause.Occur.MUST);
+ if (virtualWikiNames != null && virtualWikiNames.length() > 0) {
+ bQuery.add(buildOredTermQuery(virtualWikiNames, IndexFields.DOCUMENT_WIKI), BooleanClause.Occur.MUST);
+ }
+ if (languages != null && languages.length() > 0) {
+ bQuery.add(buildOredTermQuery(languages, IndexFields.DOCUMENT_LANGUAGE), BooleanClause.Occur.MUST);
+ }
+ return bQuery;
+ }
+
+ /**
+ * @param values comma separated list of values to look for
+ * @return A query returning documents matching one of the given values in
+ * the given field
+ */
+ public Query buildOredTermQuery(final String values, final String fieldname) {
+ String[] valueArray = values.split("\\,");
+ if (valueArray.length > 1) {
+ // build a query like this: <valueArray[0]> OR <valueArray[1]> OR ...
+ BooleanQuery orQuery = new BooleanQuery();
+ for (int i = 0; i < valueArray.length; i++) {
+ orQuery.add(new TermQuery(new Term(fieldname, valueArray[i].trim())), BooleanClause.Occur.SHOULD);
+ }
+ return orQuery;
+ }
+ // exactly one value, no OR'ed Terms necessary
+ return new TermQuery(new Term(fieldname, valueArray[0]));
+ }
+
+
+ /**
+ * This is to add documents to the context
+ *
+ * @param noofdocuments (Number of documents to create and save in ontext)
+ * @param addAttachements (Need to attache the file for the created documents)
+ * @throws XWikiException
+ */
+ private void addDocuments(int noofdocuments, boolean addAttachements) throws XWikiException {
+ XWikiDocument doc;
+ for (int i = 0; i < noofdocuments; i++) {
+ doc = new XWikiDocument("Main", "TestLuceneDocument" + i);
+ doc.setContent(" Test lucene document only " + i);
+ doc.setAuthor(author);
+ doc.setParent(parent);
+ if (addAttachements) {
+ addAttachments(doc, "attachFile" + i + ".ppt", "Content for the attachment file " + i);
+ }
+ xwiki.saveDocument(doc, context);
+ }
+ }
+
+ /**
+ * This is to add given filename with the given content as an attachment file to the document passed on.
+ *
+ * @param doc
+ * @param fileName
+ * @param content
+ * @throws XWikiException
+ */
+ private void addAttachments(XWikiDocument doc, String fileName, String content) throws XWikiException {
+ XWikiAttachment attachment = new XWikiAttachment(doc, fileName);
+ attachment.setContent(content.getBytes());
+ attachment.setDate(new Date());
+ doc.saveAttachmentContent(attachment, context);
+ doc.getAttachmentList().add(attachment);
+ }
+
+ /**
+ * Testing Lucene indexing of the documents only
+ *
+ * @throws XWikiException
+ */
+ public void testDocumentIndexingWithXWikiContext() throws XWikiException, InterruptedException {
+ getXWiki().getPluginManager().addPlugin("lucene", "net.jkraemer.xwiki.plugins.lucene.LucenePlugin", getXWikiContext());
+ addDocuments(6, false);
+ LucenePlugin lpa = (LucenePlugin) xwiki.getPlugin("lucene", context);
+ com.xpn.xwiki.XWiki xwiki = context.getWiki();
+ XWiki xwikiApi = new XWiki(getXWikiContext().getWiki(), context);
+ assertNotNull("The Lucene plugin reference is :", lpa);
+ int count = lpa.rebuildIndex(xwikiApi, context);
+ Thread.sleep(8000);
+ assertEquals("The expected count is :", 7, count);
+ }
+
+
+ /**
+ * Testing Lucene indexing of the documents along with attachments
+ *
+ * @throws XWikiException
+ */
+ public void testDocumentWithAttachmentsIndexing() throws XWikiException, InterruptedException {
+ getXWiki().getPluginManager().addPlugin("lucene", "net.jkraemer.xwiki.plugins.lucene.LucenePlugin", getXWikiContext());
+ XWikiDocument document;
+ int expectedCount = 6;
+ Collection docNames;
+ addDocuments(expectedCount, true);
+
+ getXWiki().getPluginManager().addPlugin("lucene", "net.jkraemer.xwiki.plugins.lucene.LucenePlugin", getXWikiContext());
+ LucenePlugin lpa = (LucenePlugin) xwiki.getPlugin("lucene", context);
+ assertNotNull("The Lucene plugin reference is :", lpa);
+ com.xpn.xwiki.XWiki xwiki = context.getWiki();
+ XWiki xwikiApi = new XWiki(getXWikiContext().getWiki(), context);
+
+ docNames = xwiki.getStore().searchDocuments("", context);
+
+ for (Iterator iterator = docNames.iterator(); iterator.hasNext();) {
+ document = (XWikiDocument) iterator.next();
+ expectedCount += document.getAttachmentList().size();
+ }
+
+ int result = lpa.rebuildIndex(xwikiApi, context);
+ Thread.sleep(8000);
+ assertEquals(expectedCount + 1, result);
+ }
+
+ public void testXWikiObjectsOfDocument() throws XWikiException, InterruptedException {
+ XWikiDocument doc;
+
+ // Adding the lucene plugin
+ getXWiki().getPluginManager().addPlugin("lucene", "net.jkraemer.xwiki.plugins.lucene.LucenePlugin", getXWikiContext());
+
+ //adding document to the index
+ doc = xwiki.getDocument("Blog.Article1", context);
+ BaseObject baseobj = doc.getObject("XWiki.ArticleClass", true, context);
+ baseobj.set("title", "titleone", context);
+ baseobj.set("content", " test de contentone, just for trying", context);
+ List categoryList = new ArrayList();
+ categoryList.add("categoryone");
+ categoryList.add("categorytwo");
+ baseobj.set("category", categoryList, context);
+ List complexList = new ArrayList();
+ complexList.add("1");
+ baseobj.set("date", "2006", context);
+ baseobj.set("complexlist", complexList, context);
+ baseobj.set("extract", "blogarticleone", context);
+
+ xwiki.saveDocument(doc, context);
+
+ doc = xwiki.getDocument("Blog.Article2", context);
+ baseobj = doc.getObject("XWiki.ArticleClass", true, context);
+ baseobj.set("title", "titletwo", context);
+ baseobj.set("content", "categorytwo", context);
+ categoryList = new ArrayList();
+ categoryList.add("categoryone");
+ baseobj.set("category", categoryList, context);
+ baseobj.set("extract", "blogarticletwo", context);
+
+ xwiki.saveDocument(doc, context);
+
+ xwiki.flushCache();
+
+ Collection docNames = null;
+
+ LucenePlugin lpa = (LucenePlugin) xwiki.getPlugin("lucene", context);
+ com.xpn.xwiki.XWiki xwiki = context.getWiki();
+ XWiki xwikiApi = new XWiki(getXWikiContext().getWiki(), context);
+ int count = lpa.rebuildIndex(xwikiApi, context);
+
+ assertTrue("There should be at least 2 documents to index", (count >= 2));
+
+ docNames = xwiki.getStore().searchDocuments("", context);
+
+ assertEquals(3, docNames.size());
+
+ Object[] docNameArray = docNames.toArray();
+
+ String docName1 = ((XWikiDocument) docNameArray[0]).getFullName();
+ String docName2 = ((XWikiDocument) docNameArray[1]).getFullName();
+
+ assertEquals("Blog.Article1", docName1);
+ assertEquals("Blog.Article2", docName2);
+
+ LucenePluginApi lpApi = (LucenePluginApi) lpa.getPluginApi(lpa, context);
+
+ int loop = 0;
+ while ((lpa.getQueueSize() > 0) || (loop > 30)) {
+ Thread.sleep(1000);
+ }
+ assertNotNull("The plugin API should not be null :", lpApi);
+
+ String theQuery = "XWiki.ArticleClass.category:sdgdfgdfsgdfgd";
+ SearchResults results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext()));
+ assertNotNull("Search results shoould have not returned null", results);
+ assertEquals("(Objetct Search)The search result should be Zero", 0, results.getResults().size());
+
+ theQuery = "XWiki.ArticleClass.title:titleone";
+ results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext()));
+ assertNotNull("Search results shoould have not returned null", results);
+ assertEquals("(Objetct Search)The search result should be one (Title)", 1, results.getResults().size());
+
+ SearchResult res = (SearchResult) results.getResults().get(0);
+ assertEquals("Wrong document", "Blog.Article1", res.getWeb() + "." + res.getName());
+
+ theQuery = "XWiki.ArticleClass.content:contentone";
+ results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext()));
+ assertNotNull("Search results shoould have not returned null", results);
+ assertTrue("(Objetct Search)The search result should be one (Content)", results.getResults().size() == 1);
+
+ theQuery = "XWiki.ArticleClass.category:categoryone";
+ results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext()));
+ assertNotNull("Search results shoould have not returned null", results);
+ assertTrue("(Objetct Search)There should be exactly two docuements in the search(Category)", results.getResults().size() == 2);
+
+ theQuery = "XWiki.ArticleClass.category:categorytwo";
+ results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext()));
+ assertNotNull("Search results shoould have not returned null", results);
+ assertTrue("(Objetct Search)There should be exactly two docuements in the search(Category)", results.getResults().size() == 1);
+
+ theQuery = "XWiki.ArticleClass.complexlist.value:valueone";//MATCH
+ results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext()));
+ assertNotNull("Search results shoould have not returned null", results);
+ assertEquals("(Objetct Search)There should be exactly one docuement in the search(ComplexList)", 1, results.getResults().size());
+
+ theQuery = "XWiki.ArticleClass.complexlist.value:1";//NOT MATCH
+ results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext()));
+ assertNotNull("Search results shoould have not returned null", results);
+ assertEquals("(Objetct Search)There should be exactly one docuement in the search(ComplexList)", 0, results.getResults().size());
+
+ theQuery = "XWiki.ArticleClass.complexlist.key:1"; //MATCH
+ results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext()));
+ assertNotNull("Search results shoould have not returned null", results);
+ assertEquals("(Objetct Search)There should be exactly one docuement in the search(ComplexList)", 1, results.getResults().size());
+
+ theQuery = "XWiki.ArticleClass.complexlist.key:value1"; //NOT MATCH
+ results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext()));
+ assertNotNull("Search results shoould have not returned null", results);
+ assertEquals("(Objetct Search)There should be exactly one docuement in the search(ComplexList)", 0, results.getResults().size());
+
+ theQuery = "XWiki.ArticleClass.complexlist:1"; //MATCH
+ results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext()));
+ assertNotNull("Search results shoould have not returned null", results);
+ assertEquals("(Objetct Search)There should be exactly one docuement in the search(ComplexList)", 1, results.getResults().size());
+
+ theQuery = "XWiki.ArticleClass.complexlist:valueone"; //MATCH
+ results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext()));
+ assertNotNull("Search results shoould have not returned null", results);
+ assertEquals("(Objetct Search)There should be exactly one docuement in the search(ComplexList)", 1, results.getResults().size());
+
+ theQuery = "XWiki.ArticleClass.extract:blogarticletwo";
+ results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext()));
+ assertNotNull("Search results shoould have not returned null", results);
+ assertTrue("(Objetct Search)The search result should be one (Extract)", results.getResults().size() == 1);
+
+ theQuery = "XWiki.ArticleClass.date:2006"; //MATCH
+ results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext()));
+ assertNotNull("Search results shoould have not returned null", results);
+ assertEquals("(Objetct Search)There should be exactly one docuement in the search(Date)", 1, results.getResults().size());
+
+ //for fulltext search
+ theQuery = "categoryone";
+ results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext()));
+ assertTrue("(Full Text)There should be atleast one docuement in the search", results.getResults().size() == 2);
+ }
+
+ /**
+ * Method to test the document indexing without using the lucene plugin API
+ *
+ * @throws java.io.IOException , ParseException
+ */
+
+ public void testDocumentIndexing() throws Exception, ParseException {
+
+ String indexRep = "c:\\index\\";
+
+ // creating the lucene documents
+ Document lucenedoc1 = new Document();
+ Document lucenedoc2 = new Document();
+ Document lucenedoc3 = new Document();
+
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_ID, "1", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_LANGUAGE, "default,en,de", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_WIKI, "wiki", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_TYPE, "doc", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_DATE, "2006-10-11", Field.Store.YES, Field.Index.NO));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_CREATIONDATE, "2006-10-10", Field.Store.YES, Field.Index.NO));
+
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_NAME, "Login", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_WEB, "Register", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_AUTHOR, author, Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_CREATOR, "XWiki.LudovicDubost", Field.Store.YES, Field.Index.TOKENIZED));
+
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_ID, "2", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_LANGUAGE, "default,en,de", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_WIKI, "wiki", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_TYPE, "doc", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_DATE, "2006-10-11", Field.Store.YES, Field.Index.NO));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_CREATIONDATE, "2006-10-10", Field.Store.YES, Field.Index.NO));
+
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_NAME, "Register", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_WEB, "WebHome", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_AUTHOR, author, Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_CREATOR, "XWiki.LudovicDubost", Field.Store.YES, Field.Index.TOKENIZED));
+
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_ID, "3", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_LANGUAGE, "default,en,de", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_WIKI, "wiki", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_TYPE, "doc", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_DATE, "2006-10-11", Field.Store.YES, Field.Index.NO));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_CREATIONDATE, "2006-10-10", Field.Store.YES, Field.Index.NO));
+
+
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_NAME, "Welcome", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_WEB, "WebHome", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_AUTHOR, author, Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_CREATOR, "XWiki.LudovicDubost", Field.Store.YES, Field.Index.TOKENIZED));
+
+ IndexWriter writer = new IndexWriter(indexRep, new StandardAnalyzer(), true);
+
+ writer.addDocument(lucenedoc1);
+ writer.addDocument(lucenedoc2);
+ writer.addDocument(lucenedoc3);
+
+ writer.optimize();
+ writer.close();
+
+ String theQuery = "web:Web*";
+ Searcher[] mySearchers = LucenePlugin.createSearchers(indexRep);
+ MultiSearcher searcher = new MultiSearcher(mySearchers);
+ Query q = buildQuery(theQuery, null, null);
+ Hits hits = searcher.search(q);
+ assertEquals("There should be two hit", 2, hits.length());
+
+ mySearchers = LucenePlugin.createSearchers(indexRep);
+ searcher = new MultiSearcher(mySearchers);
+ q = buildQuery(theQuery, null, null);
+ hits = searcher.search(q);
+ assertEquals("There should be two hit", 2, hits.length());
+
+ mySearchers = LucenePlugin.createSearchers(indexRep);
+ searcher = new MultiSearcher(mySearchers);
+ q = buildQuery(theQuery, null, null);
+ hits = searcher.search(q);
+ assertEquals("There should be atleast one hit or more", 2, hits.length());
+ }
+
+
+
+ protected void tearDown() {
+ getXWiki().getPluginManager().removePlugin("net.jkraemer.xwiki.plugins.lucene.LucenePlugin");
+ super.tearDown();
+ }
+
+}
Added: xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/test/java/org/gelc/xwiki/plugins/lucene/LuceneTest.java
===================================================================
--- xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/test/java/org/gelc/xwiki/plugins/lucene/LuceneTest.java 2006-12-06 16:50:25 UTC (rev 1687)
+++ xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/test/java/org/gelc/xwiki/plugins/lucene/LuceneTest.java 2006-12-06 21:41:53 UTC (rev 1688)
@@ -0,0 +1,266 @@
+package org.gelc.xwiki.plugins.lucene;
+
+import com.xpn.xwiki.test.HibernateTestCase;
+import com.xpn.xwiki.api.XWiki;
+import com.xpn.xwiki.doc.XWikiDocument;
+import com.xpn.xwiki.objects.BaseObject;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.search.*;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.queryParser.ParseException;
+
+import java.io.IOException;
+
+import net.jkraemer.xwiki.plugins.lucene.IndexFields;
+import net.jkraemer.xwiki.plugins.lucene.LucenePluginTest;
+import net.jkraemer.xwiki.plugins.lucene.LucenePlugin;
+import net.jkraemer.xwiki.plugins.lucene.ObjectData;
+import groovy.ui.Console;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: xwiki
+ * Date: 30 nov. 2006
+ * Time: 11:53:06
+ * To change this template use File | Settings | File Templates.
+ */
+public class LuceneTest extends HibernateTestCase {
+
+ String author = "XWiki.LudovicDubost";
+ String parent = "Main.WebHome";
+ String parent1 = "Main.Lokesh";
+
+
+ // index directory
+ String indexDir = "C:\\indexDir\\";
+
+ /**
+ * This is to set up the plugin and required configurable items
+ *
+ * @throws Exception
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ // Setting the configuration properties to the XWiki Config
+ getXWikiConfig().setProperty("xwiki.plugins.lucene.indexdir", "C:\\indexDir\\");
+ getXWikiConfig().setProperty("xwiki.plugins.lucene.analyzer", "org.apache.lucene.analysis.standard.StandardAnalyzer");
+ getXWikiConfig().setProperty("xwiki.plugins.lucene.indexinterval", "2");
+
+ // Adding the lucene plugin
+ //getXWiki().getPluginManager().addPlugin("lucene", "net.jkraemer.xwiki.plugins.lucene.LucenePlugin", getXWikiContext());
+ //getXWikiContext().setURLFactory(new XWikiServletURLFactory(new URL("http://www.xwiki.org/"), "xwiki/", "bin/"));
+
+ // Rebuild the index
+ XWiki xwikiApi = new XWiki(getXWikiContext().getWiki(), context);
+ }
+
+ /**
+ * @param query
+ * @param virtualWikiNames comma separated list of virtual wiki names
+ * @param languages comma separated list of language codes to search in, may be
+ * null to search all languages
+ * @throws ParseException
+ */
+ public Query buildQuery(String query, String virtualWikiNames, String languages) throws ParseException {
+ // build a query like this: <user query string> AND <wikiNamesQuery> AND
+ // <languageQuery>
+ BooleanQuery bQuery = new BooleanQuery();
+ QueryParser qParser = new QueryParser(IndexFields.FULLTEXT, new StandardAnalyzer());
+ qParser.setDefaultOperator(QueryParser.Operator.AND);
+ Query parsedQuery = qParser.parse(query);
+ bQuery.add(parsedQuery, BooleanClause.Occur.MUST);
+ if (virtualWikiNames != null && virtualWikiNames.length() > 0) {
+ bQuery.add(buildOredTermQuery(virtualWikiNames, IndexFields.DOCUMENT_WIKI), BooleanClause.Occur.MUST);
+ }
+ if (languages != null && languages.length() > 0) {
+ bQuery.add(buildOredTermQuery(languages, IndexFields.DOCUMENT_LANGUAGE), BooleanClause.Occur.MUST);
+ }
+ return bQuery;
+ }
+
+ /**
+ * @param values comma separated list of values to look for
+ * @return A query returning documents matching one of the given values in
+ * the given field
+ */
+ public Query buildOredTermQuery(final String values, final String fieldname) {
+ String[] valueArray = values.split("\\,");
+ if (valueArray.length > 1) {
+ // build a query like this: <valueArray[0]> OR <valueArray[1]> OR ...
+ BooleanQuery orQuery = new BooleanQuery();
+ for (int i = 0; i < valueArray.length; i++) {
+ orQuery.add(new TermQuery(new Term(fieldname, valueArray[i].trim())), BooleanClause.Occur.SHOULD);
+ }
+ return orQuery;
+ }
+ // exactly one value, no OR'ed Terms necessary
+ return new TermQuery(new Term(fieldname, valueArray[0]));
+ }
+
+ /**
+ * Method to test the document indexing
+ *
+ * @throws IOException, ParseException
+ */
+
+ public void testDocumentIndexing() throws Exception, ParseException {
+
+ // creating the lucene documents
+ Document lucenedoc1 = new Document();
+ Document lucenedoc2 = new Document();
+ Document lucenedoc3 = new Document();
+
+ // Keyword fields: stored and indexed, but not tokenized
+ // for document one
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_ID, "1", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_LANGUAGE, "default,en,de", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_WIKI, "wiki", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_TYPE, "doc", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_DATE, "2006-10-11", Field.Store.YES, Field.Index.NO));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_CREATIONDATE, "2006-10-10", Field.Store.YES, Field.Index.NO));
+ // stored Text fields: tokenized and indexed
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_NAME, "Login", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_WEB, "Register", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_AUTHOR, author, Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc1.add(new Field(IndexFields.DOCUMENT_CREATOR, "XWiki.LudovicDubost", Field.Store.YES, Field.Index.TOKENIZED));
+
+ // Keyword fields: stored and indexed, but not tokenized
+ // for document two
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_ID, "2", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_LANGUAGE, "default,en,de", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_WIKI, "wiki", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_TYPE, "doc", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_DATE, "2006-10-11", Field.Store.YES, Field.Index.NO));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_CREATIONDATE, "2006-10-10", Field.Store.YES, Field.Index.NO));
+
+ // stored Text fields: tokenized and indexed
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_NAME, "Register", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_WEB, "WebHome", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_AUTHOR, author, Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc2.add(new Field(IndexFields.DOCUMENT_CREATOR, "XWiki.LudovicDubost", Field.Store.YES, Field.Index.TOKENIZED));
+
+ // Keyword fields: stored and indexed, but not tokenized
+ // for document three
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_ID, "3", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_LANGUAGE, "default,en,de", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_WIKI, "wiki", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_TYPE, "doc", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_DATE, "2006-10-11", Field.Store.YES, Field.Index.NO));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_CREATIONDATE, "2006-10-10", Field.Store.YES, Field.Index.NO));
+
+ // stored Text fields: tokenized and indexed
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_NAME, "Welcome", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_WEB, "WebHome", Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_AUTHOR, author, Field.Store.YES, Field.Index.TOKENIZED));
+ lucenedoc3.add(new Field(IndexFields.DOCUMENT_CREATOR, "XWiki.LudovicDubost", Field.Store.YES, Field.Index.TOKENIZED));
+
+ //creating the index directory for the lucene documents using the standard analyzer
+ IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true);
+
+ // adding the documents to the index writer
+ writer.addDocument(lucenedoc1);
+ writer.addDocument(lucenedoc2);
+ writer.addDocument(lucenedoc3);
+
+ //saving the index
+ writer.optimize();
+ writer.close();
+
+ //query
+ String theQuery = "web:Web*";
+
+ Searcher[] mySearchers = LucenePlugin.createSearchers(indexDir);
+
+ MultiSearcher searcher = new MultiSearcher(mySearchers);
+ Query q = buildQuery(theQuery, null, null);
+ Hits hits = searcher.search(q);
+
+ //Assertiong the outputs
+ assertEquals("There should be atleast one hit or more", 2, hits.length());
+
+ theQuery = "web:WebHome*";
+ //mySearchers = LucenePlugin.createSearchers(indexDir);
+ searcher = new MultiSearcher(mySearchers);
+ q = buildQuery(theQuery, null, null);
+ hits = searcher.search(q);
+ //Assertiong the outputs
+ assertEquals("There should be atleast one hit or more", 2, hits.length());
+
+
+ theQuery = "name:R*";
+ //mySearchers = LucenePlugin.createSearchers(indexDir);
+
+ searcher = new MultiSearcher(mySearchers);
+ q = buildQuery(theQuery, null, null);
+ hits = searcher.search(q);
+
+ //Assertiong the outputs
+ assertEquals("There should be atleast one hit or more", 1, hits.length());
+ }
+
+
+ /**
+ * Method to test the object indexing in documents
+ */
+ public void testObjectIndexing() throws Exception {
+
+ XWikiDocument docOne = new XWikiDocument();
+ docOne.setFullName("Blog.Article1");
+ docOne.setContent("Here is the content of the first document");
+ BaseObject baseObjOne = new BaseObject();
+ baseObjOne.setClassName("XWiki.ArticleClass");
+ baseObjOne.set("title", "title1", null);
+ baseObjOne.set("content", "here is the content of first doc Object", null);
+ baseObjOne.set("category1", "category1", null);
+ baseObjOne.set("category2", "category2", null);
+ docOne.addObject("XWiki.ArticleClass", baseObjOne);
+
+ XWikiDocument docTwo = new XWikiDocument();
+ docTwo.setFullName("Blog.Article2");
+ docOne.setContent("Here is the content of the second document");
+ BaseObject baseObjTwo = new BaseObject();
+ baseObjTwo.setClassName("XWiki.ArticleClass");
+ baseObjTwo.set("title", "title2", null);
+ baseObjTwo.set("content", "here is the content of second doc Object", null);
+ baseObjTwo.set("category3", "category3", null);
+ docOne.addObject("XWiki.ArticleClass", baseObjOne);
+
+ XWikiDocument testDoc = new XWikiDocument();
+ testDoc.setFullName("Test.Foo");
+
+ ObjectData objDataOne = new ObjectData(docOne, null);
+ ObjectData objDataTwo = new ObjectData(docTwo, null);
+ ObjectData testObjData = new ObjectData(testDoc, null);
+
+ Document luceneDoc1 = new Document();
+ Document luceneDoc2 = new Document();
+ Document testluceneDoc = new Document();
+
+ objDataOne.addDataToLuceneDocument(luceneDoc1, docOne, null);
+ objDataTwo.addDataToLuceneDocument(luceneDoc2, docTwo, null);
+ testObjData.addDataToLuceneDocument(testluceneDoc, testDoc, null);
+
+ IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true);
+
+ writer.addDocument(luceneDoc1);
+ writer.addDocument(luceneDoc2);
+ writer.addDocument(testluceneDoc);
+
+ writer.optimize();
+ writer.close();
+
+ String theQuery = "XWiki.ArticleClass.title:title1";
+ Searcher[] mySearchers = LucenePlugin.createSearchers(indexDir);
+ MultiSearcher searcher = new MultiSearcher(mySearchers);
+ Query q = buildQuery(theQuery, null, null);
+ Hits hits = searcher.search(q);
+
+ assertEquals("Here should be atleast one hit :", 1, hits.length());
+
+ }
+}
More information about the Xwiki-notifications
mailing list