Revision 32088 branches/v2_0_0_prep/libraries/libGeocoding/src/org/gvsig/geocoding/impl/LuceneGeocoderImpl.java

View differences:

LuceneGeocoderImpl.java
60 60
import org.gvsig.geocoding.address.ComposedAddress;
61 61
import org.gvsig.geocoding.address.Literal;
62 62
import org.gvsig.geocoding.address.NumberAddress;
63
import org.gvsig.geocoding.index.FeatureIndexedEvent;
64
import org.gvsig.geocoding.index.IndexListener;
63 65
import org.gvsig.geocoding.pattern.GeocodingPattern;
64 66
import org.gvsig.geocoding.result.GeocodingResult;
65 67
import org.gvsig.geocoding.result.ScoredFeature;
......
85 87
	private Logger log = LoggerFactory.getLogger(LuceneGeocoderImpl.class);
86 88
	private GeocodingPattern pattern = null;
87 89
	private DataStore store = null;
90
	@SuppressWarnings("unused")
88 91
	private DataManager manager = null;
92
	private int position = 0;
93
	
94

  
95
	private IndexListener indexListener;
96
	
89 97
	/*
90 98
	 * This path is a temporal folder. This code is a copy of the
91 99
	 * libRemoteServices and LibRaster
......
153 161
	}
154 162

  
155 163
	/**
156
	 * Geocoding process
157 164
	 * 
158
	 * @param address
159
	 * @return results
160 165
	 */
161 166
	public Set<GeocodingResult> geocode(Address address)
162 167
			throws LocatorException, DataException {
......
231 236
	}
232 237

  
233 238
	/**
234
	 * First scouting process, search literal elements in data store and return
235
	 * a list with ok scored features
236 239
	 * 
237 240
	 * @param relationsLiteral
238 241
	 * @param addressLiteral
......
247 250

  
248 251
		double minScore = getPattern().getSettings().getScore();
249 252

  
250
		// Create index writer
251
		IndexWriter indexWriter = null;
252
		try {
253
			indexWriter = createIndex(store, relationsLiteral);
254
		} catch (Exception e) {
255
			log.error("Error creating index", e);
256
		}
257
		// if index writer is not null run search process
258
		if (indexWriter != null) {
259
			// search
260
			Hits hits = null;
253
		// search
254
		Hits hits =  seachOnIndex(relationsLiteral, addressLiteral);
255
	
256
		// if there are hits in search process
257
		if (hits != null) {
258
			// filter results
261 259
			try {
262
				hits = seachOnIndex(relationsLiteral, addressLiteral);
260
				scorefeats = filterHitsByScore(store, hits, minScore);
263 261
			} catch (Exception e) {
264
				log.error("Error searching on index", e);
262
				log.error("Error filtering hits by score", e);
263
				scorefeats.clear();
265 264
			}
266
			// if there are hits on search process
267
			if (hits != null || hits.length() > 0) {
268
				// filter
269
				try {
270
					scorefeats = filterHitsByScore(store, hits, minScore);
271
				} catch (Exception e) {
272
					log.error("Error filtering hits by score", e);
273
				}
274

  
275
			}
276 265
		}
277 266

  
278 267
		return scorefeats;
......
283 272
	 * @param relationsLiteral
284 273
	 * @param addressLiteral
285 274
	 * @return
286
	 * @throws CorruptIndexException
287
	 * @throws IOException
288
	 * @throws ParseException
289 275
	 */
290 276
	private Hits seachOnIndex(Literal relationsLiteral, Literal addressLiteral)
