Writing only specific xml-element-data to the lucene index

From OpenCms Wiki
Jump to: navigation, search

Lets say you have a custom xmlcontent module named drivers and you want to search for the xmlcontent fields name and firstname.

Contents

Extract from the xml schema

<xsd:element name="firstname" type="OpenCmsString" minOccurs="1" maxOccurs="1" />
<xsd:element name="name" type="OpenCmsString" minOccurs="1" maxOccurs="1" />
<xsd:element name="scope" type="OpenCmsString" minOccurs="0" maxOccurs="1" />
<xsd:element name="subsidiary" type="OpenCmsAddress" minOccurs="1" maxOccurs="1" />
<xsd:element name="Image" type="OpenCmsImage" minOccurs="0" maxOccurs="1"/>
<xsd:element name="Availability" type="OpenCmsAvailability" minOccurs="0" />

Creating search index

  1. create a search index named My drivers as described in Configuration.
  2. create a new field configuration named drivers
    1. add a new field named name to your field config and set the following values
      1. Name: name
      2. Display: name
      3. Store: True
      4. Index: True
      5. Excerpt: False
      6. boost: 1.0
      7. Default:
    2. add a mapping to the name field and define the following values
      1. Type: item
      2. Parameter: name[1]
      3. Default:
    3. add a new field named firstname to your field config and do the same settings as for the name field
    4. add two more fields named title and title-key to your field config and do the same settings as this fields have in the standard field configuration which is used by the indices already configured in an standard OpenCms installation.
  3. create a new index source named drivers
    1. add the document type drivers and
    2. for example /sites/default/ as for the resources which should be indexed.
  4. edit your search index My drivers and change the field configuration from standard to drivers

Testing your search configuration

OpenCms 7 ships with a ready to use search form to test your search configuration in the administration view. Do this and adjust the settings if necessary.

Creating a search form

To create a search form have a look at the file alkacon-documentation\examples_search\search.jsp in the com.alkacon.documentation.examples_search_7.0.4.zip-Module. In order to get your custom fields name and firstname shown on the result page and to tell it which index to use, you have to pass them on to your action page. For example using hidden fields. This would look like this:

<input type="hidden" name="index" value="My drivers" />
<input type="hidden" name="field" value="title" />
<input type="hidden" name="field" value="name" />
<input type="hidden" name="field" value="firstname" />

Creating a search result page

To create a search form have a look at the file alkacon-documentation\examples_search\result.jsp in the com.alkacon.documentation.examples_search_7.0.4.zip-Module.

In order to read the content of your custom fields, you can use the method org.opencms.search.CmsSearchResult#getField(). For the two fields name and firstname this would look like this

<%
while (iterator.hasNext()) {
   CmsSearchResult entry = (CmsSearchResult)iterator.next();
%>
 
	<h3>
		<%= resultno %>.&nbsp;<a href="
					<%= cmsGlbVar.link(cmsGlbVar.getRequestContext().removeSiteRoot(entry.getPath())) %>
					">
					<%= entry.getTitle() %>	
				      </a>
	</h3>
	<p>
		<%= entry.getExcerpt() %>
		<br />
		<a href="<%= cmsGlbVar.link(cmsGlbVar.getRequestContext().removeSiteRoot(entry.getPath())) %>" class="link">
			<%= cmsGlbVar.link(cmsGlbVar.getRequestContext().removeSiteRoot(entry.getPath())) %>
		</a>
		<br />
		<%= entry.getField("name") %>
		<br />
		<%= entry.getField("firstname") %>
	</p>
 
<%
     resultno++;            
}
%>
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox