Reputation: 163
Hello i've read the Solr wiki and searched here but didn't find a solution for my use-case:
We're indexing customer-data with different kind of contracts into a single document. So each customer will result in a Solr document witth one or more different contracts.
The fields for each contract are added dynamically via import (e.g. contract_type_1_s, contract_type_2_s, ...; contract_change_date_1_dt, contract_change_date_2_dt, ...). So all fields with '2' are related to contract no 2. With this the user is able to search for customers who have a contract of type one and none of type two and so on.
My use case is now to return only the fields of the contract which matched the query.
Here's an example:
<doc>
<field name="id">100</field>
<field name="customer_name">paul</field>
<field name="contract_type_1_s">inhouse</field>
<field name="contract_change_date_1_dt">2012-09-01T00:00:00Z</field>
</doc>
<doc>
<field name="id">101</field>
<field name="customer_name">fred</field>
<field name="contract_type_1_s">inhouse</field>
<field name="contract_change_date_1_dt">2012-09-01T00:00:00Z</field>
<field name="contract_type_2_s">external</field>
<field name="contract_change_date_2_dt">2012-09-01T00:00:00Z</field>
</doc>
<doc>
<field name="id">102</field>
<field name="customer_name">karl</field>
<field name="contract_type_1_s">external</field>
<field name="contract_change_date_1_dt">2012-09-01T00:00:00Z</field>
<field name="contract_type_2_s">inhouse</field>
<field name="contract_change_date_2_dt">2012-09-01T00:00:00Z</field>
</doc>
If the user now searches for customers with contract-type 'external' the documents with ids 101 and 102 are in the result. Now i want to return different fields of the contract which matched the query.
In this example these should be contract_change_date_1_dt for document 102 and contract_change_date_2_dt for document 101, since contract no 1 is external in document 102 and contract no 2 is external in document 101.
Is there a way to achive this behavior with build-in components?
I know that i can find out which fields matched the query with the highlight-component.
I endet up with following resolution, but it forces me to extend Solr:
I Hope i made my problem clear. Any suggestions which is a good way to archive this are really appreciated.
greetings René
Upvotes: 2
Views: 790
Reputation: 163
I now managed the whole thing.
I must not change the response writer at all :-)
I just had to store all fields which i resolved for each document and add them to the response:
rb.rsp.setReturnFields(globalResultFields);
greetings René
Upvotes: 1
Reputation: 163
if someone needs a similar thing ;-)
I now managed to build up my custom result list in the following way:
Create a Custom QueryComponent (extending standard QueryComponent) to store the fields which are used in the query. In the prepare-method activate highlighting with the stored fields:
// Making params modifieable
ModifiableSolrParams modifiableParams = new ModifiableSolrParams(params);
req.setParams(modifiableParams);
modifiableParams.set(HighlightParams.FIELDS, queryFieldList);
modifiableParams.set(HighlightParams.HIGHLIGHT, "true");
modifiableParams.set(HighlightParams.FIELD_MATCH, "true");
modifiableParams.set(HighlightParams.SIMPLE_PRE, "");
modifiableParams.set(HighlightParams.SIMPLE_POST, "");
Create a Custom HighlightComponent (extending standard HighlightComponent) to build the result out of the std. result. In the process-method i now get the highlight info and extract the information i need:
NamedList<Object> rspValues = rb.rsp.getValues();
NamedList<Object> nlHl = (NamedList<Object>) rspValues.get("highlighting");
this.hlDocsAndFields = extractHighlightingInfo(nlHl);
For that i created a custom List, which is able to count the matches per contract (how much fields of contract_X_s are in the highlighted results).
This works fine.
I now stuck at the response writer who resolves the document-fields himself when he builds the response :-(
Has annyone a suggestion on changig/customizing the response writer?
greetings René
Upvotes: 1