Lucene在索引过程中发布问题

时间:2017-05-02 01:23:59

标签: java lucene

我有lucene索引的问题,我的文档有TEXT和FIELD和DocNO。我的查询包含标题和说明。我有相关的判断。问题是当我计算MAP时,它非常小(0.017)。但是我的油炸食品价值为0.13。我认为我有一个与IndexFiles类有关的问题?你能帮助我吗 ? ^ - ^

 public class IndexFiles {
        public IndexFiles() {}
        public static void main(String[] args) throws IOException, ParseException {  
           ReadDocuments t = new ReadDocuments();         
           List<DocumentsParser> docs = new ArrayList<>();


       t.readXml(docs, "documents");
       final String FIELD_PATH = "path";
       final String FIELD_CONTENTS = "contents";

       String indexPath = "index1";

        Directory dir = FSDirectory.open(new File(indexPath));     

       Reader r=new FileReader(new File("stopwords.txt"));
      StandardAnalyzer analyzer=new StandardAnalyzer(Version.LUCENE_40,r);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40,analyzer); 

       /* use BM25 similarity*/
     Similarity bm25similarity = new BM25Similarity();
 iwc.setSimilarity(bm25similarity);

          IndexWriter indexWriter = new IndexWriter(dir, iwc);

       for (DocumentsParser  doc : docs){ 

         Document document = new Document();

    document.add(new StringField("DocNo", doc.getDOCNO(), Field.Store.YES));

    document.add(new TextField("TEXT", doc.getTEXT()+" "+doc.getHEAD(),Field.Store.YES));
       indexWriter.addDocument(document); }   
        indexWriter.close();}}

/ 类SearchFiles /

public class SearchFiles {

      public static void main(String[] args) throws Exception {
          SearchFiles ch=new SearchFiles();
              searchStemTfidfQLong();
      }
    SearchFiles() {}

       public static void  searchStemTfidfQLong() throws ParseException, IOException{
       String index = "index1";
       String field = "TEXT";

       int hitsPerPage = 1000;

       IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index)));
       IndexSearcher searcher = new IndexSearcher(reader);

       /* use BM25 similarity*/
        Similarity bm25similarity = new BM25Similarity();

       searcher.setSimilarity(bm25similarity);

         Reader r=new FileReader(new File("stopwords.txt"));
      StandardAnalyzer analyzer=new StandardAnalyzer(Version.LUCENE_40,r);

       QueryParser parser = new QueryParser(Version.LUCENE_40,field,analyzer);
       int i=0;

        File file = new File("fichier.txt");
        FileWriter writere=new FileWriter(file.getAbsoluteFile(), true);
         for(Topic topic : Parser.getQuerysTopics(Parser.filename)){    
          /*query chort*/
         String queryChort=topic.getTitle();
         queryChort=queryChort.replaceAll("([<>\\(\\):/\\\\',\\s\"])", "  ").trim();
          i++;
        //writere.write(queryChort+"\n");
          Query query = parser.parse(queryChort);
         System.out.println("Query number : "+(i));
    searcher.search(query,1000);
       doSearch(i, searcher, query, hitsPerPage);    

         } 
    reader.close();
writere.close();
       }
    public static void doSearch(int idReq, IndexSearcher searcher, Query query, int hitsPerPage) throws IOException {

        TopDocs results = searcher.search(query, null, hitsPerPage);
        System.out.println(query);
        ScoreDoc[] hits = results.scoreDocs;
        int numTotalHits = results.totalHits;

        System.out.println(numTotalHits + " total matching documents");

        int start = 1;
        int end = Math.min(numTotalHits, hitsPerPage);

            File file = new File("file.txt");
            FileWriter writer=new FileWriter(file.getAbsoluteFile(), true);
            File file1 = new File("fichier.txt");
            FileWriter writere=new FileWriter(file1.getAbsoluteFile(), true);

            for (int i = start; i < end; i++) {
            Document doc = searcher.doc(hits[i].doc);
            String DocNo = doc.get("DocNo");
            writere.write(DocNo+"\n");
            if (DocNo != null) {

       writer.write(idReq+" 0 "+DocNo+" "+String.format("%.6f", new Double(hits[i].score))+" "+ i + " "+"ScoreID"+"\n");

                            } else {
                System.out.println((i + 1) + ". " + "No DocNo for this document");          }       }
          writer.close();
        writere.close();}}

0 个答案:

没有答案