26

How can I query a Solr instance for all (or prefixed) field names?

I want to use dynamic fields where I do not know how many may exist e.g: category_0_s, category_1_s etc.

Preferably I'd like to use a prefix e.g. category_

xlm
  • 6,854
  • 14
  • 53
  • 55
floplus
  • 315
  • 1
  • 3
  • 7

3 Answers3

51

This query will return a comma-separated list of all the fields that are in use, including dynamic ones.

select?q=*:*&wt=csv&rows=0&facet

To answer the original question, this is how to get a list of all the fields starting with category_

select?q=*:*&wt=csv&rows=0&facet&fl=category_*

The presence of the facet parameter is needed to make this query work on newer versions of Solr. On older versions, it will work without it.

On older versions, a wildcard in the fl parameter won't work.

GoalBased
  • 1,810
  • 1
  • 14
  • 12
  • 2
    In Solr5, including both `facet` and `facet.field` causes a 400 Bad Request, but still include the fields. Leaving out `facet.field` returns the fields and a 200 Success – Izkata Apr 20 '15 at 19:54
  • @Izkata Thanks for the improvement! I've updated my answer. Curiously, setting `facet=true` doesn't work, but `facet` with no value works. – GoalBased Apr 21 '15 at 03:11
  • On Solr 6.3 only fl=id,category... worked without the facet=on. I am wondering if i still need to add something in the schema to show addition info – Gadelkareem Dec 13 '16 at 20:24
  • 2
    Bizarre, why does this only work with csv as return format? Why can't I get this as JSON? (Solr newb here) – K.-Michael Aye Apr 27 '18 at 04:10
  • 1
    @K.-MichaelAye because JSON has no column titles. By asking for 0 rows this returns the CSV column titles only. It's evil genius. – James M May 13 '20 at 15:10
25

Use the luke handler:

http://solr:8983/solr/admin/luke?numTerms=0

Use a xpath to get all the field which has the tag dynamicBase matching the dynamic fields definition you are looking for.

Pascal Dimassimo
  • 6,908
  • 1
  • 37
  • 34
  • @Philippe That won't get the names of all the dynamic fields in use, which is what the original poster asked for. Read the question and answer carefully. – GoalBased Apr 04 '15 at 14:39
  • @GoalBased Ok, but if instead of criticizing you gave a better solution, that would help as well. So to update my "answer", the correct call for dynamic fields is http://host:/solr/collection/schema/dynamicfields. For more options see https://cwiki.apache.org/confluence/display/solr/Schema+API – Philippe Apr 04 '15 at 14:51
  • I posted an answer below that doesn't require Luke – GoalBased Apr 09 '15 at 18:35
  • How would I get a list of all fields using this term, and still apply fq on the same? – Krunal Dec 07 '18 at 11:00
1

If you need to do this from your Java application you can use Solrj and LukeRequestRequestHandler.

Your code would look like this.

LukeRequest lukeRequest = new LukeRequest();
lukeRequest.setNumTerms(0);

LukeResponse lukeResponse = lukeRequest.process(server);

Map<String, FieldInfo> fieldInfoMap = lukeResponse.getFieldInfo();

for (Entry<String, FieldInfo> entry : fieldInfoMap.entrySet()) {

    String fieldName = entry.getKey();
    FieldInfo fieldInfo = entry.getValue();

    // process fieldInfo    
}
cjungel
  • 3,701
  • 1
  • 25
  • 19