【转载保存】Lucene 实战教程第六章 Lucene 的精确、包含、集合查询 Query 的简单使用

原链接:https://www.xttblog.com/?p=3532

所有的搜索基本上都存在精确匹配,包含等操作。Lucene 中同样存在这样的操作,今天我们以 IntPoint 为例,来说说 Lucene 中的精确查询。

IntPoint、LongPoint、FloatPoint、DoublePoint 这个 4 个的操作类似,我就只以 IntPoint 来进行举例。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

@Test

public void testIntPointQuery() throws IOException {

    Directory directory = new RAMDirectory();

    IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig(new StandardAnalyzer()));

 

    // 创建 Document

    Document document = new Document();

 

    // 创建 Field 字段/域

    Field intPoint = new IntPoint("age"11);

    document.add(intPoint);

    intPoint = new StoredField("age"11);

    document.add(intPoint);

    indexWriter.addDocument(document);

 

    Field intPoint1 = new IntPoint("age"22);

    document = new Document();

    document.add(intPoint1);

    intPoint1 = new StoredField("age"22);

    document.add(intPoint1);

     

    indexWriter.addDocument(document);

    indexWriter.close();

    IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(directory));

    //精确查询

    Query query = IntPoint.newExactQuery("age"11);

    ScoreDoc[] scoreDocs = indexSearcher.search(query, 10).scoreDocs;

    for (ScoreDoc scoreDoc : scoreDocs) {

        System.out.println("精确查询:" + indexSearcher.doc(scoreDoc.doc));

    }

    //范围查询,不包含边界

    query = IntPoint.newRangeQuery("age", Math.addExact(111), Math.addExact(22, -1));

    scoreDocs = indexSearcher.search(query, 10).scoreDocs;

    for (ScoreDoc scoreDoc : scoreDocs) {

        System.out.println("不包含边界:" + indexSearcher.doc(scoreDoc.doc));

    }

    //范围查询,包含边界

    query = IntPoint.newRangeQuery("age"1122);

    scoreDocs = indexSearcher.search(query, 10).scoreDocs;

    for (ScoreDoc scoreDoc : scoreDocs) {

        System.out.println("包含边界:" + indexSearcher.doc(scoreDoc.doc));

    }

    //范围查询,左包含,右不包含

    query = IntPoint.newRangeQuery("age"11, Math.addExact(22, -1));

    scoreDocs = indexSearcher.search(query, 10).scoreDocs;

    for (ScoreDoc scoreDoc : scoreDocs) {

        System.out.println("左包含右不包含:" + indexSearcher.doc(scoreDoc.doc));

    }

    //集合查询

    query = IntPoint.newSetQuery("age"112233);

    scoreDocs = indexSearcher.search(query, 10).scoreDocs;

    for (ScoreDoc scoreDoc : scoreDocs) {

        System.out.println("集合查询:" + indexSearcher.doc(scoreDoc.doc));

    }

}

IntPoint.newExactQuery 精确查询,使用的是 PointRangeQuery。
IntPoint.newRangeQuery 范围查询,使用的是 PointRangeQuery。
IntPoint.newSetQuery 集合查询,使用的是 PointInSetQuery。

它们都继承自 Query,通过 IntPoint 去创建这些抽象类的匿名实现类。

LongPoint、FloatPoint、DoublePoint 封装的和 IntPoint 都很相似,我就不在列举了。大家主要记住 newExactQuery,newRangeQuery,newSetQuery 三个方法的用法即可。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值