Skip to main content

Desktop Inside Document Searcher


While I was working with lucene I tried to analyze it for the purpose of creating a demo search engine . From That I got the plan to make a search tool for pc which will search in .txt , .cpp,.pdf,.ppt,.doc as search engine do. Like If I have written sieve or kmp in any file my tool will find it ,rank it and give the location .After finding location just paste it in the explorer and it will give you the particular file.

Work Flow :

1.Indexed the particular drive.
2.Then search option enable
3. Give the input string for search
4.Output will be given rank wise in an area.
5. Copy the link and paste it to the explorer.
6.Cheachk is this you need.


There are three classes
1.RS.java (Main Class)
2.Indexer.java
3.Searcher.java

You must have to add the api of lucene here.


Rs.java

/**
 * @
 *
 *
 * @author sharma
 * @version 1.00 2010/7/16
 */

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import java.net.URLConnection;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Transparency;
import javax.swing.event.*;
import java.sql.*;
import java.util.*;
import java.net.URL;
import javax.imageio.ImageIO;
import java.lang.*;

import java.io.*;
import javax.swing.BoxLayout;

public class Rs extends JFrame {
    
    String name="";
    String str[] = new String[1000];
    int k,chk;
    StringBuilder builder;

    JLabel Send_lebel = new JLabel();
    JTextField send_bit = new JTextField();
    
    JLabel Rec_lebel = new JLabel();
    JTextArea rec_bit = new JTextArea();
    JTextField err = new JTextField();
    
    JButton gen = new JButton();
    JButton sen = new JButton();
    JButton cal = new JButton();
    JScrollPane scrollPane = new JScrollPane();
    int a[] = new int[100];
    int b[] = new int[100];
    WindowListener l;

    public Rs(){
        
        
        setTitle("INDEXER AND SEARCHER DEMO");
        setSize(600,453);
        setLocation(220,100);

        l = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                setEnabled(true);
            }
        };
         setUndecorated(true);
        getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
        setVisible(true);
        jbinit();
        
    }
    
    
       void jbinit(){
             this.setResizable(false);
             this.getContentPane().setLayout(null);
             
             Send_lebel.setText("ADDRESS / TEXT");
             Send_lebel.setBounds(new Rectangle(18, 18, 150, 42));
             send_bit.setFont(new java.awt.Font("Dialog", 1, 12));
             send_bit.setText("");
             send_bit.setBounds(new Rectangle(18, 48, 156, 21));
             
             Rec_lebel.setText("LINKS:");
             Rec_lebel.setBounds(new Rectangle(330, 18, 132, 21));
             rec_bit.setFont(new java.awt.Font("Dialog", 1, 12));
             rec_bit.setText("");
             rec_bit.setBounds(new Rectangle(212,48,336,341));
             scrollPane.setBounds(new Rectangle(212, 48, 336, 341));
             
             gen.setBounds(new Rectangle(18, 100, 88, 24));
             gen.setFont(new java.awt.Font("Dialog", 1, 12));
             gen.setBorder(BorderFactory.createRaisedBevelBorder());
             gen.setMnemonic('I');
             gen.setText("INDEXING");     
             gen.addActionListener(new gen_actionAdapter(this));
            
             sen.setBounds(new Rectangle(118, 100, 88, 24));
             sen.setFont(new java.awt.Font("Dialog", 1, 12));
             sen.setBorder(BorderFactory.createRaisedBevelBorder());
             sen.setMnemonic('S');
             sen.setText("SEARCH");     
             sen.addActionListener(new sen_actionAdapter(this));
            
                     
                        
             
             this.getContentPane().add(Send_lebel, null);
             this.getContentPane().add(Rec_lebel, null);
             this.getContentPane().add(scrollPane,null);
             scrollPane.getViewport().add(rec_bit,null);
             this.getContentPane().add(send_bit, null);  
             this.getContentPane().add(gen,null);
             this.getContentPane().add(sen,null);      
               

               

    }

    public static void main(String[] args) {
       Rs jmain = new Rs();

       jmain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    
   
// all button action
void gen_actionPerformed(ActionEvent e)
{

 String pre ="";
 String rc="";
          name = send_bit.getText().toLowerCase();
       
              
            try {
            
    
             
             rc = name;
             Indexer ind = new Indexer("D:",rc);
             pre += ind.pep;
                 
             rec_bit.setText(pre);
            }
            
            catch (Exception ex) {
          
            
        }
            
            
}

void sen_actionPerformed(ActionEvent e)
{
            String sp ="",nam;
            nam = send_bit.getText().toLowerCase();
            try {
                  Searcher sc =  new Searcher("D:",nam);
                  sp += sc.prs;
                  rec_bit.setText(sp);
                } 
                catch (Exception ep) 
                    {
                        System.out.print("failed writing"); 
                    } 
}



}



