Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2015
    Rep Power

    Lightbulb How to add a Field for word position in indexing file using Lucene

    i am new in lucene environment and lean from web tutorial Lucene - First Application.
    1- class is used to index the raw data so that we can make it searchable using lucene library.
    2- class is used to provide various constants to be used across the sample application.
    3- class is used to search the indexes created by Indexer to search the requested contents.
    4- class is used as a .txt file filter.
    5- lass is used to test the indexing and search capability of lucene library..

    Now I am trying to index the Field for word position (TermVectorPosition) in and retrieve it through query in . Any one help me

  2. #2
    Join Date
    Nov 2015
    Rep Power

    Default Re: How to add a Field for word position in indexing file using Lucene

    Below code are added in and files. but still face a problem, Help me to retrieve the term position using TERMVECTOPOSITION....

    To add the Field in for Term Vector Position

    BufferedReader in=new BufferedReader(new FileReader(file));
    String temp;
    StringBuffer sb=new StringBuffer();
    Field fieldTVP = new Field(LuceneConstants.Term_Vector_Position,sb.toSt ring(),Field.Store.YES,Field.Index.ANALYZED,TermVe ctor.WITH_POSITIONS_OFFSETS);

    Now in
    public void doSearch(String querystr) throws IOException, ParseException {
    // 1. Specify the analyzer for tokenizing text.
    // The same analyzer should be used as was used for indexing
    String Dpath =indexDir;
    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);

    Directory index = File(Dpath));

    // 2. query

    Query q = new QueryParser(Version.LUCENE_36, LuceneConstants.Term_Vector_Position, analyzer).parse(querystr);

    // 3. search

    IndexSearcher searcher = new IndexSearcher(index, true);
    IndexReader reader =, true);
    searcher.setDefaultFieldSortScoring(true, true);
    //searcher.setDefaultFieldSortScoring(true, false);
    TopScoreDocCollector collector = TopScoreDocCollector.create(LuceneConstants.MAX_SE ARCH, true);, collector);
    ScoreDoc[] hits = collector.topDocs().scoreDocs;

    // 4. display term positions, and term indexes
    System.out.println("Found " + hits.length + " hits.");
    for(int i=0;i<=hits.length;i++) {

    int docId = hits[i].doc;
    // System.out.println(docId);
    TermFreqVector tfvector = reader.getTermFreqVector(docId,LuceneConstants.Ter m_Vector_Position);
    // System.out.println(tfvector);
    TermPositionVector tpvector = (TermPositionVector)tfvector;
    // System.out.println(tpvector);
    // this part works only if there is one term in the query string,
    // otherwise you will have to iterate this section over the query terms.
    int termidx = tfvector.indexOf(querystr);
    int[] termposx = tpvector.getTermPositions(termidx);
    TermVectorOffsetInfo[] tvoffsetinfo = tpvector.getOffsets(termidx);

    for (int j=0;j<termposx.length;j++) {
    System.out.println("termpos : "+termposx[j]);
    // for (int j=0;j<=tvoffsetinfo.length;j++)
    int j=1;

    int offsetStart = tvoffsetinfo[j].getStartOffset();
    int offsetEnd = tvoffsetinfo[j].getEndOffset();
    System.out.println("offsets : "+offsetStart+" "+offsetEnd);
    } while(j<=tvoffsetinfo.length);

    // print some info about where the hit was found...
    Document d = searcher.doc(docId);
    System.out.println((i + 1) + ". " + d.get("filepath"));

    // searcher can only be closed when there
    // is no need to access the documents any more.
    Last edited by Muhammad Adnan; 12-01-2015 at 09:22 AM.

Similar Threads

  1. Hai, How to do Lucene indexing based on keywords?
    By srikanth.vaddanti in forum Lucene
    Replies: 0
    Last Post: 03-04-2013, 12:12 PM
  2. Lucene indexing problem
    By Blacky777 in forum New To Java
    Replies: 9
    Last Post: 12-05-2012, 03:43 PM
  3. Lucene indexing help
    By JP10 in forum Lucene
    Replies: 1
    Last Post: 07-25-2011, 06:34 PM
  4. Indexing XML using lucene
    By peliukasss in forum Lucene
    Replies: 0
    Last Post: 03-28-2010, 11:20 PM
  5. Lucene Re-Indexing
    By connect2srinath in forum Lucene
    Replies: 1
    Last Post: 05-11-2008, 06:35 PM

Tags for this Thread

Posting Permissions

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