Solr TextField to Java #Field Mapping - solr

I am using Solr 5.2.1 (Simple stand alone instance on dev PC) and Running Solrj in a Spring Application.
I have everything working fine (Solrj Client is able to commit and retrieve the domain Model which has been annotated with #Field annotations) however I wanted to use free text search on one of the domain model's fields so I changed its type from String to a Text Field, my understanding is this will allow a more free text approach to the search instead of key:value pair type searches where solr give all or nothing back.
schema.xml snippet
<field name="description" type="text_general" indexed="true" stored="true"/>
Java bean snippet
public String getDescription() {
return description;
}
#Field
public void setDescription(String description) {
this.description = description;
}
The Save side worked fine, I am able to update solr vai the java bean ans solrj and using the http client I can see the data goes in, the problem I have is I can't seem to get the java bean back out when querying. Solr does not like the String based setter when it tries to populate the bean. I get the following error:
Caused by: org.apache.solr.client.solrj.beans.BindingException: Exception while setting value : [PetShop] on public void search.CompanySummarySolr.setDescription(java.lang.String)
INFO [stdout] (http-localhost-127.0.0.1-8080-6) at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.set(DocumentObjectBinder.java:447)
INFO [stdout] (http-localhost-127.0.0.1-8080-6) at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.inject(DocumentObjectBinder.java:430)
INFO [stdout] (http-localhost-127.0.0.1-8080-6) at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBean(DocumentObjectBinder.java:64)
INFO [stdout] (http-localhost-127.0.0.1-8080-6) ... 64 more
INFO [stdout] (http-localhost-127.0.0.1-8080-6) Caused by: java.lang.IllegalArgumentException: argument type mismatch
I understand the error, I just can't find out how one would map the Text Field. In the http client the Json output it looks like an Array, but in the Solr API http://lucene.apache.org/solr/5_2_0/solr-core/org/apache/solr/schema/TextField.html it extends Object not an Array.
All I can see is I need do ether have the field as
#Field
public void setDescription(Object description) {
this.description = (???) description;
}
In which case how do I get it back to a String?
Or I can use the org.apache.solr.schema.TextField
#Field
public void setDescription(TextField description) {
this.description = description.getValueSource(???, ???);
}
But I also don't really understand how to get the String back out for starters, and secondly it looks like I have to pull in the whole solr library into the project, as the TextField object is not in the Solrj library.
I am sure I have missed something, all the other fields map so simply, but I cant find an example of how to get description back to the client as a String and also allow a free text search on this field, as Strings don't allow that type of search as far as I understand.
Any help gladly appreciated

So it would appear Solr defaults TextField to a multi valued field. This means that Solr wraps the String field I passed in with an Array, which causes the Bean Setter to break as Solr wants to return an Array with 1 String instead of a String.
To fix this simply edit your Schema.xml file and explicitly tell Solr text_general is a Single Value, so in my case the description field went
From this:
<field name="description" type="text_general" indexed="true" stored="true"/>
To This:
<field name="description" type="text_general" indexed="true" stored="true" multiValued="false"/>
Now Solr happily converts my response into its component java bean.

Related

How to add new type to Solr?

What is the proper way to add my custom field type, written on Java, extended from Solr's FieldType class?
I've tried to add jar with my type to sole/lib folder, but while creating collection I'm getting
HttpSolrClient$RemoteSolrException:Error from server at http://node:8983/solr: Error CREATEing SolrCore 'custom_type_shard1_replica_n1': Unable to create core [custom_type_shard1_replica_n1] Caused by: com.custom.type.Location"
In FieldType's write() method I just log incoming field for testing purpose.
This is how I define my type an use it in managed_schema file:
<fieldType name="customlocation" class="com.custom.type.Location"/>
...
<field name="location" type="customlocation" indexed="true" stored="false"/>
I've found a couple vvery short tutorials, for example this and this
But is there any official and full tutorial about creating and defining custom field type for Solr?

Solr Spatial - Indexing bean

I need to index on Solr a bean that contains a generic spatial field (generally, a polygon).
I configured my Solr core schema in this way (following the tutorial here):
<fieldType name="area" class="solr.RptWithGeometrySpatialField" spatialContextFactory="org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory"
autoIndex="true"
validationRule="repairBuffer0"
distErrPct="0.025"
maxDistErr="0.001"
distanceUnits="kilometers" />
....
<field name="location" type="area" indexed="true" stored="true" required="true" multiValued="false" />
My bean class is as follows:
public class MySolrBean {
#Field("id")
private String id;
#Field("location")
private Geometry location;
// getters and setters...
}
where Geometry refers to com.vividsolutions.jts.geom.Geometry (jts-1.13)
When I try to add a new bean to the index with SolrClient.addBean(Object) I get the following error:
Unable to parse shape given formats "lat,lon", "x y" or as WKT because java.text.ParseException: Unknown Shape definition [com.vividsolutions.jts.geom.Polygon:POLYGON ((1 0, 0.9980267284282716 0.0627905195293134, 0.9921147013144779 0.12533323356430...]
where WKT representation of my polygon is prefixed by the class fqn. I remember I saw a similar problem some time ago, this time when using ZonedDateTime: I changed my code to use java.util.Date and everything worked.
Though now I would not know which class to use instead of com.vividsolutions.jts.geom.Geometry and surfing the web I didn't find any documentation about that.
Anyone can help me out sorting this issue?
EDIT
Forgot to mention I'm using the latest Solr and Solrj distribution: 6.5.1

org.apache.solr.common.SolrException: ERROR: [doc=SOMEURL] unknown field ''

Hi I am getting this exception and I've exhausted all the possible settings that I could think of.
org.apache.solr.common.SolrException: ERROR: [doc=SOMEURL] unknown field ''
The problem is field '' - the quotation marks are empty so I don't know what causes the problem.
Does anybody had the same problem? I will help me a lot.
Some informations:
Nutch version 2.1
Solr version 1.5
Hbase as a data storage
-Tomcat6 for Solr running
In code have just this:
nutchDocument.add("my_key",stringValue);
I have checked Solr's schema.xml, Nutch's schema.xml and also Nutch solr-mapping.xml (I am sure in the right directories) in each is "my_key" written in the right way.
Thanks for help
Well, I had to be blind. I found where was the problem. For someone who will have the similar problem here is the reason:
In my solrindex-mapping.xml I had this:
<field dest="video_og_title" source="video_og_title" />
<field dest="video_og_type" source="video_og_type"/>
<field dest="video_og_image" source="video_og_image" />
<field name="video_og_url" source="video_og_url"/>
<field name="video_og_description" source="video_og_description" />
<field name="video_og_video" source="video_og_video" />
I didn't see the field has attribute name and not dest so Solr represent the dest attribute, which it uses for mapping like empty field ' '.

400 Bad Request: unknown field 'type'

I've set up Solr 3.6.2 on Tomcat as described here.
Using the sunspot-rails gem and the embedded solr server I have no problems, but on my staging server I'm getting the response:
message ERROR: [doc=Foo 20] unknown field 'type'
description The request sent by the client was syntactically incorrect.
The request data looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<add>
<doc>
<field name="id">Foo 20</field>
<field name="type">Foo</field>
<field name="type">ActiveRecord::Base</field>
<field name="class_name">Foo</field>
<field name="name">test</field>
</doc>
</add>
What's causing this? Is there some configuration that should be set? (I'm expecting something that allows for the type name to be used regardless of whether or not such a column exists.)
It turns out that the sunspot-solr gem expects a slightly different schema.xml than the default that is bundled with solr.
I replaced the file with the one that the gem uses (from here) and it works fine now. This answer explains what the schema.xml file is.

make location fields made visible in solr

I have the following field defined in solr (schema.xml)
<field name="store" type="location" indexed="true" stored="true"/>
If I search for say this-
&fq={!geofilt pt=45.15,-93.85 sfield=store d=5}
Then I can see the location coordinates in the search result.
But the field "store" seems to be a hidden field under normal circumstances. How do I get the coordinates to be a part of the search result for normal searches? (q=*:* for example)
I just verified that this works correctly for both Solr 3.1 and Solr 4.0-dev with the example data.
Example:
http://localhost:8983/solr/select?q=:&fl=id,store&wt=json&indent=true
[...]
"response":{"numFound":17,"start":0,"docs":[
{
"id":"SP2514N",
"store":"35.0752,-97.032"},
{
"id":"6H500F0",
"store":"45.17614,-93.87341"},
{
"id":"F8V7067-APL-KIT",
"store":"45.18014,-93.87741"},
[...]
Did you perhaps change this setting and forget to re-index or forget to commit?

Resources