zoukankan      html  css  js  c++  java
  • lucene 7.x 查询

     1 @Test
     2     public void indexSearch() throws IOException, ParseException {
     3         
     4         //Termquery:精确string查询
     5     //    Query termQuery = new TermQuery(new Term("id","1"));
     6     //    doSearch(termQuery);
     7         
     8         //数值范围查询
     9 //        Query rangeQuery = FloatPoint.newRangeQuery("price",55,56);
    10     //    doSearch(rangeQuery);
    11         
    12         //组合查询
    13         /*Query q1 = new TermQuery(new Term("description","java"));
    14         Query q2 = new TermQuery(new Term("name","java"));
    15         
    16         BooleanQuery.Builder builder = new BooleanQuery.Builder();
    17         builder.add(new BooleanClause(q1, Occur.MUST));
    18         builder.add(new BooleanClause(q2, Occur.MUST_NOT));
    19         doSearch(builder.build());*/
    20         
    21         
    22         //queryparser,需要指定分词器
    23         /*QueryParser parser = new QueryParser("description",new StandardAnalyzer());
    24         Query query = parser.parse("lucene");
    25         query = parser.parse("description:lucene AND name:lucene");
    26         doSearch(query);*/
    27         
    28         
    29         //多域查询
    30         //默认的搜索域,或的关系
    31         String[] fields = {"name","description"};
    32         MultiFieldQueryParser parser = new MultiFieldQueryParser(fields,new StandardAnalyzer());
    33         Query query = parser.parse("lucene");
    34     //    query = parser.parse("description:lucene AND name:lucene");
    35     //    query = parser.parse("description:lucene NOT name:lucene");
    36         query = parser.parse("lucene java");//以或的关系在域中搜索
    37         doSearch(query);
    38     }
     1 private void doSearch (Query query) {
     2         Directory directory;
     3         try {
     4             directory = FSDirectory.open(Paths.get("F:/luceneIndex"));
     5         
     6         IndexReader reader = DirectoryReader.open(directory);
     7         //创建IndexSearcher
     8         IndexSearcher searcher = new IndexSearcher(reader);
     9         
    10         //10表示显示的条数
    11         TopDocs topDocs = searcher.search(query,10);
    12         long count = topDocs.totalHits;
    13         System.out.println("匹配的总条数是----" + count);
    14         
    15         //根据相关度排序后的结果
    16         ScoreDoc[] docs = topDocs.scoreDocs;
    17         for (ScoreDoc scoreDoc : docs) {
    18             int docId = scoreDoc.doc;
    19             Document document = searcher.doc(docId);
    20             System.out.println("id----"+document.get("id"));
    21             System.out.println("name----"+document.get("name"));
    22             /*System.out.println("price----"+document.get("price"));
    23             System.out.println("pic----"+document.get("pic"));
    24             System.out.println("description----"+document.get("description"));*/
    25             System.out.println("======================");
    26         }
    27         reader.close();
    28         } catch (IOException e) {
    29             // TODO Auto-generated catch block
    30             e.printStackTrace();
    31         }
    32         
    33     }

    需要注意的组合查询的api变化,与老版本的直接new BooleanQuery()相比变化还是挺大的

  • 相关阅读:
    BUAA OO Unit3 Summary——万物即可形式化
    BUAA OO Unit2 Summary
    BUAA OO Unit1 Summary
    [机器学习笔记(三)]保存加载模型的几种方式
    交互式多媒体图书平台的设计
    【tips】带公式的Markdown转pdf
    【学习笔记】码农的自我修养之必备技能
    【Callback接口初探】工程化编程实战Callback接口学习笔记
    在linux下配置VSCode的开发环境
    网络知识水平与网络编程技能水平测试
  • 原文地址:https://www.cnblogs.com/tele-share/p/9205589.html
Copyright © 2011-2022 走看看