On 10/13/2013 8:34 AM, Michael Tyler wrote:
> Hello,
> 
>     I have 2 different solr indexes returning 2 different sets of
> SolrDocumentList. Doc Id is the foreign key relation.
> 
> After obtaining them, I want to perform "AND" operation between them and
> then return results to user. Can you tell me how do I get this? I am using
> solr 4.3
> 
>  SolrDocumentList results1 = responseA.getResults();
>  SolrDocumentList results2 = responseB.getResults();
> 
> results1  : d1, d2, d3
> results2  :  d1,d2, d4

The SolrDocumentList class extends ArrayList<SolrDocument>, which means
that it inherits all ArrayList functionality.  Unfortunately, there's no
built-in way of eliminating duplicates with a java List.  It's very easy
to combine the two results into another object, but that object will
contain both of the d1 and both of the d2 SolrDocument objects.

The following code is a reasonably fast way to handle this.  It assumes
that results1 is the list that should win when there are duplicates, so
it gets added first.  It assumes that the uniqueKey field is named "id"
and that it contains a String value.  If these are incorrect
assumptions, you can adjust the code accordingly.

SolrDocumentList results1 = responseA.getResults();
SolrDocumentList results2 = responseB.getResults();
List<SolrDocumentList> tmpList = new ArrayList<SolrDocumentList>();
tmpList.add(results1);
tmpList.add(results2);

Set<String> tmpSet = new HashSet<String>();
SolrDocumentList newList = new SolrDocumentList();
for (SolrDocumentList l : tmpList)
{
        for (SolrDocument d : l)
        {
                String id = (String) d.get("id");
                if (tmpSet.contains(id)) {
                        continue;
                }
                tmpSet.add(id);
                newList.add(d);
        }
}

Thanks,
Shawn

Reply via email to