Results 1 to 2 of 2
  1. #1
    peliukasss is offline Member
    Join Date
    Feb 2010
    Posts
    21
    Rep Power
    0

    Default Get query indexes.

    Can anyone tell me what method to use to get all indexes of search query which is searched in Lucene field?

    I'm using:
    Java Code:
    QueryParser parser = new QueryParser(Version.LUCENE_30, "fieldname", analyzer);
    Query query = parser.parse(word);
    to parse query and searching like this:
    Java Code:
    TopScoreDocCollector collector = TopScoreDocCollector.create(500, true);
    isearcher.search(query, collector);
    But I need to get all indexes of this query in List or array. How can I do this?

  2. #2
    peliukasss is offline Member
    Join Date
    Feb 2010
    Posts
    21
    Rep Power
    0

    Default

    Found solution:

    Java Code:
        public ArrayList<String> search(String w, String corpus, int size) {
            listOfResults.clear();
            try {
                String word = w.toLowerCase();
                analyzer = new StandardAnalyzer(Version.LUCENE_30);
                //File file = new File(indexDirectory + corpus);
                File file = new File(testDir);
                directory = SimpleFSDirectory.open(file);
                
                QueryParser parser = new QueryParser(Version.LUCENE_30, corpus, analyzer);
                Query query = parser.parse(word);
    
                int hitsPerPage = 50;
                IndexSearcher searcher = new IndexSearcher(directory, true);
                IndexReader reader = IndexReader.open(directory, true);
    
                //Weight weight = query.createWeight(searcher);
                //Explanation ep = weight.explain(reader, 1);
                //System.out.println(ep.toString());
    
                TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
                searcher.search(query, collector);
                ScoreDoc[] hits = collector.topDocs().scoreDocs;
                System.out.println("Found " + hits.length + " hits.");
                
                Document hitDoc = null;
                for (int i = 0; i < hits.length; ++i) {
                    hitDoc = searcher.doc(hits[i].doc);
                    String value = hitDoc.get(corpus);
                    int docId = hits[i].doc;
    
                    TermFreqVector tfvector = reader.getTermFreqVector(docId, corpus);
                    TermPositionVector tpvector = (TermPositionVector) tfvector;
    
                    int termidx = tfvector.indexOf(word);
                    System.out.println(termidx);
                    TermVectorOffsetInfo[] tvoffsetinfo = tpvector.getOffsets(termidx);
                    
                    for (int j = 0; j < tvoffsetinfo.length; j++) {
                        int offsetStart = tvoffsetinfo[j].getStartOffset();
                        hadleSearch(value, word, offsetStart, size, j);
                    }
                }
                directory.close();
                searcher.close();
                
            } catch (ParseException ex) {
                Logger.getLogger(SimpleSearchHandler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(SimpleSearchHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
            //setInfoMap(infoMap);
            return listOfResults;
        }
    This is the whole method off searching for query and getting offsets.

Similar Threads

  1. Removing indexes from an array.
    By Cruncher in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 09:41 PM
  2. Replies: 1
    Last Post: 11-04-2009, 05:58 PM
  3. array indexes
    By Kaito in forum New To Java
    Replies: 5
    Last Post: 10-30-2009, 03:14 AM
  4. Removing Indexes
    By gilbertsavier in forum JDBC
    Replies: 0
    Last Post: 07-17-2009, 07:23 AM
  5. Creating Indexes
    By gilbertsavier in forum JDBC
    Replies: 0
    Last Post: 07-17-2009, 07:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •