How to exclude fields in a SOLR query
Asked Answered
M

4

12

I have a SOLR query which should fetch all the fields I store, except one field.
Say I have 20 fields, do I need to hard code the 19 fields I want to fetch in the
&fl=[f],[f],[f],....[f]'
Or is there a way to do something similar to
&fl=*,![f]'

[f] stands for a field name.

Monthly answered 19/4, 2013 at 16:28 Comment(0)
B
16

Unfortunately the ability to remove a field name via the query string is still an outstanding improvement request. Please see SOLR-3191 for more details.

Until this improvement is implemented, you will need to specify all 19 fields in the fl parameter. However, you could update your default /select requestHandler to define the 19 fields that you want to return as a default setting that would be applied to all queries unless it was overridden in the query string.

Here is a modified version of the default /select requestHandler from the example solrconfig.xml:

  <requestHandler name="/select" class="solr.SearchHandler">
    <!-- default values for query parameters can be specified, these
     will be overridden by parameters in the request
     -->
    <lst name="defaults">
      <str name="echoParams">explicit</str>
      <int name="rows">10</int>
      <str name="df">text</str>
      <!-- Only showing 3 fields for this example -->
      <str name="fl">field1,field2,field3</str>
    </lst>
  </requestHandler>

For more details about these default settings and requestHandler configuration, please refer to RequestHandlers and SearchComponents in SolrConfig.

Brill answered 19/4, 2013 at 17:24 Comment(1)
I wish having this functionality.Fia
L
8

Another option could be: fields you want to include should share a common prefix; on top of that you can use globs in your fl. So for example, assuming that prefix is tobeincluded_ you can have a fl value like

tobeincluded_*

Lair answered 28/6, 2015 at 20:23 Comment(0)
H
3

Another option would be using the Document transformer ValueAugmenterFactory to replace the actual value returned of the field by an empty string.

This should work if your requirement is simply to avoid returning the content of that field.

Below is a example replacing the content of the title field:

&fl=*,title:[value v=""]
Heeltap answered 7/10, 2016 at 21:25 Comment(0)
H
1

There is another simpler patch available that solves the simple case here: https://issues.apache.org/jira/browse/SOLR-9467 if accepted it will work like this:

&fl=*,[fl.rm v="title"]

To remove the title field.

Hedvah answered 2/9, 2016 at 21:15 Comment(4)
Disclaimer: I should probably mention I wrote that patch.Hedvah
let us know when/if get accepted. I am not familiar with the Lucen/SOLR architecture, but can't you publish this as a stand alone plugin that can be dropped into place?Monthly
I think it might be possible to package up the main class and use this in a request handler configuration, but to get it to show up for use in &fl I had to add the name to a class in solr. However, the patch applies cleanly to 6.2 branch as of yesterday so if you check that out from apache git apply and build with ant create-package in the solr dir that should create basically the same tarball as 6.2 RC1 with this added.Hedvah
downvote?? patch never went forward because 3191 was supposedly "done and ready" (I actually plan to look at and push for 3191 soon), but it did work.Hedvah

© 2022 - 2024 — McMap. All rights reserved.