// all button class

class gen_actionAdapter implements java.awt.event.ActionListener {
Rs adaptee;

    gen_actionAdapter(Rs adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.gen_actionPerformed(e);
  }
}


class sen_actionAdapter implements java.awt.event.ActionListener {
Rs adaptee;

    sen_actionAdapter(Rs adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.sen_actionPerformed(e);
  }
}





Indexer.java




/**
 * @(#)Indexer.java
 *Lucene 2.9
 *
 * @author Sharma 
 * @version 1.00 2011/1/30
 * 
 *comand prompt
 *DwnlData\New Folder (2)\Thesis\Lucene - Opensource search Engine\lucene-1.9-final\src\java
 *java Indexer.java D: C:lucene
 */

import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import java.io.FileReader;

import java.io.IOException;



import java.util.*;
import java.io.*;
import java.lang.*;


public class Indexer {
    public  String prp="";
    public String pep="";
  /*public static void main(String[] args) throws Exception {
    if (args.length != 2) {
      throw new Exception("Usage: java " + Indexer.class.getName()
        + " <index dir> <data dir>");
    }
    File indexDir = new File(args[0]);   
    File dataDir = new File(args[1]);   
    long start = new Date().getTime();
    int numIndexed = index(indexDir, dataDir);
    long end = new Date().getTime();
    System.out.println("Indexing " + numIndexed + " files took "
      + (end - start) + " milliseconds");
  }
  */
  Indexer(String rs,String rp)
  {
      System.out.println(rs+" "+rp);
  
    File indexDir = new File(rs);   
    File dataDir = new File(rp);   
    long start = new Date().getTime();
    try{
        int numIndexed = index(indexDir, dataDir);
        long end = new Date().getTime();
        //System.out.println("Indexing " + numIndexed + " files took "
       // + (end - start) + " milliseconds");
        pep += "Indexing " + numIndexed + " files took "+ (end - start) + " milliseconds"+"\n";
        
    }
    catch(Exception e)
    {
        System.out.print(" "+e);
    }

  }
  
  // open an index and start file directory traversal
  public int index(File indexDir, File dataDir)
    throws IOException 
        {
            if (!dataDir.exists() || !dataDir.isDirectory()) 
            {
                throw new IOException(dataDir+ " does not exist or is not a directory");
               }
        IndexWriter writer = new IndexWriter(indexDir,new StandardAnalyzer(), true);
        writer.setUseCompoundFile(false);
        indexDirectory(writer, dataDir);
        int numIndexed = writer.docCount();
        writer.optimize();
        writer.close();    
        return numIndexed;
      }
      
    private void indexDirectory(IndexWriter writer, File dir)
    throws IOException {
    File[] files = dir.listFiles();
    for (int i = 0; i < files.length; i++) {
      File f = files[i];
      if (f.isDirectory()) {
        indexDirectory(writer, f);   
      } else if (f.getName().endsWith(".txt")|| f.getName().endsWith(".pdf")||f.getName().endsWith(".doc") ||f.getName().endsWith(".docx")) {   
        indexFile(writer, f);
      }
    }
  }
  
