Skip to content

Commit

Permalink
implementation
Browse files Browse the repository at this point in the history
Issue #680
  • Loading branch information
rsoika committed Jun 10, 2020
1 parent 9d1c42b commit c7cb9cc
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 19 deletions.
@@ -0,0 +1,91 @@
/*
* Imixs-Workflow
*
* Copyright (C) 2001-2020 Imixs Software Solutions GmbH,
* http://www.imixs.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You can receive a copy of the GNU General Public
* License at http://www.gnu.org/licenses/gpl.html
*
* Project:
* https://www.imixs.org
* https://github.com/imixs/imixs-workflow
*
* Contributors:
* Imixs Software Solutions GmbH - Project Management
* Ralph Soika - Software Developer
*/

package org.imixs.workflow.engine.index;

import org.imixs.workflow.ItemCollection;

/**
* The IndexEvent provides a CDI event fired immediately before a document is
* indexed by the search service implementation.
* <p>
* An observer CDI bean can change or extend the text content to be indexed. The
* IndexEvent defines only one event type:
* <ul>
* <li>ON_INDEX_UPDATE - is send immediately before a document will be indexed
* </ul>
* The property 'textContent' can be updated or extended by a client.
*
* @author Ralph Soika
* @version 1.0
*/
public class IndexEvent {

public static final int ON_INDEX_UPDATE = 1;

private int eventType;
private String textContent;
private ItemCollection document;

public IndexEvent(int eventType, ItemCollection document) {
this.eventType = eventType;
this.document = document;
}

/**
* Returns the textContent for the given document to be indexed.
*
* @return
*/
public String getTextContent() {
return textContent;
}

/**
* Update the textContent for the given document to be indexed.
*
* @return
*/
public void setTextContent(String textContent) {
this.textContent = textContent;
}

public int getEventType() {
return eventType;
}

/**
* Returns the document to be indexed
*
* @return
*/
public ItemCollection getDocument() {
return document;
}

}
Expand Up @@ -42,6 +42,7 @@
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
Expand All @@ -66,6 +67,7 @@
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.engine.EventLogService;
import org.imixs.workflow.engine.adminp.AdminPService;
import org.imixs.workflow.engine.index.IndexEvent;
import org.imixs.workflow.engine.index.SchemaService;
import org.imixs.workflow.engine.jpa.EventLog;
import org.imixs.workflow.exceptions.IndexException;
Expand Down Expand Up @@ -113,6 +115,10 @@ public class LuceneIndexService {

@Inject
private SchemaService schemaService;

@Inject
protected Event<IndexEvent> indexEvents;


public String getLuceneIndexDir() {
// issue #599
Expand Down Expand Up @@ -295,6 +301,8 @@ protected boolean flushEventLogByCount(int count) {
ItemCollection workitem = new ItemCollection();
workitem.setAllItems(doc.getData());
if (!workitem.getItemValueBoolean(DocumentService.NOINDEX)) {


indexWriter.updateDocument(term, createDocument(workitem));
logger.finest("......lucene add/update workitem '" + doc.getId() + "' to index in "
+ (System.currentTimeMillis() - l2) + "ms");
Expand Down Expand Up @@ -359,22 +367,22 @@ protected boolean flushEventLogByCount(int count) {
* The property 'AnalyzeIndexFields' defines if a indexfield value should by analyzed by the
* Lucene Analyzer (default=false)
*
* @param aworkitem
* @return
* @param document - the Imixs document to be indexed
* @return - a lucene document instance
*/
@SuppressWarnings("unchecked")
protected Document createDocument(ItemCollection aworkitem) {
protected Document createDocument(ItemCollection document) {
String sValue = null;
Document doc = new Document();
// combine all search fields from the search field list into one field
// ('content') for the lucene document
String sContent = "";
String textContent = "";

List<String> searchFieldList = schemaService.getFieldList();
for (String aFieldname : searchFieldList) {
sValue = "";
// check value list - skip empty fields
List<?> vValues = aworkitem.getItemValue(aFieldname);
List<?> vValues = document.getItemValue(aFieldname);
if (vValues.size() == 0)
continue;
// get all values of a value list field
Expand All @@ -397,11 +405,24 @@ protected Document createDocument(ItemCollection aworkitem) {
sValue += o.toString() + ",";
}
if (sValue != null) {
sContent += sValue + ",";
textContent += sValue + ",";
}
}
logger.finest("......add lucene field content=" + sContent);
doc.add(new TextField("content", sContent, Store.NO));

// fire IndexEvent to update the text content if needed
if (indexEvents != null) {
IndexEvent indexEvent=new IndexEvent(IndexEvent.ON_INDEX_UPDATE,document);
indexEvent.setTextContent(textContent);
indexEvents.fire(indexEvent);
textContent=indexEvent.getTextContent();
} else {
logger.warning("Missing CDI support for Event<IndexEvent> !");
}



logger.finest("......add lucene field content=" + textContent);
doc.add(new TextField("content", textContent, Store.NO));

// add each field from the indexFieldList into the lucene document
List<String> _localFieldListStore = new ArrayList<String>();
Expand All @@ -410,22 +431,22 @@ protected Document createDocument(ItemCollection aworkitem) {
// analyzed...
List<String> indexFieldListAnalyze = schemaService.getFieldListAnalyze();
for (String aFieldname : indexFieldListAnalyze) {
addItemValues(doc, aworkitem, aFieldname, true, _localFieldListStore.contains(aFieldname));
addItemValues(doc, document, aFieldname, true, _localFieldListStore.contains(aFieldname));
// avoid duplication.....
_localFieldListStore.remove(aFieldname);
}

// ... and not analyzed...
List<String> indexFieldListNoAnalyze = schemaService.getFieldListNoAnalyze();
for (String aFieldname : indexFieldListNoAnalyze) {
addItemValues(doc, aworkitem, aFieldname, false, _localFieldListStore.contains(aFieldname));
addItemValues(doc, document, aFieldname, false, _localFieldListStore.contains(aFieldname));
}

// add $uniqueid not analyzed
doc.add(new StringField("$uniqueid", aworkitem.getItemValueString("$uniqueid"), Store.YES));
doc.add(new StringField("$uniqueid", document.getItemValueString("$uniqueid"), Store.YES));

// add $readAccess not analyzed
List<String> vReadAccess = (List<String>) aworkitem.getItemValue(DocumentService.READACCESS);
List<String> vReadAccess = (List<String>) document.getItemValue(DocumentService.READACCESS);
if (vReadAccess.size() == 0
|| (vReadAccess.size() == 1 && "".equals(vReadAccess.get(0).toString()))) {
// if emtpy add the ANONYMOUS default entry
Expand Down
Expand Up @@ -44,6 +44,7 @@
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.persistence.EntityManager;
Expand All @@ -57,6 +58,7 @@
import org.imixs.workflow.engine.SetupEvent;
import org.imixs.workflow.engine.adminp.AdminPService;
import org.imixs.workflow.engine.index.DefaultOperator;
import org.imixs.workflow.engine.index.IndexEvent;
import org.imixs.workflow.engine.index.SchemaService;
import org.imixs.workflow.engine.index.SortOrder;
import org.imixs.workflow.engine.jpa.EventLog;
Expand Down Expand Up @@ -137,6 +139,9 @@ public class SolrIndexService {

@Inject
private AdminPService adminPService;

@Inject
protected Event<IndexEvent> indexEvents;

@PersistenceContext(unitName = "org.imixs.workflow.jpa")
EntityManager manager;
Expand Down Expand Up @@ -569,11 +574,10 @@ protected String buildAddDoc(List<ItemCollection> documents) {
}

xmlContent.append("<doc>");

xmlContent.append("<field name=\"id\">" + document.getUniqueID() + "</field>");

// add all content fields defined in the schema
String content = "";
String textContent = "";
for (String field : fieldList) {
String sValue = "";
// check value list - skip empty fields
Expand Down Expand Up @@ -601,18 +605,30 @@ protected String buildAddDoc(List<ItemCollection> documents) {
sValue += o.toString() + ",";
}
if (sValue != null) {
content += sValue + ",";
textContent += sValue + ",";
}
}

// fire IndexEvent to update the text content if needed
if (indexEvents != null) {
IndexEvent indexEvent=new IndexEvent(IndexEvent.ON_INDEX_UPDATE,document);
indexEvent.setTextContent(textContent);
indexEvents.fire(indexEvent);
textContent=indexEvent.getTextContent();
} else {
logger.warning("Missing CDI support for Event<IndexEvent> !");
}


if (debug) {
logger.finest("......add index field " + DEFAULT_SEARCH_FIELD + "=" + content);
logger.finest("......add index field " + DEFAULT_SEARCH_FIELD + "=" + textContent);
}
// remove existing CDATA...
content = stripCDATA(content);
textContent = stripCDATA(textContent);
// strip control codes..
content = stripControlCodes(content);
textContent = stripControlCodes(textContent);
// We need to add a wrapping CDATA, allow xml in general..
xmlContent.append("<field name=\"" + DEFAULT_SEARCH_FIELD + "\"><![CDATA[" + content + "]]></field>");
xmlContent.append("<field name=\"" + DEFAULT_SEARCH_FIELD + "\"><![CDATA[" + textContent + "]]></field>");

// now add all analyzed fields...
for (String aFieldname : fieldListAnalyze) {
Expand Down

0 comments on commit c7cb9cc

Please sign in to comment.