Sorting by field from a given 'offset'

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Sorting by field from a given 'offset'

Stephane Bailliez
Hi all,


I'm trying to do the following:

"starting from document that has field f1 = a, give me the first n
documents sorted by field 'z' ordered by asc/desc"

Is it possible to build a complex query more efficient than:

1) search for the document where f1 = a
2) get the field f2 from this document
3) give me the nth sorted documents (in either asc or desc order)


In sql that would be something like:

select LIMIT 0 count * from table where f2 < (SELECT f2 FROM table WHERE
f1 = 'a') order by f2 desc


Thanks

Stephane


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Sorting by field from a given 'offset'

Stephane Bailliez
Stephane Bailliez wrote:

> Hi all,
>
> "starting from document that has field f1 = a, give me the first n
> documents sorted by field 'z' ordered by asc/desc"
To be more specific, that's something like:

[...]

// get the document field 'dt' from a search
...

// search must occur starting from the dt we just found
Term upperTerm = new Term(FIELD_DT, descending ? MIN_DT:MAX_DT);
Term lowerTerm = new Term(FIELD_DT, dt);
RangeQuery rangeQuery = new RangeQuery(lowerTerm, upperTerm, false);

// we will search in the content
Query contentQuery = QueryParser.parse(queryString, FIELD_DATACONTENT,
analyzer);

// a document must have both query required
BooleanQuery query = new BooleanQuery();
query.add(rangeQuery, true, false);
query.add(contentQuery, true, false);

// sort according to dt
Sort sort = new Sort( new SortField(FIELD_DT, SortField.STRING,
descending) );

// return only a given number of docs
TopFieldDocs topFieldDocs = searcher.search(query, null, maxlength, sort);
...


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]