291
			throws CorruptIndexException, IOException, ParseException {
277
			 {
292 278

  
293 279
		log.info("Buscar ...");
294 280

  
295
		IndexSearcher searcher = new IndexSearcher(tempDirectoryPath);
281
		IndexSearcher searcher = null;
282
		try {
283
			searcher = new IndexSearcher(tempDirectoryPath);
284
		} catch (Exception e) {
285
			log.debug("Error building index searcher", e);
286
			log.debug("Rebuild the index file because the initial index is wrong");
287
			try {
288
				this.indexer(store, relationsLiteral);
289
			} catch (Exception e1) {
290
				log.debug("Error building index searcher", e1);
291
				return null;
292
			}
293
		}
294
		if (searcher != null) {
295
			String[] input = addressLiteral.getStringValuesArray();
296
			String[] fields = relationsLiteral.getStringValuesArray();
296 297

  
297
		String[] input = addressLiteral.getStringValuesArray();
298
		String[] fields = relationsLiteral.getStringValuesArray();
299

  
300
		Query query = MultiFieldQueryParser.parse(input, fields,
301
				new StandardAnalyzer());
302
		Hits hits = searcher.search(query);
303

  
304
		return hits;
298
			Query query = null;
299
			try {
300
				query = MultiFieldQueryParser.parse(input, fields,
301
						new StandardAnalyzer());
302
			} catch (ParseException e) {
303
				log.debug("Error building the query", e);
304
				return null;
305
			}
306
			Hits hits = null;
307
			if(query != null){
308
				try {
309
					hits = searcher.search(query);
310
				} catch (IOException e) {
311
					log.debug("Error in the searching process", e);
312
					return null;
313
				}
314
			}
315
			return hits;
316
		} else {
317
			return null;
318
		}
305 319
	}
306 320

  
307 321
	/**
308 322
	 * 
323
	 * @param store
309 324
	 * @param hits
325
	 * @param score
310 326
	 * @return
311 327
	 * @throws IOException
312 328
	 * @throws DataException
......
317 333
		FeatureStore fstore = (FeatureStore) store;
318 334
		List<ScoredFeature> scoreFeats = new ArrayList<ScoredFeature>();
319 335
		for (int i = 0; i < hits.length(); i++) {
320
			if (hits.score(i) > score) {
336
			if (hits.score(i) * 100 > score) {
321 337
				Document doc = hits.doc(i);
322 338
				String id = doc.get(GeocoID);
323 339
				FeatureSet fset = fstore.getFeatureSet();
......
336 352
	 * 
337 353
	 * @param store
338 354
	 * @param relationsLiteral
339
	 * @return
340 355
	 * @throws DataException
341 356
	 * @throws CorruptIndexException
342 357
	 * @throws LockObtainFailedException
343 358
	 * @throws IOException
344 359
	 */
345
	private IndexWriter createIndex(DataStore store, Literal relationsLiteral)
360
	public void indexer(DataStore store, Literal relationsLiteral)
346 361
			throws DataException, CorruptIndexException,
347 362
			LockObtainFailedException, IOException {
348 363

  
349
		log.info("Create indexes ...");
364
		log.debug("Create indexes ...");
350 365

  
351 366
		IndexWriter indexWriter = new IndexWriter(tempDirectoryPath,
352 367
				new StandardAnalyzer(), true);
......
358 373
		Feature feat = null;
359 374

  
360 375
		Iterator it = features.fastIterator();
376
		// process situation
377
		this.position = 0;
361 378
		while (it.hasNext()) {
362 379

  
363 380
			Document doc = new org.apache.lucene.document.Document();
......
377 394
				doc.add(field);
378 395
			}
379 396
			indexWriter.addDocument(doc);
397
			// throw indexed event
398
			if(this.indexListener != null){
399
				this.indexListener.featureIndexed(new FeatureIndexedEvent(this));
400
			}			
401
			// new position
402
			this.position++;
380 403
		}
381 404
		indexWriter.optimize();
382

  
383
		return indexWriter;
384 405
	}
406
	
407
	
408
	
409
	/**
410
	 * 
411
	 * @param listener
412
	 */
413
	public void registreIndexListener(IndexListener listener){
414
		this.indexListener = listener;
415
	}
416
	
417
	/**
418
	 * 
419
	 * @return
420
	 */
421
	public int getIndexPosition() {
422
		return position;
423
	}
385 424

  
386 425
}

Also available in: Unified diff