You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Sujit Pal <su...@comcast.net> on 2011/10/03 23:35:22 UTC

Re: Sort five random "Top Offers" to the top

Hi Mouli,

I was looking at the code here, not sure why you even need to do the
sort...

After you get the DocList, couldn't you do something like this?

List<Integer> topofferDocIds = new ArrayList<Integer>();
for (DocIterator it = ergebnis.iterator(); it.hasNext();) {
  topofferDocIds.add(it.next());
}
Collections.shuffle(topofferDocIds);
rb.req.getContext().set(TOPOFFERS, topofferDocIds);

so in first-component, you have identified the top 5 offers for the
query and client, and stuffed them into the context.

Then you define a last component which will take the topofferDocIds and
place them at the top of the search results, and remove them if they
exist from the main result.

Would that not work?

Alternatively (kind of a hybrid way) would be to define your own
(single) component that takes the query, sends back two queries to the
underlying solr, one with the topoffers and one without and merges the
results before sending back. This would replace the component that does
the search.

-sujit

On Wed, 2011-09-28 at 07:15 -0700, MOuli wrote:
> Hey Community.
> 
> I write my first component and now i got a problem hear is my code: 
> 
> @Override
> public void prepare(ResponseBuilder rb) throws IOException {
>     try {
>             rb.req.getParams().getBool("topoffers.show", true);
>             String client = rb.req.getParams().get("client", "1");
>             BooleanQuery[] queries = new BooleanQuery[2];
>             queries[0] = (BooleanQuery) DisMaxQParser.getParser(
>                 rb.req.getParams().get("q"),
>                 DisMaxQParserPlugin.NAME,
>                 rb.req)
>             .getQuery();
>             queries[1] = new BooleanQuery();
>             Occur occur = BooleanClause.Occur.MUST;
>             queries[1].add(QueryParsing.parseQuery("ups_topoffer_" + client
> + ":true", rb.req.getSearcher().getSchema()), occur);
> 
>             Query q = Query.mergeBooleanQueries(queries[0], queries[1]);
> 
>             DocList ergebnis = rb.req.getSearcher().getDocList(q, null,
> null, 0, 5, 0);
> 
>             String[] machineIds = new String[5];
>             int position = 0;
>             DocIterator iter = ergebnis.iterator();
>             while (iter.hasNext()) {
>                 int docID = iter.nextDoc();
>                 Document doc =
> rb.req.getSearcher().getReader().document(docID);
>                 for (String value : doc.getValues("machine_id")) {
>                     machineIds[position++] = value;
>                 }
>             }
> 
>             Sort sort = rb.getSortSpec().getSort();
>             if (sort == null) {
>                 rb.getSortSpec().setSort(new Sort());
>                 sort = rb.getSortSpec().getSort();
>             }
> 
>             SortField[] newSortings = new SortField[sort.getSort().length +
> 5];
>             int count = 0;
>             for (String machineId : machineIds) {
>                 SortField sortMachineId = new SortField("map(machine_id," +
> machineId + "," + machineId + ",1,0) desc", SortField.DOUBLE);
>                 newSortings[count++] = sortMachineId;
>             }
> 
>             SortField[] sortings = sort.getSort();
>             for (SortField sorting : sortings) {
>                 newSortings[count++] = sorting;
>             }
> 
>             sort.setSort(newSortings);
> 
>             rb.getSortSpec().setSort(sort);
> 
>         } catch (ParseException e) {
>             LoggerFactory.getLogger(Topoffers.class).error( "Fehler bei den
> Topoffers!", this);
>             LoggerFactory.getLogger(Topoffers.class).error(e.toString(),
> this);
>         }
> 
> }
> 
> Why can't i manipulate the sort? Is there something i miss understand?
> 
> This search component is added as a "first-component" in the solrconfig.xml.
> 
> Please can anyone help me??
> 
> 
> --
> View this message in context: http://lucene.472066.n3.nabble.com/Sort-five-random-Top-Offers-to-the-top-tp3355469p3376166.html
> Sent from the Solr - User mailing list archive at Nabble.com.