0
votes

I'm new to Lucene and i'm doing hibernate search for two fields for a employee class and the fields are emp_name and emp_address but using this code

Session session = HibernateUtil.getSessionFactory().openSession();
        FullTextSession fullTextSession = Search.getFullTextSession(session);
        SearchFactory searchFactory = fullTextSession.getSearchFactory();
        String testString = "\"karthick\" AND \"tn\"";
        //String testString = '+'+"karthick AND tn";
        System.out.println(testString);
        String[] fields={"emp_name","emp_address"};
        /* Creating the Lucene Query */
        FullTextSession fSession = Search.getFullTextSession(session);
        fSession.beginTransaction();
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
        QueryBuilder mythQB = searchFactory.buildQueryBuilder().forEntity( EmpVo.class ).get();
        Query luceneQuery = mythQB.keyword().onFields("emp_name","emp_address").matching(testString).createQuery();
        org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery( luceneQuery );
        List<EmpVo> result = fullTextQuery.list();
        for (EmpVo employee : result) {
            System.out.println(employee.getEmp_name()+"------"+employee.getEmp_telno()+"------"+employee.getEmp_address());
        }

I need to get only the result for the employee name karthick and employee address tn,but for me i'm getting the result like this

karthick------137594------tn
guna------134679------tn
chandra------246810------tn
1

1 Answers

0
votes

I have found the solution by using this below syntax in query parser

String tst = "emp_name:\"karthick\" AND emp_address:\"tn\"";
        Query lucenceQuery = new QueryParser(Version.LUCENE_35, null, analyzer).parse(tst);
        List<EmpVo> employees = fSession.createFullTextQuery(lucenceQuery, EmpVo.class).list();