Page tree
Skip to end of metadata
Go to start of metadata

Overview

A ResponseTransformer operates on a SearchDocument extracted from a SearchDocumentList in a QueryResponse message in the defaultReponse workflow. A ResponseTransformer typically retrieves field values from the SearchDocument, applies business logic to them, and then asserts new information back into the SearchDocument before allowing it to proceed to the Search Client.


ResponseWorkflowBoxes

Response transformers operate on QueryResponse  objects. Each QueryResponse contains a list of matching documents plus an extensive set of header information (the "info"). 


Response transformers that implement the ResponseTransformer  interface must implement a processResponseInfo() method and/or a processResponseDocuments() method.


The processResponseInfo() method lets us process a QueryResponseInfo which contains information about the query request and the metadata about how long it took to compute the results. Here's an example:

Based on SampleResponseTransformer.java
  @Override
  public void processResponseInfo(QueryResponseInfo info) throws AttivioException {
    // you can modify the QueryResponseInfo here if necessary
    List<QueryFeedback> qfList = info.getFeedbackByMessageName("sampleProcessedResponse");
    QueryFeedback qf = new QueryFeedback(this.getClass().getSimpleName(), "sampleProcessedResponseInfo",
        "also added an additional feedback message to the query's QueryResponseInfo");
    qfList.add(qf);
    info.setFeedback(qfList);
  }

To obtain the QueryResponseInfo object, use the QueryResponse.getResponseInfo() method.

The processResponseDocuments() method lets us preform operations on the documents of a response. Here is an example:

Based on SampleResponseTransformer.java
  @Override
  public void processResponseDocuments(QueryResponseInfo info, SearchDocumentList documents) throws AttivioException {
    // here we'll add an additional response document
    SearchDocument doc4 = new SearchDocument("4");
    doc4.setField("title", "document 4");
    doc4.setField("cat", "cat1");
    doc4.setField("date", new Date());
    documents.add(doc4);
    // adding feedback is optional but is useful for letting end users know what happened.
    info.addFeedback(this.getClass().getSimpleName(), "sampleProcessedResponse", "added a new response document");
  }

To obtain the SearchDocumentList, use the QueryResponse.getDocuments() method.

View incoming links.

 

On this page we demonstrate how to create a custom ResponseTransformer in Java, and how to put it into an AIE response workflow. 

Field Expressions

Note that AIE's rich palette of Field Expressions offer an alternate (and often easier) way to transform response field values. You should review their capabilities before embarking on a custom Response Transformer. Field Expressions are available through the Java Client API and the JSON REST API.

Creating Custom Code

This section presents a simple example of extending the AIE response process with custom code. Tasks include:

  • Creating a new ResponseTransformer in Java. This transformer extracts a value from the Date field of a SearchDocument, transforms the date to a localized format, and adds the transformed date string to a new field in the SearchDocument.
  • Wrapping the new transformer in a new component.
  • Inserting the new component into the defaultResponse workflow.
  • Viewing modified records in SAIL to see the new date string appear in the search results.

Create a Custom Field Transformer in Java

A ResponseTransformer operates on the fields of a SearchDocument.

To create a simple response transformer, do the following:

  1. See the Using Java APIs page to create a "plusjava" project in the AIE Designer. Then continue with the steps of this procedure.
  2. In Designer, right-click the project node in the Package Explorer view. 
  3. Select New -> Class from the context menu. The New Java Class dialog box appears.
  4. Specify the Source folder as the /src folder of your project, in this case AIE - plusjava/src.
  5. Select (or invent) an appropriate Java Package for the new transformer. In this case we chose com.acme.examples.
  6. Enter the transformer's name. In this case it is DateResponseTransformer.
  7. Specify the appropriate interface. In this case the appropriate choice is the com.attivio.sdk.server.component.query.ResponseTransformer interface.
  8. Accept the remaining defaults and click Finish. This opens an editor for DateResponseTransformer.java .
  9. Edit the DateResponseTransformer class definition to import the necessary classes, as shown here:

    DemoFieldTransformer.java
    package com.acme.examples;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import com.attivio.sdk.AttivioException;
    import com.attivio.sdk.search.QueryResponse;
    import com.attivio.sdk.search.SearchDocument;
    import com.attivio.sdk.search.SearchDocumentList;
    import com.attivio.sdk.search.SearchFieldValue;
  10. Edit the processSearchDocument() method. This is the method that processes each SearchDocument that appears in the input queue of the transformer.

    DateResponseTransformer
    public class DateResponseTransformer  {
        public void processResponseDocuments(QueryResponse info,
                SearchDocumentList documents) throws AttivioException {
            for (SearchDocument doc : documents)
            { SearchFieldValue Value = doc.getFirstValue("date"); 
            Date newDate = Value.dateValue(); 
            SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy"); 
            doc.addValue("newDate", sdf.format(newDate)); }
        }
    }

    The transformer receives a SearchDocumentList from the QueryResponse. It iterates over the documents, and pulls the date field from each. It takes the first (in this case, the only) value from the field and casts it as a java.Date.util object. The response transformer then generates a string representing the date in "dd MMM yyy" format. This string is stored in a new field, newDate, and the SearchDocument is released. 

  11. Save the file. If the Project menu Build Automatically feature is selected (the default), saving the file will automatically build the project. If not, use the Project menu Build Project command.
  12. Use the AIE Runtime menu to Start All Project Servers.
  13. If this is the first time you have ever run the plusjava project, Designer will automatically deploy the project resources to the Configuration Servers and the AIE nodes. If not, you must use the AIE Runtime menu to Deploy Project Configuration.

 

Configure a Component

Using dynamic configuration, we're going to create a new DateChanger component to encapsulate an instance of the new DateResponseTransformer.java class. We'll insert the new component at the beginning of the defaultResponse workflow.

All of this needs to be configured a step at a time in the AIE Administrator.

Create a Component

We need to configure a new component, DateChanger, to encapsulate the DateResponseTransformer instance.

  1. Direct your browser to http://localhost:17000/admin (or substitute your host and port).
  2. Open the Palette.
  3. Click the New button. This opens a dialog box to select a platform component type.
  4. Open the list of Response Transformers.
  5. Scroll down to the DateResponseTransformer. Select it and click OK. This opens a Component Editor.
  6. On the Platform Component tab of the editor, enter the component's name. We used "DateChanger."
  7. When finished, click the Save button. 

 

Add Component to a Workflow

The next step is to insert the component into the defaultResponse workflow. 

  1. Navigate to System Manager > Workflows > Response in the AIE Administrator interface.
  2. Click the defaultResponse workflow. This opens an Workflow Editor.
  3. Use the Add Existing Component button to add the DateChanger component to the workflow.
  4. Use the Move Up button to move DateChanger to the top of the list. .
  5. Click the Save button.

 

Run AIE and Search for Medals

This date-conversion transformer should work with any AIE project, because SearchDocuments all have a Date field. In this case we used a Factbook demo, and searched for Olympic medals. It was plainly evident that the transformer had worked, because the newDate field was present in the results, and the date string was formatted as requested.

  • No labels