You need to use <dynamicField> not <field>, that's all :)

     Erik

On Feb 20, 2013, at 4:06, Erik Dybdahl <erik...@gmail.com> wrote:

> Hi,
> I'm currently assessing lucene/solr as a search front end for documents
> currently stored in an rdbms.
> The data has been made searchable to clients, in a way so that each
> client/customer may define what elements of their documents are searchable,
> by defining a field name, and a reference from that name into the data
> which is subsequently used to extract the value from the client documents
> and for each document associate it with the customer chosen field name.
> The concept of dynamic fields seemed to be exactly what I was looking for.
> Under Solr Features, Detailed Features, lucene.apache.org/solr says
> "Dynamic Fields enables on-the-fly addition of new fields".
> And, in more depth, at wiki.apache.org/solr/SchemaXml:
> "One of the powerful features of Lucene is that you don't have to
> pre-define every field when you first create your index.
> :
> For example the following dynamic field declaration tells Solr that
> whenever it sees a field name ending in "_i" which is not an explicitly
> defined field, then it should dynamically create an integer field with that
> name...
>    <dynamicField name="*_i"  type="integer"  indexed="true"
> stored="true"/>"
> Cool.
> However, after definining
>       <field name="customerField_*" type="string" indexed="true"
> stored="true" multiValued="true"/>
> then trying to add the dynamic fields from the values in the db using solrj
> thus
> 
> solrInputDocument.addField("customerField_"+searchFieldResultSet.getString("name"),
> searchFieldResultSet.getString("value"));
> (i.e. attempting to create a dynamic field for e.g. the customer field
> FirstName) yields
> 
> org.apache.solr.common.SolrException: ERROR: [doc=62485318] unknown field
> 'customerField_FirstName'
>    at
> org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:404)
> 
> How come? The documentation states clearly that solr should dynamically
> create a field in such cases.
> I am aware that similar problems have been discussed in several threads
> before, but the appearant complexity of the issue confuses me.
> 
> Is what is stated in the documentation correct?
> In that case, what am I doing wrong, and what is the correct way?
> If the Dynamic Field concept is just a way of specifying common
> characteristics of multiple defined fields, then I think the documentation
> ought to be changed.
> 
> I was able to solve the problem by following a suggestion in one of the
> threads, namely to create just one generic field, and then concatenate name
> and value of the customer field into it, like this:
>   <field name="customerField_s" type="string" indexed="true" stored="true"
> multiValued="true"/>
> and
>        solrInputDocument.addField("customerField_s",
> searchFieldResultSet.getString("name")+"$"+searchFieldResultSet.getString("value"));
> which enables queries like:
>  customerField_s:LastName$PETTERSON
> but that's not a very elegant solution.
> Also, attempting to use customerField_* in the query does not work, and
> from reading the documentation I do not understand why.
> 
> Anyway, the response time when searching for these values is superb, when
> compared to doing the search directly in the database :-)
> 
> Regards
> Erik

Reply via email to