简体   繁体   English

在Lucene搜索提供0结果

[英]Searching in Lucene provides 0 results

I am using Lucene 6.4.0 and use the following code. 我正在使用Lucene 6.4.0并使用以下代码。

But for each query it returns 0 results. 但是对于每个查询,它返回0结果。 Code seems to be correct and index is also being built but I am not able to know where I am going wrong. 代码似乎是正确的,索引也在建立,但我无法知道我哪里出错了。

public void buildIndex(){

        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        IndexWriter writer=null;
        StandardAnalyzer analyzer = null;       


        analyzer = new StandardAnalyzer();




        IndexWriterConfig config = new IndexWriterConfig(analyzer);

        try{
            System.out.println("Start indexing");
            //get a reference to index directory filejdbc:mysql://localhost/MTDATABASE


            writer =  new IndexWriter(FSDirectory.open(file.toPath()), config);

            //initialize the driver class
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            //get connection object
            con = DriverManager.getConnection(
                    "jdbc:mysql://"+DB_HOST_NAME+"/MTDATABASE",
                    DB_USER_NAME, DB_PASSWORD);
            //create statement object
            stmt = con.createStatement();
            //execute query
            rs = stmt.executeQuery("SELECT * FROM products");
            //iterate through result set
            while(rs.next()){
                productId = rs.getInt("productId");
                productImageName = rs.getString("productImageName");
                productCategory = rs.getString("productCategory");
                productBrandName = rs.getString("productBrandName");
                productType = rs.getString("productType");
                productName = rs.getString("productName");
                prop1 = rs.getString("prop1");
                prop2 = rs.getString("prop2");
                prop3 = rs.getString("prop3");
                prop4 = rs.getString("prop4");
                prop5 = rs.getString("prop5");
                description = rs.getString("description");
                price = rs.getInt("price");
                discount = rs.getFloat("discount");
                cashback = rs.getFloat("cashback");
                availability = rs.getString("availability");

                //create a full text field which contains name,
                //age and designation
                String fulltext = productId + " " + productImageName +
                " " + productCategory+" "+productBrandName+" "+productType+
                " " + productName + " "+prop1+" "+prop2+" "+prop3+" "+prop4+" "+prop5+
                " "+description+" "+price+" "+discount+" "+cashback+" "+availability;


                /*doc.add(new StringField("id",
                        Integer.toString(id), StringField.Store.YES));
                        doc.add(new TextField("title", title,
                        TextField.Store.YES));
                        w.addDocument(doc);*/

                //create document object


                addDoc(writer,productId,productImageName,productCategory,productBrandName,productType,
                        productName,prop1,prop2,prop3,prop4,prop5,description,price,discount,cashback,availability);
                writer.close();



            }




        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(writer!=null)
                    writer.close();
                if(rs!=null)
                    rs.close();
                if(stmt!=null)
                    stmt.close();
                if(con!=null)
                    con.close();
                System.out.println("Finished indexing");

            }catch(Exception ex){
                ex.printStackTrace();
            }

        }


    }
    public static void main(String[] args) throws Exception {

        IndexBuilder builder = new IndexBuilder();
        builder.buildIndex();
    }






private  void addDoc(IndexWriter w, int productId, String      productImageName,String productCategory,String productBrandName,String    productType,
                String productName,String prop1,String prop2,String    prop3,String prop4,String prop5,
                String description,int price,float discount,float   cashback,String availability) throws IOException {
   Document doc = new Document();
   doc.add(new StringField("produciId",Integer.toString(productId),   StringField.Store.YES));

   doc.add(new StringField("productImageName",productImageName,StringField.Store.YES));

   doc.add(new TextField("productCategory",productCategory,TextField.Store.YES));

   doc.add(new TextField("productBrandName",productBrandName,TextField.Store.YES));

   doc.add(new TextField("productType",productType,TextField.Store.YES));

   doc.add(new TextField("productName",productName,TextField.Store.YES));

   doc.add(new TextField("prop1",prop1,TextField.Store.YES));

   doc.add(new TextField("prop2",prop2,TextField.Store.YES));

   doc.add(new TextField("prop3",prop3,TextField.Store.YES));

   doc.add(new TextField("prop4",prop4,TextField.Store.YES));

   doc.add(new TextField("prop5",prop5,TextField.Store.YES));

   doc.add(new StringField("description",description,StringField.Store.YES));

   doc.add(new StringField("price",Integer.toString(price),StringField.Store.YES));

   doc.add(new StringField("discount",Float.toString(discount),StringField.Store.YES));

   doc.add(new StringField("cashback",Float.toString(cashback),StringField.Store.YES));

   doc.add(new StringField("availability",availability,StringField.Store.YES));

   w.addDocument(doc);
}

And for search I'am using: 为了搜索我使用:

public class Testing {

    public static void main(String[] args) {
        try{

            String LUCENE_INDEX_DIRECTORY = "C:\\lucene";

            File file = new File(LUCENE_INDEX_DIRECTORY);

            Directory index = FSDirectory.open(file.toPath());

            StandardAnalyzer analyzer = new StandardAnalyzer();

            String query = "mountain";
            QueryParser parser = new QueryParser("productName",analyzer);
            Query q = null;
            q=parser.parse(query);

            int hitsPerPage = 10;
            IndexReader reader=null;
            TopScoreDocCollector collector = null;
            IndexSearcher searcher = null;

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

            collector = TopScoreDocCollector.create(10);
            searcher.search(q,collector);
            ScoreDoc[] hits=collector.topDocs().scoreDocs;
            System.out.println(hits.length);
            if(hits.length>0){
                for(int i=0; i<hits.length; i++){
                int docId = hits[i].doc;

                Document document = searcher.doc(docId);

                System.out.println("BrandName is: "+document.get("productBrandName")+"and ProductName is: "+document.get("productName")+
                                   "productCategory is: "+document.get("productCategory")+"and prop is:"+document.get("prop1"));

            }


        }else{
            System.out.println("Not Done");

        }

    }catch(Exception e){
        System.out.println("Not done ... ");
    }




}

} }

If I were you, the first thing might be use the Luke to open the search database to see if your content were really wrote into the search index. 如果我是你,第一件事可能是使用Luke打开搜索数据库,看看你的内容是否真的被写入搜索索引。

https://github.com/DmitryKey/luke https://github.com/DmitryKey/luke

And also double check if there were any value in the "productName" being populated. 并且还要仔细检查“productName”中是否有任何值被填充。

Once you can certain the integrity of your index is good, then you can check does the search word "mountain" are really being a valid search term. 一旦你可以确定索引的完整性是好的,那么你可以检查搜索词“mountain”是否真的是一个有效的搜索词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM