List of usage examples for org.apache.lucene.search IndexSearcher doc
public Document doc(int docID) throws IOException
.getIndexReader().document(docID)
From source file:com.berico.clavin.resolver.impl.lucene.LuceneUtils.java
License:Apache License
/** * Convert a set of Lucene Document Results into a list of * ResolvedCoordinates.//from w ww . ja v a 2 s . co m * @param occurrence the CoordinateOccurrence in the document. * @param searcher the Lucene Searcher that retrieved the results. * @param results the Search results context * @param components LuceneComponents (specifically, the spatial components * needed to calculate vectors). * @return a List of ResolvedCoordinates. * @throws Exception */ public static List<ResolvedCoordinate> convertToCoordinate(CoordinateOccurrence<?> occurrence, IndexSearcher searcher, TopDocs results, LuceneComponents components) throws Exception { // Results ArrayList<ResolvedCoordinate> resolvedCoordinates = new ArrayList<ResolvedCoordinate>(); // Grab the Lucene spatial context SpatialContext spatialContext = components.getSpatialContext(); // If we have results if (results.scoreDocs.length > 0) { // Get the center coordinate of the location occurrence LatLon center = occurrence.convertToLatLon(); // Convert to a Spatial4j point Point occurrencePoint = spatialContext.makePoint(center.getLongitude(), center.getLatitude()); // Iterate over the results for (int i = 0; i < results.scoreDocs.length; i++) { // Grab the document from Lucene Document doc = searcher.doc(results.scoreDocs[i].doc); // Get the Place record Place record = dehydrate(doc); // Get the centroid of the Place String positionOfLocation = doc.get(FieldConstants.GEOMETRY); // TODO: Spatial4J supposedly has a ShapeReaderWriter implementation // that we should be using instead. @SuppressWarnings("deprecation") Point point = (Point) spatialContext.readShape(positionOfLocation); // Calculate the distance double distanceInDegrees = spatialContext.getDistCalc().distance(point, occurrencePoint); double distanceInKm = DistanceUtils.degrees2Dist(distanceInDegrees, DistanceUtils.EARTH_MEAN_RADIUS_KM); // Calculate the direction double direction = calculateDirection(point.getX(), point.getY(), occurrencePoint.getX(), occurrencePoint.getY()); // Add the ResolvedCoordinate to the list. resolvedCoordinates .add(new ResolvedCoordinate(occurrence, record, new Vector(distanceInKm, direction))); } } return resolvedCoordinates; }
From source file:com.berico.clavin.resolver.impl.lucene.LuceneUtils.java
License:Apache License
/** * Convert a set of Lucene Document Results into a list of ResolvedLocations. * @param occurrence LocationOccurrence in the document. * @param searcher the Lucene Searcher used to find the locations. * @param results the results of the Lucene Search * @param usingFuzzy whether fuzzy matching was used * @return List of ResolvedLocations//from w ww .j a v a 2 s. c o m * @throws IOException */ public static List<ResolvedLocation> convertToLocations(LocationOccurrence occurrence, IndexSearcher searcher, TopDocs results, boolean usingFuzzy) throws IOException { ArrayList<ResolvedLocation> locations = new ArrayList<ResolvedLocation>(); if (results.scoreDocs.length > 0) { for (int i = 0; i < results.scoreDocs.length; i++) { Document doc = searcher.doc(results.scoreDocs[i].doc); ResolvedLocation location = convertToLocation(doc, occurrence, usingFuzzy); locations.add(location); } } return locations; }
From source file:com.bewsia.script.LuceneHandler.java
License:Open Source License
public void load(String id, SEntity src) { try {/*from ww w .ja va2s .com*/ IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex))); IndexSearcher searcher = new IndexSearcher(reader); TopDocs td = searcher.search(new TermQuery(new Term(SEntity.ID, id)), 1); if (td.totalHits > 0) { Document doc = searcher.doc(td.scoreDocs[0].doc); if (allowLoad(id, doc.get(SEntity.KIND))) { src.setSchema(doc.get(SEntity.SCHEMA)); read(src, doc); } } searcher.close(); reader.close(); } catch (Exception e) { } }
From source file:com.bewsia.script.LuceneHandler.java
License:Open Source License
public List<SEntity> search(String kind, Query query, Filter filter, Sort sort, int max) { List<SEntity> tag = new ArrayList<SEntity>(); try {//from w w w. java 2 s .c om IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex))); IndexSearcher searcher = new IndexSearcher(reader); BooleanQuery boolQuery = new BooleanQuery(); boolQuery.add(new BooleanClause(new TermQuery(new Term(SEntity.KIND, kind)), Occur.MUST)); if (query != null) { boolQuery.add(new BooleanClause(query, Occur.MUST)); } TopDocs td = null; if (filter != null && sort != null) { td = searcher.search(boolQuery, filter, max, sort); } else if (filter != null) { td = searcher.search(boolQuery, filter, max); } else if (sort != null) { td = searcher.search(boolQuery, max, sort); } else { td = searcher.search(boolQuery, max); } for (int i = 0; i < td.totalHits; i++) { SEntity item = new SEntity(this); Document doc = searcher.doc(td.scoreDocs[i].doc); item.setSchema(doc.get(SEntity.SCHEMA)); read(item, doc); tag.add(item); } searcher.close(); reader.close(); } catch (Exception e) { } return tag; }
From source file:com.bewsia.script.LuceneHandler.java
License:Open Source License
public List<SEntity> search(String kind, Query query, Filter filter, Sort sort, int pagesize, int pageno) { List<SEntity> tag = new ArrayList<SEntity>(); try {/* ww w .j a va 2 s . co m*/ IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex))); IndexSearcher searcher = new IndexSearcher(reader); BooleanQuery boolQuery = new BooleanQuery(); boolQuery.add(new BooleanClause(new TermQuery(new Term(SEntity.KIND, kind)), Occur.MUST)); if (query != null) { boolQuery.add(new BooleanClause(query, Occur.MUST)); } if (pagesize <= 0) pagesize = 10; if (pageno <= 0) pageno = 1; int max = pageno * pagesize; TopDocs td = null; if (filter != null && sort != null) { td = searcher.search(boolQuery, filter, max, sort); } else if (filter != null) { td = searcher.search(boolQuery, filter, max); } else if (sort != null) { td = searcher.search(boolQuery, max, sort); } else { td = searcher.search(boolQuery, max); } for (int i = (pageno - 1) * pagesize; i < td.totalHits && i < max; i++) { SEntity item = new SEntity(this); Document doc = searcher.doc(td.scoreDocs[i].doc); item.setSchema(doc.get(SEntity.SCHEMA)); read(item, doc); tag.add(item); } searcher.close(); reader.close(); } catch (Exception e) { } return tag; }
From source file:com.bewsia.script.LuceneHandler.java
License:Open Source License
protected void deleteEntity(String id) { if (id.length() == 0) return;// w ww . j a v a 2 s . c om String kind = ""; try { IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex))); IndexSearcher searcher = new IndexSearcher(reader); TopDocs td = searcher.search(new TermQuery(new Term(SEntity.ID, id)), 1); if (td.totalHits > 0) { Document doc = searcher.doc(td.scoreDocs[0].doc); kind = doc.get(SEntity.KIND); } searcher.close(); reader.close(); } catch (Exception e) { } if (kind.length() == 0) return; if (!allowDelete(id, kind)) return; try { if (!kind.equals(KIND_QUOTA)) { if (!quotaDelete(id, kind)) return; } removeBackup(id, kind); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer); iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); IndexWriter writer = new IndexWriter(FSDirectory.open(new File(dirIndex)), iwc); writer.deleteDocuments(new Term(SEntity.ID, id)); writer.close(); } catch (Exception e) { } }
From source file:com.bitranger.parknshop.common.fulltext.SearchCustomer.java
License:Open Source License
public List<PsCustomer> search(String value) throws IOException { // get the index IndexReader reader = DirectoryReader.open(FSDirectory.open(BuildIndexForItem.getIndexFile())); // use this to search IndexSearcher indexSearcher = new IndexSearcher(reader); // use the queryParser to wrap your request QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_45, queryString, analyzer); List<PsCustomer> customer = new ArrayList<PsCustomer>(); Query query = null;//w w w .j a va2 s.c o m try { query = queryParser.parse("title:" + value + "~"); } catch (ParseException e) { e.printStackTrace(); } // we get what we want in the topdocs TopDocs topDocs = indexSearcher.search(query, 25); System.out.println(":" + topDocs.totalHits + ""); // ScoreDoc[] scoreDoc = topDocs.scoreDocs; for (int i = 0; i < topDocs.scoreDocs.length; i++) { Document resultDocument = indexSearcher.doc(topDocs.scoreDocs[i].doc); PsCustomer mycustomer = new PsCustomer(); mycustomer.setNickname(resultDocument.get((indexField[0]))); mycustomer.setEmail(resultDocument.get((indexField[1]))); mycustomer.setPassword(resultDocument.get((indexField[2]))); mycustomer.setGender(Short.valueOf(resultDocument.get((indexField[3])))); mycustomer.setName(resultDocument.get((indexField[4]))); customer.add(mycustomer); } return customer; }
From source file:com.bitranger.parknshop.common.fulltext.SearchItem.java
License:Open Source License
public List<PsItem> search(String value) throws IOException { // get the index IndexReader reader = DirectoryReader.open(FSDirectory.open(BuildIndexForItem.getIndexFile())); // use this to search IndexSearcher indexSearcher = new IndexSearcher(reader); // use the queryParser to wrap your request QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_45, queryString, analyzer); List<PsItem> item = new ArrayList<PsItem>(); Query query = null;// www.jav a 2 s . com try { query = queryParser.parse("title:" + value + "~"); } catch (ParseException e) { e.printStackTrace(); } // we get what we want in the topdocs TopDocs topDocs = indexSearcher.search(query, 25); System.out.println(":" + topDocs.totalHits + ""); // ScoreDoc[] scoreDoc = topDocs.scoreDocs; for (int i = 0; i < topDocs.scoreDocs.length; i++) { Document resultDocument = indexSearcher.doc(topDocs.scoreDocs[i].doc); PsItem myitem = new PsItem(); myitem.setName(resultDocument.get((indexField[0]))); myitem.setIntroduction(resultDocument.get((indexField[1]))); myitem.setPrice(Double.valueOf(resultDocument.get((indexField[2])))); myitem.setUrlPicture(resultDocument.get((indexField[3]))); myitem.setExtra1(resultDocument.get((indexField[4]))); myitem.setExtra2(resultDocument.get((indexField[5]))); myitem.setCountPurchase(Integer.valueOf(resultDocument.get((indexField[6])))); myitem.setCountFavourite(Integer.valueOf(resultDocument.get((indexField[7])))); myitem.setCountClick(Integer.valueOf(resultDocument.get((indexField[8])))); myitem.setVote(Double.valueOf(resultDocument.get((indexField[9])))); item.add(myitem); } return item; }
From source file:com.bitranger.parknshop.common.fulltext.SearchOrder.java
License:Open Source License
public List<PsOrder> search(String value) throws IOException { // get the index IndexReader reader = DirectoryReader.open(FSDirectory.open(BuildIndexForItem.getIndexFile())); // use this to search IndexSearcher indexSearcher = new IndexSearcher(reader); // use the queryParser to wrap your request QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_45, queryString, analyzer); List<PsOrder> order = new ArrayList<PsOrder>(); Query query = null;// w ww .j a va 2 s . co m try { query = queryParser.parse("title:" + value + "~"); } catch (ParseException e) { e.printStackTrace(); } // we get what we want in the topdocs TopDocs topDocs = indexSearcher.search(query, 25); System.out.println(":" + topDocs.totalHits + ""); // ScoreDoc[] scoreDoc = topDocs.scoreDocs; for (int i = 0; i < topDocs.scoreDocs.length; i++) { Document resultDocument = indexSearcher.doc(topDocs.scoreDocs[i].doc); PsOrder myorder = new PsOrder(); myorder.setId(Integer.valueOf(resultDocument.get((indexField[0])))); myorder.setStatus(Short.valueOf(resultDocument.get((indexField[3])))); myorder.setTrackingNumber(resultDocument.get((indexField[4]))); myorder.setPriceTotal(Double.valueOf(resultDocument.get((indexField[9])))); order.add(myorder); } return order; }
From source file:com.bitranger.parknshop.common.fulltext.SearchSeller.java
License:Open Source License
public List<PsSeller> search(String value) throws IOException { // get the index IndexReader reader = DirectoryReader.open(FSDirectory.open(BuildIndexForItem.getIndexFile())); // use this to search IndexSearcher indexSearcher = new IndexSearcher(reader); // use the queryParser to wrap your request QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_45, queryString, analyzer); List<PsSeller> seller = new ArrayList<PsSeller>(); Query query = null;/* w w w .j a va2 s. c o m*/ try { query = queryParser.parse("title:" + value + "~"); } catch (ParseException e) { e.printStackTrace(); } // we get what we want in the topdocs TopDocs topDocs = indexSearcher.search(query, 25); System.out.println(":" + topDocs.totalHits + ""); // ScoreDoc[] scoreDoc = topDocs.scoreDocs; for (int i = 0; i < topDocs.scoreDocs.length; i++) { Document resultDocument = indexSearcher.doc(topDocs.scoreDocs[i].doc); PsSeller myseller = new PsSeller(); myseller.setId(Integer.valueOf(resultDocument.get((indexField[0])))); myseller.setNickname(resultDocument.get((indexField[1]))); myseller.setPersonIdNum(resultDocument.get((indexField[2]))); myseller.setEmail(resultDocument.get((indexField[3]))); myseller.setPassword(resultDocument.get((indexField[4]))); myseller.setStatus(Short.valueOf(resultDocument.get((indexField[5])))); seller.add(myseller); } return seller; }