Time for action – projecting fields with fl

Another important parameter to consider is fl, that can be used for fields projection, obtaining only certain fields in the results:

If we are interested on obtaining the titles and artist reference for all the documents, we can use the following command:

>>curl -X GET 'http://localhost:8983/solr/paintings/select?q=artist:*&wt=json&indent=true&omitHeader=true&fl=title,artist'

The output for the command is similar to the one shown in the following screenshot:

Time for action – projecting fields with fl
  1. Notice that the results will be indented as requested, and they will not contain any header to be more readable. Moreover, the parameters list does not need to be written in a specific order.
  2. The previous query can also be rewritten:
    >>curl -X GET 'http://localhost:8983/solr/paintings/select?q=artist:*&wt=json&indent=true&omitHeader=true&fl=title&fl=artist'
    

Here we ask for field projection one by one, if needed. For example, when using HTML and JavaScript widget to compose the query following user's choices.

What just happened?

The fl parameter stands for the fields list. Using this parameter, we can define a comma-separated list of fields names that explicitly define what fields are projected in the results. We can also use a space to separate fields; but in this case we should use the URL encoding for the space, writing fl=title+artist or fl=title%20artist.

Tip

If you are familiar with relational databases and SQL, you should consider the fl parameter. It is similar to the SELECT clause in SQL statements, which is used to project the selected fields in the results. Similarly, writing fl=author:artist,title corresponds to the usage of aliases. For example, SELECT artist AS author, title.

Let's see the full list of parameters in details:

  • The parameter q=artist:* is used in this case in place of a more generic q=*:* to select only the fields that have a value for the field artist. The special character * is used again for indicating all the values.
  • The wt=json, indent=true parameters are used for asking an indented json format.
  • The omitHeader=true parameter is used for omitting the header from the response.
  • The fl=title,artist parameter represents the list of the fields to be projected for the results.

Notice how the fields are projected in the results without using the order asked in fl, as this has no particular sense for json output. This order will be used for the CSV response writer (we will see this later) where changing the columns order could be mandatory.

In addition to the existing field, which can be added by using the * special character, we could also ask for the projection of the implicit score field. A composition of these two options can be seen in the following query:

>>curl -X GET 'http://localhost:8983/solr/paintings/select?q=artist:*&wt=json&indent=true&omitHeader=true&fl=*,score'

This will return every field for every document, including the score field explicitly, which is sometimes called a pseudo-field to distinguish it from the field defined by a schema.

Introducing pseudo-fields and DocTransformers

When using fields list, we can also use transformers to manipulate a document while returning it. In this way, we can implement and use a custom transformer or use some of the predefined ones, for example, [docid], [explain], and [shard]. Each of these should be considered as a pseudo-field. For example, [docid] can be used to return the explicit value of the internal ID of a document; and [shard] is used for knowing where to find it(in which shard inside a distributed system). We will see this later in Chapter 8, Indexing External Data Sources.

In this short list, we can use [explain] in particular to obtain detailed information about how the matching of a document has been calculated:

>> curl -X GET 'http://localhost:8983/solr/paintings/select?q=abstract:dali+vermeer&wt=json&indent=true&fl=[explain+style=nl]'

It helps in obtaining the result shown in the following snippet:

Introducing pseudo-fields and DocTransformers

This example will contain structured information in the details field, which helps us to see how the final match is assembled, and a description field that exposes the internal parsed query.

We will again see the [explain] pseudo-field in action with the debug parameter, which we will use in the Chapter 5, Extending Search.

Adding a constant field using transformers

Sometimes we need to add a constant field value in every document returned, without changing the original data. Think about adding, for example, the request time, or some copyright note.

This can be easily done using the following command:

>> curl -X GET 'http://localhost:8983/solr/paintings_transformers/select?q=*:*&fl=*,[noempty],[source_transformer%20v=dbpedia.org]&wt=json&indent=true'

It can also be done with a specific configuration in solrconfig.xml:

<transformer name="source_transformer" class="org.apache.solr.response.transform.ValueAugmenterFactory" >
  <str name="value">dbpedia.org</int>
</transformer>

This little addition in our configuration can be also useful when we need to add custom code. For a list of available transformers, you can refer to the official wiki page at http://wiki.apache.org/solr/DocTransformers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.206.25