日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/8/23 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转载保存】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(11,?1), 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",?11,?22);

????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",?11,?22,?33);

????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 三個方法的用法即可。

總結

以上是生活随笔為你收集整理的【转载保存】Lucene 实战教程第六章 Lucene 的精确、包含、集合查询 Query 的简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。