    // method to actually index a file using Lucene
  private void indexFile(IndexWriter writer, File f)
    throws IOException {
    if (f.isHidden() || !f.exists() || !f.canRead()) {
      return;
    }
   // System.out.println("Indexing " + f.getCanonicalPath());
    
     pep += "Indexing " + f.getCanonicalPath()+"\n";
     
     //rp.rec_bit.setText(pep);
    Document doc = new Document();
    doc.add(Field.Text("contents", new FileReader(f)));   
    doc.add(Field.Keyword("filename", f.getCanonicalPath()));
    writer.addDocument(doc);                  
  }
}





Searcher.java



/**
 * @(#)Searcher.java
 *
 *
 * @author Sharma
 * @version 1.00 2011/2/1
 */


import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.*;
import org.apache.lucene.store.RAMDirectory;
import java.io.FileReader;

import java.io.IOException;



import java.util.*;
import java.io.*;
import java.lang.*;
public class Searcher 
{
    String prs = "";

  /*public static void main(String[] args) throws Exception {
    if (args.length != 2) 
    {
        throw new Exception("Usage: java " + Searcher.class.getName()
        + " <index dir> <query>");
    }
    
    File indexDir = new File(args[0]);   
    String q = args[1];   
    if (!indexDir.exists() || !indexDir.isDirectory()) 
    {
      throw new Exception(indexDir +" does not exist or is not a directory.");
    }
    search(indexDir, q);
  }*/
  
    public  Searcher(String f, String q)
    throws Exception {
           
            File indexDir = new File(f);
            Directory fsDir = FSDirectory.getDirectory(indexDir, false);
            IndexSearcher is = new IndexSearcher(fsDir);   
            Query query = QueryParser.parse(q, "contents",new StandardAnalyzer());
    long start = new Date().getTime();
    Hits hits = is.search(query);    
    long end = new Date().getTime();
    System.err.println("Found " + hits.length() +" document(s) (in " + (end - start) +" milliseconds) that matched query '" + q + "':");
    prs += "Found " + hits.length() +" document(s) (in " + (end - start) +" milliseconds) that matched query '" + q + "':\n";
    for (int i = 0; i < hits.length(); i++) {
      Document doc = hits.doc(i); 
      prs += doc.get("filename");
      prs += "\n";                 
      System.out.println(doc.get("filename"));   
    }
  }
}



Comments

Popular posts from this blog

UDP server client in c

Server #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <stdlib.h> int main() {         int sock;         int addr_len, bytes_read;         char recv_data[1024],send_data[1024];         struct sockaddr_in server_addr , client_addr;         if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {             perror("Socket");             exit(1);         }         server_addr.sin_family = AF_INET;         server_addr.sin...

[ASTERIK] configure: error: *** uuid support not found (this typically means the uuid development package is missing)

ISSUE: Build error on Asterik , when you want test webrtc feature :) checking for uuid_generate_random in -luuid... no checking for uuid_generate_random in -le2fs-uuid... no checking for uuid_generate_random... no configure: error: *** uuid support not found (this typically means the uuid development package is missing) Fix: This issue arises due to missing of UUID generator specified by rfc4122 . +Linux sudo apt-get install uuid-dev  @Unix yum -y install libuuid-devel Asterik comes with lots of helpful script available on - asterisk/contrib/scripts/ folder of your ASTERIK source. So just use the following command on UNIX console to run the asterik pre-requisite script. contrib/scripts/install_prereq install And you are done! configuring. Now -- Make Asterik.

My favourite writer Humayun Ahmed

There is none who can replace him.At least the standard which he create in is life time in the running literature it never be replaceable.The new generation which is all the time busy in playing PC games,chatting ,bands etc. only his writing makes them to take a glance on the literature.For example Himu and Misir ali all the time keep them on track by anti-logic and logic.They also have show-down on the novel.I myself read all of the books of Himu and Misir ali and wait for the new one to come every year.Now I have to wait for life time. Except books he makes our dirty film industry pure by his heart warming ,well versed film.Following him many new producer try to make well and good film (not the dirty one) .He also contribute our drama by his dashing drama's. In writing except romance ,logic ,anti-logic ,he also write many science fiction.His brother Sir Dr. Md. Zafar Iqbal is the man who started science fiction in bangla. The list of books: Selected novels • Lilaboti (2...