This documentation is for WSO2 API Manager 1.7.0 View documentation for the latest release.
Page Comparison - Adding Apache Solr-Based Indexing (v.1 vs v.2) - API Manager 1.7.0 - WSO2 Documentation

All docs This doc

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In addition to the default ones, you can write your own indexer implementation and register it . When a new resource is added to the registry with the relevant media type in an indexer, the content is indexed using thatas follows:

  1. Write a custom indexer. Given below is a sample indexer code

...

  1. .

    Code Block
    languagejava
    package org.wso2.indexing.sample;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Arrays;
    import org.apache.solr.common.SolrException;
    import org.wso2.carbon.registry.core.exceptions.RegistryException;
    import org.wso2.carbon.registry.core.utils.RegistryUtils;
    import org.wso2.carbon.registry.indexing.IndexingConstants;
    import org.wso2.carbon.registry.indexing.AsyncIndexer.File2Index;
    import org.wso2.carbon.registry.indexing.indexer.Indexer;
    import org.wso2.carbon.registry.indexing.solr.IndexDocument;
    
    public class PlainTextIndexer implements Indexer {
        public IndexDocument getIndexedDocument(File2Index fileData) throws SolrException,
                RegistryException {
                 
                 /* Create index document with resource path and raw content*/
                 IndexDocument indexDoc = new IndexDocument(fileData.path, RegistryUtils.decodeBytes(fileData.data), null);
                 
                 /* You can specify required field/value pairs for this indexing document.
                  * When searching we can query on these fields  */
                 Map<String, List<String>> fields = new HashMap<String, List<String>>();
                 fields.put("path", Arrays.asList(fileData.path));
                                         
                 if (fileData.mediaType != null) {
                             fields.put(IndexingConstants.FIELD_MEDIA_TYPE, Arrays.asList(fileData.mediaType));
                 } else {
                             fields.put(IndexingConstants.FIELD_MEDIA_TYPE, Arrays.asList("text/plain"));
                 }
                 
                 /* set fields for index document*/
                 indexDoc.setFields(fields);             
                 return indexDoc;
        }    
    }

 

 

 

 

 

 

Writing custom indexer

 

 

 

Sample Indexer Code. (Refer complete source sample-indexer.zip)

 

 

 

 

...

  1. Add the custom indexer JAR file to <APIM_HOME>/repository/components/lib directory.

 

...

  1. Update the <Indexers> element in <APIM_HOME>/repository/conf/registry.xml

...

 

ex:

 

...

  1. file with the new indexer. The content is indexed using this media type. For example,

    Code Block
    languagexml
    <indexers>
         <indexer class="org.wso2.indexing.sample.PlainTextIndexer" mediaTypeRegEx="text/plain" profiles="default,api-store,api-publisher"/>

...

 

 

...

class

indexer class name

...

  1. 
    </indexers>

    The attributes of the above configuration are described below:

    classJava class name of the indexer
    mefiaTypeRegExA regex pattern to match the media type

...

  1. profilesAPIM profiles in which the indexer

...

  1. is available

 

 

...

  1. Restart the server

...

 

...

  1. .
  2. Add API documentation

...

  1. using the

...

  1. new media type and then search some term in the documentation using the syntax (doc:keyword). You will see how the documentation has got indexed according to the media type.