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

    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- Indexer.java class is used to index the raw data so that we can make it searchable using lucene library.
    2- LuceneConstants.java class is used to provide various constants to be used across the sample application.
    3- Searcher.java class is used to search the indexes created by Indexer to search the requested contents.
    4- TextFileFilter.java class is used as a .txt file filter.
    5- LuceneTester.java 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 Indexer.java and retrieve it through query in LuceneTester.java . Any one help me

  2. #2
    Join Date
    Nov 2015
    Posts
    3
    Rep Power
    0

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

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

    To add the Field in Inder.java for Term Vector Position

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

    document.add(fieldTVP);
    Now in LuceneTester.java
    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 = FSDirectory.open(new 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 = IndexReader.open(index, true);
    searcher.setDefaultFieldSortScoring(true, true);
    //searcher.setDefaultFieldSortScoring(true, false);
    TopScoreDocCollector collector = TopScoreDocCollector.create(LuceneConstants.MAX_SE ARCH, true);
    searcher.search(q, 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);
    //System.out.println("termidx"+termidx);
    int[] termposx = tpvector.getTermPositions(termidx);
    TermVectorOffsetInfo[] tvoffsetinfo = tpvector.getOffsets(termidx);
    //System.out.println(termposx);
    //System.out.println(tvoffsetinfo.length);

    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;
    do
    {

    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.
    searcher.close();
    }
    Last edited by Muhammad Adnan; 12-01-2015 at 08: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, 11:12 AM
  2. Lucene indexing problem
    By Blacky777 in forum New To Java
    Replies: 9
    Last Post: 12-05-2012, 02:43 PM
  3. Lucene indexing help
    By JP10 in forum Lucene
    Replies: 1
    Last Post: 07-25-2011, 05:34 PM
  4. Indexing XML using lucene
    By peliukasss in forum Lucene
    Replies: 0
    Last Post: 03-28-2010, 10:20 PM
  5. Lucene Re-Indexing
    By connect2srinath in forum Lucene
    Replies: 1
    Last Post: 05-11-2008, 05: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
  •