lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sidhant Aggarwal <sidhan...@me.com>
Subject Re: Query related to GeoSpatial Search
Date Sun, 18 Feb 2018 07:22:31 GMT
Hi Aakanksha,

You will need to use a boolean query to do this. In the boolean query first, add a clause
for the distance attribute using MUST clause and then add another timestamp query MUST clause.

Use this:
https://lucene.apache.org/core/6_1_0/core/org/apache/lucene/search/BooleanQuery.html

On Feb 17, 2018, at 10:50 PM, Aakanksha Gupta <aakankshagupta2000@gmail.com> wrote:

I have an assignment to find cab drivers close to a given user's
location(similar to Grab/Lyft). I have the drivers' location(latitude,
longitude) with timestamp. This data is pushed by their mobile to my server
every 2 minutes. When a user requests a ride, I need to find the nearest
driver available using the drivers' data. I'm trying to use Lucene's
GeoSpatial search for this. I've used the drivers data to be indexed and
search based on the latitude and longitude of the user. I'm also able to
search with a given latitude/longitude combination and get the nearest
driver(s) with a distance parameter. But I don't know how I can add another
parameter to the search query to specify the timestamp of the driver's data
as well. For e.g., I want to search for only drivers who are near a given
location at a particular timestamp. Can someone help me with this? Here's
the code snippet I'm using:

package com.test.trials;
import java.io.BufferedReader;import java.io.FileReader;import
java.io.IOException;import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;import
org.apache.lucene.analysis.standard.StandardAnalyzer;import
org.apache.lucene.document.Document;import
org.apache.lucene.document.StoredField;import
org.apache.lucene.index.DirectoryReader;import
org.apache.lucene.index.IndexWriter;import
org.apache.lucene.index.IndexWriterConfig;import
org.apache.lucene.search.IndexSearcher;import
org.apache.lucene.search.ScoreDoc;import
org.apache.lucene.search.TopDocs;import
org.apache.lucene.spatial3d.Geo3DPoint;import
org.apache.lucene.store.Directory;import
org.apache.lucene.store.FSDirectory;import
org.apache.lucene.document.LatLonPoint;
public class LuceneTrial {

private IndexSearcher searcher;

public static void main(String[] args) throws IOException {
LuceneTrial trial = new LuceneTrial();
trial.generateData();
trial.search();
}

private void generateData() throws IOException {
Directory dir = FSDirectory.open(Paths.get("C:/temp/Lucene"));
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
IndexWriter writer = new IndexWriter(dir, iwc);

try (BufferedReader br = new BufferedReader(new FileReader(
"C:/temp/Lucene/Drivers.csv"))) {
String line;
String[] fieldNames = new String[] { "Driver ID",
"Latitude", "Longitude", "Time" };
while ((line = br.readLine()) != null) {
// process the line.
String[] tags = line.split(",");
Document doc = new Document();
for (int i = 0; i < fieldNames.length; i++)
doc.add(new StoredField(fieldNames[i], tags[i]));

// Add a latlon point to index
try {
doc.add(new LatLonPoint("latlon", Double
.parseDouble(tags[1]),
Double.parseDouble(tags[2])));
Geo3DPoint point = new Geo3DPoint("geo3d",
Double.parseDouble(tags[1]),
Double.parseDouble(tags[2]));
doc.add(point);
} catch (Exception e) {
System.out.println("Skipped: " + line);
}
writer.addDocument(doc);
}
}
searcher = new IndexSearcher(DirectoryReader.open(writer));
}

public void search() throws IOException {
System.out
.println("\nLatLonQuery around given point, 10km
radius --------------------------------------");
TopDocs docs = searcher.search(LatLonPoint.newDistanceQuery("latlon",
6.9270790, 79.8612430, 10 * 1000), 20);
for (ScoreDoc scoreDoc : docs.scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc);
}
}
}


And here's the sample data I'm using:

Driver ID Latitude Longitude Time1 -6.081689 145.391881
7:01:172 -5.207083 145.7887 8:32:403 -5.826789 144.295861
8:40:494 -6.569828 146.726242 8:57:335 -9.443383 147.22005
6:14:266 -3.583828 143.669186 8:13:357 61.160517 -45.425978
8:58:248 64.190922 -51.678064 7:42:169 67.016969 -50.689325
6:52:2010 76.531203 -68.703161 6:08:2111 65.659994 -18.072703
7:57:4512 65.283333 -14.401389 7:32:2313 64.295556 -15.227222
8:20:2614 65.952328 -17.425978 8:51:3415 66.058056 -23.135278
8:33:4316 63.985 -22.605556 7:39:3517 65.555833 -23.965
7:20:5418 64.13 -21.940556 7:37:4819 66.133333 -18.916667
6:46:3620 63.424303 -20.278875 7:15:1221 46.485001 -84.509445
6:14:1522 50.056389 -97.0325 7:12:1523 44.639721 -63.499444
6:15:3124 51.391944 -56.083056 7:15:5025 49.082222 -125.7725
6:52:22

Can someone show me how I can search for the drivers based on two
attributes - distance and time?

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
    • Unnamed multipart/related (inline, None, 0 bytes)
View raw message