The Terms Component provides access to the indexed terms in a field and the number of documents that match each term. This can be useful for building an auto-suggest feature or any other feature that operates at the term level instead of the search or document level. Retrieving terms in index order is very fast since the implementation directly uses Lucene’s TermEnum to iterate over the term dictionary.
In a sense, this search component provides fast field-faceting over the whole index, not restricted by the base query or any filters. The document frequencies returned are the number of documents that match the term, including any documents that have been marked for deletion but not yet removed from the index.
Configuring the Terms Component
By default, the Terms Component is already configured in solrconfig.xml
for each collection.
Defining the Terms Component
Defining the Terms search component is straightforward: simply give it a name and use the class solr.TermsComponent
.
<searchComponent name="terms" class="solr.TermsComponent"/>
This makes the component available for use, but by itself will not be useable until included with a request handler.
Using the Terms Component in a Request Handler
The terms component is included with the /terms
request handler, which is among Solr’s out-of-the-box request handlers - see Implicit RequestHandlers.
Note that the defaults for this request handler set the parameter "terms" to true, which allows terms to be returned on request. The parameter "distrib" is set to false, which allows this handler to be used only on a single Solr core.
You could add this component to another handler if you wanted to, and pass "terms=true" in the HTTP request in order to get terms back. If it is only defined in a separate handler, you must use that handler when querying in order to get terms and not regular documents as results.
Terms Component Parameters
The parameters below allow you to control what terms are returned. You can also configure any of these with the request handler if you’d like to set them permanently. Or, you can add them to the query request. These parameters are:
Parameter | Required | Default | Description |
---|---|---|---|
terms |
No |
false |
If set to true, enables the Terms Component. By default, the Terms Component is off. Example: |
terms.fl |
Yes |
null |
Specifies the field from which to retrieve terms. Example: |
terms.list |
No |
null |
Fetches the document frequency for a comma delimited list of terms. Terms are always returned in index order. If ‘terms.ttf’ is set to true, also returns their total term frequency. If multiple ‘terms.fl’ are defined, these statistics will be returned for each term in each requested field. Example: |
terms.limit |
No |
10 |
Specifies the maximum number of terms to return. The default is 10. If the limit is set to a number less than 0, then no maximum limit is enforced. Although this is not required, either this parameter or Example: |
terms.lower |
No |
empty string |
Specifies the term at which to start. If not specified, the empty string is used, causing Solr to start at the beginning of the field. Example: |
terms.lower.incl |
No |
true |
If set to true, includes the lower-bound term (specified with Example: |
terms.mincount |
No |
null |
Specifies the minimum document frequency to return in order for a term to be included in a query response. Results are inclusive of the mincount (that is, >= mincount). Example: |
terms.maxcount |
No |
null |
Specifies the maximum document frequency a term must have in order to be included in a query response. The default setting is -1, which sets no upper bound. Results are inclusive of the maxcount (that is, ⇐ maxcount). Example: |
terms.prefix |
No |
null |
Restricts matches to terms that begin with the specified string. Example: |
terms.raw |
No |
false |
If set to true, returns the raw characters of the indexed term, regardless of whether it is human-readable. For instance, the indexed form of numeric numbers is not human-readable. Example: |
terms.regex |
No |
null |
Restricts matches to terms that match the regular expression. Example: |
terms.regex.flag |
No |
null |
Defines a Java regex flag to use when evaluating the regular expression defined with
Example: |
terms.stats |
No |
null |
Include index statistics in the results. Currently returns only the numDocs for a collection. When combined with terms.list it provides enough information to compute idf for a list of terms. |
terms.sort |
No |
count |
Defines how to sort the terms returned. Valid options are count, which sorts by the term frequency, with the highest term frequency first, or index, which sorts in index order. Example: |
terms.ttf |
No |
false |
If set to true, returns both 'df' (docFreq) and 'ttf' (totalTermFreq) statistics for each requested term in ‘terms.list’. In this case, the response format is:
|
terms.upper |
No |
null |
Specifies the term to stop at. Although this parameter is not required, either this parameter or Example: |
terms.upper.incl |
No |
false |
If set to true, the upper bound term is included in the result set. The default is false. Example: |
The output is a list of the terms and their document frequency values. See below for examples.
Examples
All of the following sample queries work with Solr’s “bin/solr -e techproducts” example.
Get Top 10 Terms
This query requests the first ten terms in the name field: http://localhost:8983/solr/techproducts/terms?terms.fl=name
Results:
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">2</int>
</lst>
<lst name="terms">
<lst name="name">
<int name="one">5</int>
<int name="184">3</int>
<int name="1gb">3</int>
<int name="3200">3</int>
<int name="400">3</int>
<int name="ddr">3</int>
<int name="gb">3</int>
<int name="ipod">3</int>
<int name="memory">3</int>
<int name="pc">3</int>
</lst>
</lst>
</response>
Get First 10 Terms Starting with Letter 'a'
This query requests the first ten terms in the name field, in index order (instead of the top 10 results by document count): http://localhost:8983/solr/techproducts/terms?terms.fl=name&terms.lower=a&terms.sort=index
Results:
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">0</int>
</lst>
<lst name="terms">
<lst name="name">
<int name="a">1</int>
<int name="all">1</int>
<int name="apple">1</int>
<int name="asus">1</int>
<int name="ata">1</int>
<int name="ati">1</int>
<int name="belkin">1</int>
<int name="black">1</int>
<int name="british">1</int>
<int name="cable">1</int>
</lst>
</lst>
</response>
SolrJ Invocation
SolrQuery query = new SolrQuery();
query.setRequestHandler("/terms");
query.setTerms(true);
query.setTermsLimit(5);
query.setTermsLower("s");
query.setTermsPrefix("s");
query.addTermsField("terms_s");
query.setTermsMinCount(1);
QueryRequest request = new QueryRequest(query);
List<Term> terms = request.process(getSolrClient()).getTermsResponse().getTerms("terms_s");
Using the Terms Component for an Auto-Suggest Feature
If the Suggester doesn’t suit your needs, you can use the Terms component in Solr to build a similar feature for your own search application. Simply submit a query specifying whatever characters the user has typed so far as a prefix. For example, if the user has typed "at", the search engine’s interface would submit the following query:
http://localhost:8983/solr/techproducts/terms?terms.fl=name&terms.prefix=at
Result:
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
</lst>
<lst name="terms">
<lst name="name">
<int name="ata">1</int>
<int name="ati">1</int>
</lst>
</lst>
</response>
You can use the parameter omitHeader=true
to omit the response header from the query response, like in this example, which also returns the response in JSON format: http://localhost:8983/solr/techproducts/terms?terms.fl=name&terms.prefix=at&indent=true&wt=json&omitHeader=true
Result:
{
"terms": {
"name": [
"ata",
1,
"ati",
1
]
}
}
Distributed Search Support
The TermsComponent also supports distributed indexes. For the /terms
request handler, you must provide the following two parameters:
Parameter | Description |
---|---|
shards |
Specifies the shards in your distributed indexing configuration. For more information about distributed indexing, see Distributed Search with Index Sharding. |
shards.qt |
Specifies the request handler Solr uses for requests to shards. |