Solr Spatial - Indexing bean - solr

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

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?

Sitecore 9 Indexing : Solr Pattern Tokenizer not Working

I'm new with this combination sitecore and solr stuff.. I've a little issue with the pattern tokenizer which is not working.. I'm following this documentation
Solr :
https://lucene.apache.org/solr/guide/6_6/tokenizers.html#Tokenizers-RegularExpressionPatternTokenizer)
Sitecore 9 Solr :
https://doc.sitecore.net/sitecore_experience_platform/setting_up_and_maintaining/search_and_indexing/using_solr_field_name_resolution
When I do the indexing, my field value is : a,b,c and I expected on solr it will be ["a","b","c"] but it contains ["a,b,c"]
This is my Sitecore Config
<fieldMap>
<typeMatches hint="raw:AddTypeMatch">
<typeMatch type="System.Collections.Generic.List`1[System.String]" typeName="commaDelimitedCollection" fieldNameFormat="{0}_cd"
multiValued="true" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider"/>
</typeMatches>
<fieldNames hint="raw:AddFieldByFieldName">
<field fieldName="Keywords" returnType="commaDelimitedCollection"/>
</fieldNames>
</fieldMap>
This is my Solr Schema
<fieldType name="commaDelimited" class="solr.TextField" multiValued="true">
<analyzer>
<tokenizer class="solr.PatternTokenizerFactory" pattern="\s*,\s*"/>
</analyzer>
</fieldType>
<dynamicField name="*_cd" type="commaDelimited" multiValued="true" indexed="true" stored="true"/>
Any idea what's wrong with my configuration above?
Thanks
Not sure if I get the full picture here. Maybe your approach is perfectly valid, but I don't think I've seen that one before. Instead of defining a new type, you could reuse the *_sm (multiValued string) and perform the splitting of the string at index time on the Sitecore side. Usually you don't need more field types than the ones provided by sitecore and it's typically easier to maintain all the code in your VS solution instead of depending on additional Solr config. (In Sitecore 9 you can deploy your Solr managed schema from the control panel though.)
A simple computed field field can look like this:
<fields hint="raw:AddComputedIndexField">
<field fieldName="keywords" returnType="stringCollection">
Your.Name.Space.YourComputedFieldClass, YourAssembly
</field>
</fields>
And a class implementation could look something like this:
public class YourComputedFieldClass : IComputedIndexField
{
public object ComputeFieldValue(IIndexable indexable)
{
var item = indexable as SitecoreIndexableItem;
var fieldValue = item?.Item?["Keywords"]
if (string.IsNullOrWhitespace(fieldValue)) {
return null;
}
return fieldValue.Split(',');
}
public string FieldName { get; set; }
public string ReturnType { get; set; }
}

Solr TextField to Java #Field Mapping

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.

Solr geographical search

i am testing solr query for geographical search, this is my query:
SolrQuery query =new SolrQuery();
query.setParam("q","*:*");
query.setParam("fq","geofilt");
query.setParam("d","100000");
query.setParam("pt","51.53750834,-0.19329616");
query.setParam("sfield","location_s");
i am getting no results although there is very near points and also exact point to the pt.
any idea whats the reason??
hint: im using this field type for spatial search (the one comes in the schema.xml by default):
<fieldType name="location_rpt" class="solr.SpatialRecursivePrefixTreeFieldType"
geo="true" distErrPct="0.025" maxDistErr="0.000009" units="degrees" />
because when i try to use this one as mentioned in the solr website i get an error:
<fieldType name="location_rpt" class="solr.SpatialRecursivePrefixTreeFieldType" spatialContextFactory="com.spatial4j.core.context.jts.JtsSpatialContextFactory"
autoIndex="true"
distErrPct="0.025"
maxDistErr="0.000009"
units="degrees" />
and this is my field definition:
<field name="location_s" type="location_rpt" indexed="true" stored="true"/>
thanks in advance!
instead of this kind of field names please add only 1 field that served for both x/y :
<dynamicField name="*_coordinate" type="tdouble" indexed="true" stored="true"/>
That will allow you to put directly datas in solr geo format :
"env": "DEV",
"latlgn_0_coordinate": -2.6263,
"latlgn_1_coordinate": -44.1978,
please take a look to both solr spatialsearch & solr wiki spatialsearch
As said above, please make sure to have in your runtime classpath.
You might download and install in your path the JTS library : JTS Library
Solr Manual solr install documentation
The JTS jar file must be on Solr's classpath as well. Due to a
combination of things, JTS can't simply be referenced by a ""
entry in solrconfig.xml; it needs to be in WEB-INF/lib in Solr's war
file, basically.
enjoy :)
Thanks jean, it worked with me without using JTS library, solr schema comes already with this field type that I used:
<fieldType name="location_rpt" class="solr.SpatialRecursivePrefixTreeFieldType"
geo="true" distErrPct="0.025" maxDistErr="0.000009" units="degrees" />
I defined a field of that type and it worked. This is my query:
SolrQuery query =new SolrQuery();
query.set("q","*:*");
query.set("fq","{!geofilt}");
query.set("pt","32.014708,35.873725");
query.set("sfield","location");
query.set("d","100");
Thanks.

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