lucene查询介绍

对于某字段的关键字的模糊查询
Query query=new WildcardQuery(new Term("sender","*davy*"));
        Searcher searcher=new IndexSearcher(indexFilePath);

        Hits hits=searcher.search(query);

        for (int i = 0; i < hits.length(); i++) {

            System.out.println(hits.doc(i).get("name"));

        }



对于某字段的关键字的语义查询
Query query=QueryParser.parse("索引","title",analyzer);

        Searcher searcher=new IndexSearcher(indexFilePath);

        Hits hits=searcher.search(query);

        for (int i = 0; i < hits.length(); i++) {

            System.out.println(hits.doc(i).get("name"));

        }




对于多字段的关键字的查询
Query query=MultiFieldQueryParser.parse("索引",new String[]{"title","content"},analyzer);    

        Searcher searcher=new IndexSearcher(indexFilePath);

        Hits hits=searcher.search(query);

        for (int i = 0; i < hits.length(); i++) {

            System.out.println(hits.doc(i).get("name"));

        }




复合查询(多种查询条件的综合查询)
Query query=MultiFieldQueryParser.parse("索引",new String[]{"title","content"},analyzer);

        Query mquery=new WildcardQuery(new Term("sender","bluedavy*"));

        TermQuery tquery=new TermQuery(new Term("name","jerry"));

        BooleanQuery bquery=new BooleanQuery();

        bquery.add(query,true,false);

        bquery.add(mquery,true,false);

        bquery.add(tquery,true,false);       

        Searcher searcher=new IndexSearcher(indexFilePath);

        Hits hits=searcher.search(bquery);

        for (int i = 0; i < hits.length(); i++) {

            System.out.println(hits.doc(i).get("name"));

        }

你可能感兴趣的:(java,Lucene)