Plexian - Autocomplete storage for Java applications

Plexian project page available here

Here is sample how to use plexian:

Requirements

Step 1. Bean definition

Write to applicationContext.xml next xml code:

  <bean id="plexian" class="net.sf.plexian.springframework.PlexianBean">
     <constructor-arg value="${webapp.root}/../../suggest" />
  </bean>

Step 2. Access to autocomplete

Define Struts2 action in struts.xml like this:

    <action name="autoCompleter" class="webapp.action.AutoCompleterAction">
      <result>/WEB-INF/jsp/blocks/auto_completer.jspx</result>
    </action>

Write java code for retrieve autocomplete information from storage:

   public class AutoCompleterAction {

        private Plexian plexian;

        private String query;
        private String field;
        private boolean keywords = false;

        private List<String> result;

        public String execute() {
                result = plexian.search(field, keywords ? SearchType.KEYWORDS : SearchType.ORIGINAL_NAMES, query);
                return "success";
        }

        public void setQuery(String query) {
                this.query = query;
        }

        public void setField(String field) {
                this.field = field;
        }

        public void setPlexian(Plexian plexian) {
                this.plexian = plexian;
        }

        public List<String> getResult() {
                return result;
        }

        public void setKeywords(boolean keywords) {
                this.keywords = keywords;
        }
   }

Define aculo.us scripts in html head required for autocomplete:

            <script type="text/javascript" src="${request.contextPath}/jslib/aculous/effects.js">
                      <jsp:text />
            </script>
                <script type="text/javascript" src="${request.contextPath}/jslib/aculous/controls.js">
                      <jsp:text />
            </script>

Insert div elements after input fields required for autocomplete:

    <input type="text" name="query" AUTOCOMPLETE="OFF" id="search-query" />
    <div class="auto_complete" id="query_auto_completer" style="display: none;"><jsp:text /></div>

Write onload javascript for initialize aculo.us:

    function onLoad() {
        new Ajax.Autocompleter('search-query', 'query_auto_completer', '/pub/autoCompleter', { paramName: 'query', parameters : 'keywords=true&field=PlexianFieldName' });
    }

Write auto_completer.jspx:

    <ul>
        <c:if test="${not empty result}">
             <c:forEach var="item" items="${result}">
                 <li>${item}</li>
             </c:forEach>
        </c:if>
    </ul>

Step 3. Indexation

You can index and reindex plexian fields, using plexian bean:

    plexian.index(FIELD_NAME, stringForIndex, IndexType.ADD_FREQ, freq);

Step 4. Plexian Indexation

You can use plexian indexator for Hibernate and Lucene datasource:

  <bean id="plexianIndexators" class="net.sf.plexian.springframework.PlexianIndexatorsBean">
    <property name="indexators">
       <list>
          <bean class="net.sf.plexian.indexator.lucene.LuceneIndexator">
            <property name="plexian" ref="plexian" />
            <property name="luceneIndexProvider" ref="luceneConfig" />
            <property name="fields">
              <map>
                 <entry key="FirstName" value="Profile.FIRST_NAME,Person.FIRST_NAME" />
              </map>
            </property>
          </bean>
          <bean class="net.sf.plexian.indexator.hibernate.HibernateIndexator">
            <property name="plexian" ref="plexian" />
            <property name="transactionManager" ref="transactionManager" />
            <property name="hibernateOperations" ref="hibernateOperations" />
            <property name="fields">
              <map>
                 <entry key="LastName" value="Profile.lastName,Person.lastName" />
              </map>
            </property>
          </bean>
       </list>
    </property>
  </bean>

Step 5. JMX

If you wants to get statistics by JMX then define:

  <bean name="plexianMBean" class="net.sf.plexian.jmx.PlexianMBeanImpl">
    <constructor-arg ref="plexian" />
  </bean>

Then you can export this bean like this:

  <bean id="MBeanExporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
    <property name="exposeManagedResourceClassLoader" value="true" />
    <property name="beans">
      <map>
        <entry key="Webapp:name=Plexian" value-ref="plexianMBean" />
      </map>
    </property>
    <property name="autodetect" value="true" />
  </bean>