Richard Parnaby-King
Richard Parnaby-King

Reputation: 14862

Zend Lucene search related fields?

I have a lot of paired fields (hoursDistance1, cityName1, hoursDistance2, cityName2, hoursDistance3, cityName3, etc.).

What query do I need to search for so that Lucene scores based on both fields having the correct terms instead of just one of them? i.e. if I search for a city 3 hours from here with this name, how do I get it to return results where hoursDistanceN is 3 hours from here AND cityNameN is this without scoring the other pairs of fields?

Upvotes: 0

Views: 422

Answers (2)

Xodarap
Xodarap

Reputation: 11849

You could create a document for each pair. So instead of

id | hours1 | name1 | hours2 | name2 | ...

You would have:

id | pair_num = 1 | hours | name
id | pair_num = 2 | hours | name
...

Since you only want to search one pair at a time, you shouldn't need to merge the results together or anything.

Upvotes: 2

Yuval F
Yuval F

Reputation: 20621

IIUC, you can do this by denormalizing your data: Create a Lucene document for each pair of fields, e.g. if:

hoursDistance1=3,cityName1=London

Create a document with the fields:

hoursDistance=3,cityName=London,pairIndex=1

And then run a query like:

hoursDistance=5 AND cityName=Leeds

Upvotes: 0

Related Questions