Faceted Site Search - Documentation topics on: deprecated,faceted site search,facets,site search,.

Faceted Site Search
Deprecated

Faceted search dynamically clusters items or search results into categories and allows users to drill into the search results by any value in any field. Each displayed facet shows the number of hits within the search that match that category, and users can “drill down” by applying specific constraints to the search results.


Important

Elasticsearch facets have been deprecated.


The getFacets Method

To perform a faceted search, call the $sitesearch.getFacets method.

Arguments

The getFacets method requires two arguments:

ArgumentTypeDescription
Index AliasStringThe alias of the search index to perform the search against.
If you have not defined any search index aliases, use “Default”.
Query StringStringA valid ElasticSearch query string (using full Elasticsearch JSON syntax).

Search Fields

You can perform a faceted search on any of the following search fields:

  • content
  • filename
  • host
  • mimeType
  • title
  • description
  • keywords
  • author
  • contentLength
  • modified
  • score

Results

getFacets returns a list of Facets for the most popular terms across the documents matching your query, or across all documents in the index, each with a term and count.

Example

The following Elasticsearch query performs a facet search on the mimeType term and a search query $searchQ.

First, we define the facet query using the mimeType term:

#set($mimeFacetsQry = '{
    "query" : { "query_string" : {"query" : "$searchQ"} },
    "facets" : {
        "mimeType" : { "terms" : {"field" : "mimeType"} } 
    },
    "size":0
}')

Next we call the $sitesearch.getFacets method:

#set($mimeFacetsQry = $render.eval($context, $mimeFacetsQry ))
#set($mimeFacets = $sitesearch.getFacets("Default", $mimeFacetsQry))

Finally we iterate over each of the returned facets to display both the facet term and the count:

#if($UtilMethods.isSet($mimeFacets) && $mimeFacets.size() > 0)
    #foreach($facet in $mimeFacets)
        #foreach($term in $facet.entries())
            $term.term ($term.count)
        #end
    #end
#end

Additional Facet Implementations

The following examples demonstrate queries using additional Elasticsearch facet implementations, including Range facets, Histogram facets and Date Histogram facets.

Range

The following query implements an Elasticsearch Range Facet.

#set($rangeFacets = '  {
    "query" : { "query_string" : {"query" : "$searchQ"} },
    "facets" : {
        "range1" : {
            "range" : {
                "field" : "contentLength",
                "ranges" : [
                    { "to" : 50 },
                    { "from" : 20, "to" : 70 },
                    { "from" : 70, "to" : 120 },
                    { "from" : 150 }
                ]
            }
        }
    },"size":0
  }')

Histogram

The following query implements an Elasticsearch Histogram Facet.

#set($histogramFacets = '  {
    "query" : { "query_string" : {"query" : "$searchQ"} },
    "facets" : {
        "histo1" : {
            "histogram" : {
                "field" : "modified",
                "interval" : 100
            }
        }
    },
    "size":0
}')

Date Histogram

The following query implements an Elasticsearch Date Histogram Facet.

#set($dateFacets = '  {
    "query" : { "query_string" : {"query" : "$searchQ"} },
    "facets" : {
        "modified" : { "date_histogram" : {"field" : "modified","interval" : "month" }}
    },
    "size":0
}')

For more information on Site search, please see the Site Search documentation.

To preview an example of a site search query on our demo site, please visit http://demo.dotcms.com/home/site-search?q=dotcms&search=Search.