I am using Solr 6.4.2. I have defined 2 cores:
companies, with the fields 'Id, Town, Name, Type, ManagerId'
users, with the fields 'Id, Login, ManagerId, Email'
In users core the field ManagerId is parent-child relation (ManagerId->Id).
Companies and users are related by companies.ManagerId->users.Id
I am trying to build a very simple join query:
{!join from=ManagerId to=Id fromIndex=users}Login:someuser1
url looks like:
?q=*:*&fq={!join%20from=ManagerId%20to=Id%20fromIndex=users}Login:someuser1
nothing works, I always get zero results. I just want to understand how Solr join works. It seems to me that there is a big difference in understanding between Solr joins and SQL joins.
In fact I want to do the queries like:
get all docs from users by company Types
get companies by user managers
Right now I always get zero results no matter how I write the join query.
Try this on the company core (assuming you want the get all companies run by a certain manager login):
login:someuser1 is the filter condition you put on the child table, this should be the manager login you are looking for
from=ManagerId should be the id on the child table, so this is wrong
to=id is the field on the parent table that relates to the child table, so this is wrong
fromIndex is the child table, this is correct
{!join from=ManagerId to=Id fromIndex=users}Login:someuser1
Related
Is it possible to write a query to access parent-child and child-parent objects, in one SOQL query?
I have a scenario where, I need to access Account Objects from child and child of Account too, in the same query.
Example:
Select Id,
(Select Id,Name, (Select Id, Address from Addresses__r) from x__r.Account),
x__r.Account.Name
From x
.
(Pardon me, if I use any wrong terms. I am pretty new to Salesforce)
Yes, it is possible to do so.
In the example below, we are using sub-query to retrieve Ids of all Account Territories (parent-to-child relationship) and, at the same time, we are using child-to-parent relationship to retrieve TimeZoneSidKey of the User who has created the record.
SELECT Id, (SELECT Id FROM Accounts_Territories__r), CreatedBy.TimeZoneSidKey FROM Account
Documentation on the topic
I would like to "convert" a SQL query in a solr command.
I have 2 SQL tables "jobs" and "companies".
Today to count the number of jobs published by a company, I run this query :
select c.id, count(j.id)
from companies c
left join jobs j on j.client_id = c.id
group by c.id;
In other hand I have 2 collections "jobs" and "companies" there is the same fields.
How can I "convert" the query below in solr ?
I saw it's possible to make a join with a parent collection, but I don't want to create a hierarchy between job and company (it doesn't make sense).
You can't implement a direct version of the query, as Solr doesn't do joins the way you want to do joins. A possible solution would be to facet on client_id in jobs, so you get a count for each company id, then look up values from that result when you display the counts for each company (if you really need the left join part). If the client_id isn't present in the data, its count is 0. Something like facet=true&facet.field=client_id should work.
If you only need counts for companies present in the index, I'd index the company name together with the id, so that you can facet on the name directly.
I have the following 2 questions:
1) I was going through the Join query parser in Solr here https://wiki.apache.org/solr/Join. From the above example, what I understand is that it is not possible to join between 2 separate schemas in Solr. The only join that is feasible is a self-join. Did I understand it correctly?
2) I was trying to find a way to create auto-generated id's in Solr.
I came across this link https://wiki.apache.org/solr/UniqueKey, what I understand from this link is that there is a way to create the unique id in Solr, but what if I have 2 separate fields in my schema that I want to be auto-generated? Is there a way to achieve that?
You can join across different cores the syntax is something like:
{!join from=fromId to=toId fromIndex=Core2}query
So if you have two core that looks something like
PersonCore - ID, Name
AddressCore - ID, Address, PersonID
You can find all people at a specific address by querying the PersonCore like this:
{!join from=PersonID to=ID fromIndex=AddressCore}Address:Address1
You can only have one UniqueKey. It might be possible to auto generate another unique field value though, have a look here:
http://solr.pl/en/2013/07/08/automatically-generate-document-identifiers-solr-4-x/
I've never used this because I always used a unique key from the data being indexed but it might be worth looking into? If you were to add another field name into the updateRequestProcessorChain section descibed in solrconfig.xml would that generate another unique ID? I'm not sure but it's something to try
I have the following three custom objects:
Order__c
Design__c (has a lookup to Order and a lookup to Location, so the design ojbect is the junction object)
Location__c
On the order page layout I want to add a blank section that contains a VF page in order to display the Location records for all the design records for an order.
An order can have many designs and a design can have many locations. I need a way to group and display each design/locations in the VF page on the Order.
How can I build this query and display the results on the VF page?
I was trying a query like this: Select Location_r.Name, Location_r.Mockup From Design_c where Order_c = 'xxxxxxxxxxxxxx'
Also, is there a better way to display the results besides a VF page section in a related list?
Thanks for any help!
Regards.
First things, first. since design is related to the other 2 objects via lookup, it is not a junction object. junction objects are only when it's 2 parents have a master-detail relationship with the child.
I think what you're trying to achieve is to traverse a parent-child-parent relationship. You'll need to use subqueries for the last half. From the documentation- here you go: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm
Query **child-to-parent relationships**, which are often many-to-one. Specify these relationships directly in the SELECT, FROM, or WHERE clauses using the dot (.) operator.
For example:
SELECT Id, Name, Account.Name
FROM Contact
WHERE Account.Industry = 'media'
This query returns the ID and name for only the contacts whose related account industry is media, and for each contact returned, the account name.
Query **parent-to-child**, which are almost always one-to-many. Specify these relationships using a subquery (enclosed in parentheses), where the initial member of the FROM clause in the subquery is related to the initial member of the outer query FROM clause. Note that for subqueries, you should specify the plural name of the object as that is the name of the relationship for each object.
For example:
SELECT Name,
(
SELECT LastName
FROM Contacts
)
FROM Account
The query returns the name for all the accounts, and for each account, the last name of each contact.
I'm trying to write a SOQL query that pulls back User information along with the DefaultDivision's name. When executing the following:
Select u.Email, u.FirstName, u.LastName, u.DefaultDivision from User u
the value for 'DefaultDivision' is an Id (like 02d3498202020222) instead of the friendly name (like North America). I know that the DefaultDivision maps to the Division object's Id, but how do I get the name?
I have tried things like:
select u.Email, (select Name from Division where Id = u.DefaultDivision) from User u
but that doesn't work because there is no direct relationship (and yet, there is...!).
Unlike SQL, SOQL does not support subqueries of the type you have given there.
In most cases with SOQL where there is an explicit relationship defined, you can query data from that explicit relationship directly, like
select u.Email, u.DefaultDivision.Name from User u where Id=blahblahblah
However there are some cases where such a relationship is not explicitly defined, and I believe DefaultDivision is one of those (so in fact my query above probably will not work). When this is the case, there's no single-query way to get the information you want -- you'll have to first query on divisions and their IDs, then query users with their DefaultDivision, and then resolve the division names from the IDs using the results of the first query.
Fortunately this type of situation doesn't happen very often anymore, but in using Divisions you're plumbing the depths of Salesforce.com a bit so you may find some unusual stuff there.