Custom Query Component - How to get "score" from document id? - solr

I'm writing several Solr Custom Query Components.
Each component run different kinds of queries:
Component A: does a group by query field A
Component B: does a group by on a different fild B
Each component will send it's the documents from it's result to the next component.
In my "process" function, I'm donig the following after the result is set by grouping:
IndexSchema schema = searcher.getSchema();
DocListAndSet s = result.getDocListAndSet();
DocSet s3 = s.docSet;
DocIterator dit = s3.iterator()
while (dit.hasNext())
{
SolrDocument doc = new SolrDocument();
int docid = dit.nextDoc();
//float score = dit.score();<--This does not get the score
Document luceneDoc = searcher.doc(docid);//get the document using the doc id
for( Fieldable field : luceneDoc.getFields())
{
SchemaField sf = schema.getField( field.name() );
doc.addField( field.name(), sf.getType().toObject( field ) );
......
}
And then iterating through the Set and createing SolrDocument.
The SolrDocumentes are entered into a SolDocumentList and end I send it off to the next Component:
rb.req.getContext().put("TAG", list);
I also want to add a field called "score" SolrDocument, this field will contain the actual score. I've tried getting the score using:
float score = dit.score()
But the above does not get the score of the document. How do I get the "score" of the document using the document id?

Is there a particular reason you are getting the docSet instead of the docList?
I would try (condensing a bit) getting s.docList.iterator() instead of s.docSet.iterator(). The latter states specifically in the documentation here that you can't get meaningful scores from it, where the docList states it may contains valid scores.

Well you have to set GET_Scores in getDocList(query,List,Lsort,offset,maxnoofdocs,1)
Here
`
query is your query obj
List<Query> your filters could be null
lsort could be null
offset
maxnoofdocs integer
1 means get score with documents`

Related

Get list of parent records from multilevel SOQL query

I'm trying to combine 3 separate SOQL queries into one, but still end up with 3 separate lists for ease of use and readability later.
List<Object__c> objectList = [SELECT Name, Id, Parent_Object__r.Name, Parent_Object__r.Id,
(SELECT Name, Id FROM Child_Objects__r)
FROM Object__c];
I know I can get a list of child objects thus:
List<Child_Object__c> childObjectList = new List<Child_Object__c>();
for(Object__c object : objectList){
childObjectList.addAll(object.Child_Objects__r);
}
How would I go about adding the Parent_Object__c records to their own list?
I'm assuming a map could be used to deal with duplicates, but how do I get this Parent_Object__c data into that map?
You are basically there.
All lookup fields are available in your example as object.Parent_Object__r. Use a Set to natively avoid duplicates. No deduping required on your part!
Set<Parent_Object__c> parentObjectSet = new Set<Parent_Object__c>();
List<Child_Object__c> childObjectList = new List<Child_Object__c>();
for(Object__c object : objectList){
childObjectList.addAll(object.Child_Objects__r);
parentObjectSet.add(object.Parent_Object__r);
}
Edit:
As per #eyescream (trust him!) you are indeed better off with a map to avoid duplicates.
So the above code would just be slightly different:
Map<Id, Parent_Object__c> parentObjectMap = new Map<Id, Parent_Object__c>();
List<Child_Object__c> childObjectList = new List<Child_Object__c>();
for(Object__c object : objectList){
childObjectList.addAll(object.Child_Objects__r);
if (object.Parent_Object__r != null) {
parentObjectMap.put(object.Parent_Object__r.Id, object.Parent_Object__r);
}
}

Apex - Retrieving Records from a type of Map<SObject, List<SObject>>

I am using a lead map where the first id represents an Account ID and the List resembles a list of leads linked to that account such as: Map<id, List<Id> > leadMap = new Map< id, List<id> >();
My question stands as following: Knowing a Lead's Id how do I get the related Account's Id from the map. My code looks something like this, The problems is on the commented out line.
for (Lead l : leads){
Lead newLead = new Lead(id=l.id);
if (l.Company != null) {
// newLead.Account__c = leadMap.keySet().get(l.id);
leads_to_update.add(newLead);
}
}
You could put all lead id and mapping company id in the trigger then get the company id
Map<string,string> LeadAccountMapping = new Map<string,string>();//key is Lead id ,Company id
for(Lead l:trigger.new)
{
LeadAccountMapping.put(l.id,l.Company);
}
//put the code you want to get the company id
string companyid= LeadAccountMapping.get(l.id);
Let me make sure I understand your problem.
Currently you have a map that uses the Account ID as the key to a value of a List of Lead IDs - So the map is -> List. Correct?
Your goal is to go from Lead ID to the Account ID.
If this is correct, then you are in a bad way, because your current structure requires a very slow, iterative search. The correct code would look like this (replace your commented line with this code):
for( ID actID : leadMap.keySet() ) {
for( ID leadID : leadMap.get( actId ) ) {
if( newLead.id == leadID ) {
newLead.Account__c = actId;
leads_to_update.add(newLead);
break;
}
}
}
I don't like this solution because it requires iterating over a Map and then over each of the lists in each of the values. It is slow.
If this isn't bulkified code, you could do a Select Query and get the Account__c value from the existing Lead by doing:
newLead.Account__c = [ SELECT Account__c FROM Lead WHERE Id = :l.id LIMIT 1];
However, this relies on your code not looping over this line and hitting a governor limit.
Or you could re-write your code soe that your Map is actually:
Map<ID, List<Leads>> leadMap = Map<ID, List<Leads>>();
Then in your query where you build the map you ensure that your Lead also includes the Account__c field.
Any of these options should work, it all depends on how this code snippet in being executed and where.
Good luck!

Dapper Results(Dapper Row) with Bracket Notation

According to the Dapper documentation, you can get a dynamic list back from dapper using below code :
var rows = connection.Query("select 1 A, 2 B union all select 3, 4");
((int)rows[0].A)
.IsEqualTo(1);
((int)rows[0].B)
.IsEqualTo(2);
((int)rows[1].A)
.IsEqualTo(3);
((int)rows[1].B)
.IsEqualTo(4);
What is however the use of dynamic if you have to know the field names and datatypes of the fields.
If I have :
var result = Db.Query("Select * from Data.Tables");
I want to be able to do the following :
Get a list of the field names and data types returned.
Iterate over it using the field names and get back data in the following ways :
result.Fields
["Id", "Description"]
result[0].values
[1, "This is the description"]
This would allow me to get
result[0].["Id"].Value
which will give results 1 and be of type e.g. Int 32
result[0].["Id"].Type --- what datattype is the value returned
result[0].["Description"]
which will give results "This is the description" and will be of type string.
I see there is a results[0].table which has a dapperrow object with an array of the fieldnames and there is also a result.values which is an object[2] with the values in it, but it can not be accessed. If I add a watch to the drilled down column name, I can get the id. The automatically created watch is :
(new System.Collections.Generic.Mscorlib_CollectionDebugView<Dapper.SqlMapper.DapperRow>(result as System.Collections.Generic.List<Dapper.SqlMapper.DapperRow>)).Items[0].table.FieldNames[0] "Id" string
So I should be able to get result[0].Items[0].table.FieldNames[0] and get "Id" back.
You can cast each row to an IDictionary<string, object>, which should provide access to the names and the values. We don't explicitly track the types currently - we simply don't have a need to. If that isn't enough, consider using the dapper method that returns an IDataReader - this will provide access to the raw data, while still allowing convenient call / parameterization syntax.
For example:
var rows = ...
foreach(IDictionary<string, object> row in rows) {
Console.WriteLine("row:");
foreach(var pair in row) {
Console.WriteLine(" {0} = {1}", pair.Key, pair.Value);
}
}

How to do searching from first character in Google App Engine?

In my pageSearch there is my value which I have to search and the country is my column name and FilterOperator is my Filteration ,If I type "A"(uppercase and lowercase) then it should give value starting with the value "A" that's what I need it.
Query query=new Query("customerRolodex").addFilter("country",FilterOperator.EQUAL,pageSearch);//.setFilter(c_r);
PreparedQuery pq=ds.prepare(query);
for(Entity result:pq.asIterable()){
//here i m using json to send and printing data;
p=new cust_rolo();
p.setCountry(result.getProperty("country").toString());
p.setRegion(result.getProperty("region").toString());
list.add(p);
}
json.put("rows", list);
out.print(json.toString());
Any help would be appreciated and also I applied Greater than or Equal to operator for this
You should use query between a range of values:
.addFilter("country", FilterOperator.GREATER_THAN_OR_EQUAL, pageSearch)
.addFilter("country", FilterOperator.LESS_THAN, pageSearch + "\uffff")

how to aggregate records in a list

I have a batch program that I need to aggregate (rollup) several currency fields by a specific contact and fund. I need a fresh set of eyes on this as I can't figure out how I need to correctly rollup the fields by the contact and fund.
Here is my current code in the batch program:
for (My_SObject__c obj : (List<My_SObject__c>)scope) {
if(!dbrToContactMap.isEmpty() && dbrToContactMap.size() > 0) {
if(dbrToContactMap.containsKey(obj.DBR__c)) {
List<Id> contactIds = dbrToContactMap.get(obj.DBR__c);
for(Id contactId : contactIds) {
My_Rollup__c rollup = new My_Rollup__c();
rollup.Fund_Name__c = obj.FundName__r.Name;
rollup.DBR__c = obj.DBR__c;
rollup.Contact__c = contactId;
rollup.YearToDate__c = obj.YearToDate__c;
rollup.PriorYear__c = obj.PriorYear__c;
rollupsToInsert.add(rollup);
}
}
}
}
if(!rollupsToInsert.isEmpty() && rollupsToInsert.size() > 0) {
insert rollupsToInsert;
}
I'm iterating over the scope of records, which is about 275,000 records that are returned. I have a map that returns a list of contacts by the key field in my scope.
Now, currently, as I loop over each contact, I put those into a list and insert. The problem is I need to aggregate (rollup) by fund and contact.
For example, let's say the map returns the contact for "John Doe" ten times for the specific key field. So, there are going to be 10 related records on "John Doe's" contact record.
The problem is that the same fund can be in those 10 records, which need to be aggregated.
So, if "Fund A" shows up 5 times and "Fund B" shows up 3 times and "Fund C" shows up 2 times, then I should only have 3 related records in total on the "John Doe" contact record that are aggregated (rolled up) by the fund.
I haven't been able to figure out how to do the rollup by fund in my list.
Can anyone help?
Any help is appreciated.
Thanks.
The key here is to use a stringified index key for your map, instead of using a raw Sobject ID. Think of this as a composite foreign key, where the combined values from a Fund and a DBR are joined. Here is the basic idea:
Map<String, My_Rollup__c> rollupMap = new Map<String, My_Rollup__c>();
for (My_SObject__c obj : (List<My_SObject__c>) scope) {
// .. stuff ..
String index = '' + new String[] {
'' + obj.FundName__r.Id,
'' + obj.DBR__c,
'' + contactId
};
// Find existing rollup, or create new rollup object...
My_Rollup__c rollup = rollupMap.get(index);
if (rollup == null) {
rollup = new My_Rollup__c();
rollupMap.put(index, rollup);
}
// Aggregate values..
}
// And save
if (rollupMap.isEmpty() == false) {
insert rollupMap.values();
}
The result is that you combining all the different "keys" that makeup a unique rollup record into a single stringified key, and then using that stringified key as the index in the map to enforce uniqueness.
The example above is incomplete, but you should be able to take it from here.

Resources