Solr: Retrieve field names from a solr index?

2019-02-11 14:04发布

问题:

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

I want to use dynamic fields like

category_0_s
category_1_s
...

but i do not know how many may exist. So I want to retrieve all fields (preferably with the prefix "category_").

Any Ideas? Thanks

回答1:

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.



回答2:

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.



回答3:

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    
}


标签: solr