SearchResultField

You can select which fields appear in the search results with the Fields list in the results section of the search panel. Brightspot populates the Select Fields widget with default fields.

../../../_images/select-fields1.png

Using the SearchResultField interface, you can add custom fields to the widget. For example, say that you want the author name to appear for articles listed in the search results. You can implement SearchResultField to return the Author field for Article objects retrieved in a query, similar to the following example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class AuthorField implements SearchResultField {

   @Override
   public String getDisplayName() {
      return "Author";
   }

   @Override
   public boolean isSupported(ObjectType type) {
      return true;
   }

   @Override
   public String createDataCellText(Object item) {
      Article article = State.getInstance(item).getOriginalObject() instanceof Article
             ? ((Article) State.getInstance(item).getOriginalObject()) : null;

      if (article != null) {
         if (article.getAuthor() != null)
            return article.getAuthor().getName();
         else return ("No attribution");
      }
      return null;
   }
}

In the previous snippet—

  • Lines 3–6 implement the getDisplayName method, specifying how the field appears in the search results.
  • Lines 8–11 implement the isSupported method. If it returns false, the Author field will not appear in the Select Fields widget.
  • Lines 13–24 implement the createDataCellText method. The method filters for Article objects and returns null if the search result is not an Article. For objects where the Author field is set, the author name is returned; otherwise “No attribution” is returned.

With the new implementation, the Author field is now available in the Select Fields widget.

../../../_images/select-fields2.png

With a query on articles, the Author field is included in the search results.

../../../_images/results1.png