I need to specify df parameter in the query. I am using SolrQuery class from SolrJ.
Is there a predefined method to set default search filed in SolrQuery class?
You can try and build the solr query using Solrj as below.
final SolrQuery query = new SolrQuery();
query.setQuery("title:bloomberg");
query.setParam("debugQuery", "on");
query.add("rows", "4");
query.add("rq", "{!ltr reRankDocs=4 model=6029760550880411648}");
query.add("fl", "*,score");
query.add("wt", "json");
query.addFilterQuery("name : tester1");
Also you can use the CommonParams API
solrQuery.setParam(CommonParams.Q, queryString);
solrQuery.setParam(CommonParams.DF, fieldName);
CommonParams Api link
Related
I have is this:
public class Product {
#Field("object_id")
private String objectId;
private List<MyObject> listOfMyObjects;
}
I use SolrJ to save the info. How can I make listOfMyObjects look like list of nested documents in Solr response. I can make the field multivalued, but I need the list to be list of documents.
I can see that this question is asked few times(e.g. Solrj Block Join Bean support ) but no answer. Solr supports nested documents, but how to make it happen using SolrJ with annotations and schema.xml.
I want to update a particular field of a document in solr. However while trying to do so other
fields of the document are becoming null.
CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://mydomain:8983/solr/index_socialmedia/");
List<SolrInputDocument> solrInputDocsList = new ArrayList<SolrInputDocument>();
SolrInputDocument solrInputDoc = new SolrInputDocument();
solrInputDoc.addField("id","427832898745234516478592");
solrInputDoc.addField("city","Kolkata");
solrInputDocsList.add(solrInputDoc);
server.add(solrInputDocsList);
server.commit();
In the above code the "id" field is the unique field.
How can I fix this problem.
I tried with the following code snippet. It's able to do partial update. Try this.
SolrServer server = new HttpSolrServer("http://mydomain:8983/solr/index_socialmedia/");
SolrInputDocument solrInputDoc = new SolrInputDocument();
Map<String, String> update = new HashMap<String, String>();
update.put("set", "Kolkata");
solrInputDoc.addField("id","427832898745234516478592");
solrInputDoc.addField("city", update);
server.commit();
Set <uniqueKey>id</uniqueKey> in your solr schema.xml
Which solr version are you using ?
if it is bellow 4.0 ,solr does not support single field update (partial update)
so in your case existing old document is deleted and new document is inserted with same id
(Reason Solr does not support single field or partial update is lucene does not support partial update )
If you using Solr 4.0 you can refer this solrj api for partial document update
I want to make a simple query with a getValueByLabel Method:
Here is my code:
public Config getValueByLabel(String label) throws EntityPersistException{
try {
Query query = em.createQuery("select id from config where config_label=:label",Long.class);
query.setParameter("label", label);
List<Long> config = query.getResultList();
return em.getReference(Config.class, config.get(0));
}
...
when I want to start the method I get:
org.hibernate.hql.internal.ast.QuerySyntaxException: config is not mapped [select id from config where config_label=:label]
Any ideas how to fix that?
UPDATE
I am using:
hibernate 4.0.1.Final
and a postgresql db 1.16.1
syntax of hql is case sensitive. please see if table/entity and column/instance variable names used in query are same as that of object.
Solr 4.x has this nice new feature that lets you specify how, when doing an update on an existing document, the multiValued fields will be updated. Specifically, you can say if the update document will replace the old values of a multivalued field with the new ones, or if it should append the new values to the existing ones.
I've tried this using the request handler, as described here:
http://wiki.apache.org/solr/UpdateXmlMessages#Optional_attributes_for_.22field.22
I've used curl to send xml where some fields used the update=add option:
<field name="skills" update="add">Python</field>
This works as expected.
However I can't get how to do this the Java API (SolrJ).
If I do something like this:
SolrInputDocument doc1 = new SolrInputDocument();
doc1.setField("ID_VENTE", "idv1");
doc1.setField("FACTURES_PRODUIT", "fp_initial");
solrServer.add(doc1);
solrServer.commit();
SolrInputDocument doc2 = new SolrInputDocument();
doc2.setField("ID_VENTE", "idv1");
doc2.setField("FACTURES_PRODUIT", "fp_2");
solrServer.add(doc2);
solrServer.commit();
The value for the field "FACTURES_PRODUIT" becomes "fp_2" (the initial value is lost).
I've also tried:
doc2.addField("FACTURES_PRODUIT", "fp_2");
but the result is the same. I've also looked into the SolrInputField class but haven't found anything similar to this.
So, my question is, how can I use the Solr 4 Java API to updated values into a multiValued field by appening (not replacing) the new values?
Ok, I solved it after debugging a bit the SolrJ code. You have to do this:
SolrInputDocument doc2 = new SolrInputDocument();
Map<String,String> fpValue2 = new HashMap<String, String>();
fpValue2.put("add","fp2");
doc2.setField("FACTURES_PRODUIT", fpValue2);
I have the following query which I took from my URL
public static String query="pen&mq=pen&f=owners%5B%22abc%22%5D&f=application_type%5B%22cde%22%5D";
public static String q="pen";
I parsed my query string and took each facetname and facet value from it and stored in a map
String querydec = URLDecoder.decode(query, "UTF-8");
String[] facetswithval = querydec.split("&f=");
Map<String, String> facetMap = new HashMap<String, String>();
for (int i = 1; i < facetswithval.length; i++) {
String[] fsplit = facetswithval[i].split("\\[\"");
String[] value = fsplit[1].split("\"\\]");
facetMap.put(fsplit[0], value[0]);
}
Then i use the following code to query in solr using solrj
CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://localhost:8983/solr/");
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery(q);
for (Iterator<String> iter = facetMap.keySet().iterator(); iter.hasNext();){
String key=iter.next();
System.out.println("key="+key+"::value="+facetMap.get(key));
solrQuery.setFilterQueries(key+":"+facetMap.get(key));
}
solrQuery.setRows(MAX_ROW_NUM);
QueryResponse qr = server.query(solrQuery);
SolrDocumentList sdl = qr.getResults();
But after running my code I found out that solrQuery.setFilterQuery method is setting filter for only last set facet. That means if i m running the loop and using this function three times it is taking the last set filter values only.
Can somebody please clarify this and tell me better approach for doing this. Also I am decoding url. So, if my facet contains some special character in the middle then i am not getting any result for that. I tried using it without encoding also but it didnt work. :(
There is also a addFilterQuery method, I would call that since you are setting the filter queries individually in your for loop.
Also, please see this post Filter query with special character using SolrJ client from the Solr Users Mailing List about the need to still escape special characters in queries.