Any Solrj API to obtain field list?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Any Solrj API to obtain field list?

T. Kuro Kurosaka-2
I'd like to write Solr client code that writes text to language specific
field, say, myfield_es, for Spanish,
if the field myfield_es is defined in schema.xml, and otherwise to a
fall-back field myfield. To do this,
I need to obtain a list of defined fields (and dynamic fields) from the
server. But I cannot find
a suitable Solrj API. Is there any? I'm using Solr 4.6.1. I could write
code to use Schema REST API
(https://wiki.apache.org/solr/SchemaRESTAPI) but I would much prefer to use
the existing code if one exists.

--
T. "Kuro" Kurosaka • Senior Software Engineer


Reply | Threaded
Open this post in threaded view
|

Re: Any Solrj API to obtain field list?

Sujit Pal
Have you looked at IndexSchema? That would offer you methods to query index
metadata using SolrJ.

http://lucene.apache.org/solr/4_7_2/solr-core/org/apache/solr/schema/IndexSchema.html

-sujit



On Tue, May 27, 2014 at 1:56 PM, T. Kuro Kurosaka <[hidden email]>wrote:

> I'd like to write Solr client code that writes text to language specific
> field, say, myfield_es, for Spanish,
> if the field myfield_es is defined in schema.xml, and otherwise to a
> fall-back field myfield. To do this,
> I need to obtain a list of defined fields (and dynamic fields) from the
> server. But I cannot find
> a suitable Solrj API. Is there any? I'm using Solr 4.6.1. I could write
> code to use Schema REST API
> (https://wiki.apache.org/solr/SchemaRESTAPI) but I would much prefer to
> use
> the existing code if one exists.
>
> --
> T. "Kuro" Kurosaka • Senior Software Engineer
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Any Solrj API to obtain field list?

iorixxx
In reply to this post by T. Kuro Kurosaka-2
Hi,

https://wiki.apache.org/solr/LukeRequestHandler  make sure numTerms=0 for performance 



On Tuesday, May 27, 2014 11:56 PM, T. Kuro Kurosaka <[hidden email]> wrote:



I'd like to write Solr client code that writes text to language specific
field, say, myfield_es, for Spanish,
if the field myfield_es is defined in schema.xml, and otherwise to a
fall-back field myfield. To do this,
I need to obtain a list of defined fields (and dynamic fields) from the
server. But I cannot find
a suitable Solrj API. Is there any? I'm using Solr 4.6.1. I could write
code to use Schema REST API
(https://wiki.apache.org/solr/SchemaRESTAPI) but I would much prefer to use
the existing code if one exists.

--
T. "Kuro" Kurosaka • Senior Software Engineer
Reply | Threaded
Open this post in threaded view
|

Re: Any Solrj API to obtain field list?

Jack Krupansky-2
In reply to this post by T. Kuro Kurosaka-2
You might consider an update request processor as an alternative. It runs on
the server and might be simpler. You can even use the stateless script
update processor to avoid having to write any custom Java code.

-- Jack Krupansky

-----Original Message-----
From: T. Kuro Kurosaka
Sent: Tuesday, May 27, 2014 4:56 PM
To: '[hidden email]'
Subject: Any Solrj API to obtain field list?

I'd like to write Solr client code that writes text to language specific
field, say, myfield_es, for Spanish,
if the field myfield_es is defined in schema.xml, and otherwise to a
fall-back field myfield. To do this,
I need to obtain a list of defined fields (and dynamic fields) from the
server. But I cannot find
a suitable Solrj API. Is there any? I'm using Solr 4.6.1. I could write
code to use Schema REST API
(https://wiki.apache.org/solr/SchemaRESTAPI) but I would much prefer to use
the existing code if one exists.

--
T. "Kuro" Kurosaka • Senior Software Engineer

Reply | Threaded
Open this post in threaded view
|

Re: Any Solrj API to obtain field list?

T. Kuro Kurosaka-2
On 05/27/2014 02:29 PM, Jack Krupansky wrote:
> You might consider an update request processor as an alternative. It
> runs on the server and might be simpler. You can even use the
> stateless script update processor to avoid having to write any custom
> Java code.
>
> -- Jack Krupansky

That's an interesting approach. I'd consider it.


On 05/27/2014 02:04 PM, Sujit Pal wrote:
> Have you looked at IndexSchema? That would offer you methods to query index
> metadata using SolrJ.
>
> http://lucene.apache.org/solr/4_7_2/solr-core/org/apache/solr/schema/IndexSchema.html
>
> -sujit
The question was essentially how to get IndexSchema for Solrj client,
without needing to parse the XML file, hopefully.


On 05/27/2014 02:16 PM, Ahmet Arslan wrote:
> Hi,
>
> https://wiki.apache.org/solr/LukeRequestHandler   make sure numTerms=0 for performance

I'm afraid this won't work because when the index is empty, Luke won't
return any fields.
And for the fields that are written, this method returns more
information than I'd like to know.
I just want to know if a field is valid or not.


Kuro

Reply | Threaded
Open this post in threaded view
|

Re: Any Solrj API to obtain field list?

sarowe
You can call the Schema API from SolrJ - see Shawn Heisey’s example code here: <http://mail-archives.apache.org/mod_mbox/lucene-solr-user/201307.mbox/%3c51DAECD2.6030702@...%3e>

Steve

On May 27, 2014, at 5:50 PM, T. Kuro Kurosaka <[hidden email]> wrote:

> On 05/27/2014 02:29 PM, Jack Krupansky wrote:
>> You might consider an update request processor as an alternative. It runs on the server and might be simpler. You can even use the stateless script update processor to avoid having to write any custom Java code.
>>
>> -- Jack Krupansky
>
> That's an interesting approach. I'd consider it.
>
>
> On 05/27/2014 02:04 PM, Sujit Pal wrote:
>> Have you looked at IndexSchema? That would offer you methods to query index
>> metadata using SolrJ.
>>
>> http://lucene.apache.org/solr/4_7_2/solr-core/org/apache/solr/schema/IndexSchema.html
>>
>> -sujit
> The question was essentially how to get IndexSchema for Solrj client,
> without needing to parse the XML file, hopefully.
>
>
> On 05/27/2014 02:16 PM, Ahmet Arslan wrote:
>> Hi,
>>
>> https://wiki.apache.org/solr/LukeRequestHandler   make sure numTerms=0 for performance
>
> I'm afraid this won't work because when the index is empty, Luke won't return any fields.
> And for the fields that are written, this method returns more information than I'd like to know.
> I just want to know if a field is valid or not.
>
>
> Kuro
>

Reply | Threaded
Open this post in threaded view
|

Re: Any Solrj API to obtain field list?

T. Kuro Kurosaka-2
On 05/27/2014 02:55 PM, Steve Rowe wrote:
> You can call the Schema API from SolrJ - see Shawn Heisey’s example code here:<http://mail-archives.apache.org/mod_mbox/lucene-solr-user/201307.mbox/%3c51DAECD2.6030702@...%3e>  
>
> Steve
It looks like this returns a Json representation of fields if I do
     query.setRequestHandler("/schema/fields");

I guess this is the closest Solrj can do.

Thank  you, Steve.

Reply | Threaded
Open this post in threaded view
|

Re: Any Solrj API to obtain field list?

sarowe
Shawn’s code shows that SolrJ parses the JSON for you into NamedList (response.getResponse()). - Steve

On May 27, 2014, at 7:11 PM, T. Kuro Kurosaka <[hidden email]> wrote:

> On 05/27/2014 02:55 PM, Steve Rowe wrote:
>> You can call the Schema API from SolrJ - see Shawn Heisey’s example code here:<http://mail-archives.apache.org/mod_mbox/lucene-solr-user/201307.mbox/%3c51DAECD2.6030702@...%3e>  
>> Steve
> It looks like this returns a Json representation of fields if I do
>    query.setRequestHandler("/schema/fields");
>
> I guess this is the closest Solrj can do.
>
> Thank  you, Steve.
>

Reply | Threaded
Open this post in threaded view
|

Re: Any Solrj API to obtain field list?

T. Kuro Kurosaka-2
On 05/27/2014 04:21 PM, Steve Rowe wrote:
> Shawn’s code shows that SolrJ parses the JSON for you into NamedList (response.getResponse()). - Steve
Thank you for pointing it out.

It wasn't apparent what get(key) returns since the method signature
of getResponse() merely tells it would return a NamedList<Object>.
After running a test code under debugger, I found out, for key="fields",
the returned object is of ArrayList<SimpleOrderedMap<NameValuePair>>.
This is what I came up with:

     private static final String url = "http://localhost:8983/solr/hlbase";
     private static final SolrServer server = new HttpSolrServer(url);
    ...
     SolrQuery query = new SolrQuery();
     query.setRequestHandler("/schema/fields");
     QueryResponse response = server.query(query);

     List<SimpleOrderedMap<NameValuePair>> fields =
(ArrayList<SimpleOrderedMap<NameValuePair>>)
response.getResponse().get("fields");
     for(SimpleOrderedMap<NameValuePair> fmap : fields) {
       System.out.println(fmap.get("name"));
     }


Kuro