0

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?

Charles
  • 50,943
  • 13
  • 104
  • 142
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129

2 Answers2

2

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.

Xodarap
  • 11,581
  • 11
  • 56
  • 94
0

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
Yuval F
  • 20,565
  • 5
  • 44
  • 69
  • Creating documents for each set of pairs is not possible as they are part of a location document, with each pair of fields relative to the location. – Richard Parnaby-King Nov 16 '10 at 14:32
  • I believe you need to explain this a little further. How did you create the fields? Maybe using a spatial search module is better. – Yuval F Nov 16 '10 at 18:14