使用 Lucene 库,我需要对现有的搜索功能进行一些更改: 让我们假设以下对象:
名称:“端口对象 1”
数据:“TCP (1)/1000-2000”
查询(或搜索文本)是“1142” 是否可以在数据字段中搜索“1142”并找到端口对象 1,因为它指的是 1000-2000 之间的范围?
我只找到了数字范围查询,但这不适用于本例,因为我不知道范围...
package com.company;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
public class Main {
public static void main(String[] args) throws IOException, ParseException {
StandardAnalyzer analyzer = new StandardAnalyzer();
// 1. create the index
Directory index = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
IndexWriter w = new IndexWriter(index, config);
addDoc(w, "TCP (6)/1100-2000", "193398817");
addDoc(w, "TCP (6)/3000-4200", "55320055Z");
addDoc(w, "UDP (12)/50000-65000", "55063554A");
w.close();
// 2. query
String querystr = "1200";
Query q = new QueryParser("title", analyzer).parse(querystr);
// 3. search
int hitsPerPage = 10;
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs docs = searcher.search(q, hitsPerPage);
ScoreDoc[] hits = docs.scoreDocs;
// 4. display results
System.out.println("Found " + hits.length + " hits.");
for(int i=0;i<hits.length;++i) {
int docId = hits[i].doc;
Document d = searcher.doc(docId);
System.out.println((i + 1) + ". " + d.get("isbn") + "\t" + d.get("title"));
}
reader.close();
}
private static void addDoc(IndexWriter w, String title, String isbn) throws IOException {
Document doc = new Document();
doc.add(new TextField("title", title, Field.Store.YES));
doc.add(new StringField("isbn", isbn, Field.Store.YES));
w.addDocument(doc);
}
}
引用上面的代码。 查询“1200”应该找到第一个文档。
LE:
我认为我需要的与范围搜索完全相反: https://lucene.apache.org/core/5_5_0/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#Range_Searches
最佳答案
这是一种方法,但它要求您将范围数据解析为单独的值,然后 Lucene 才能对数据进行索引。因此,例如,从此:
"TCP (6)/1100-2000"
您需要提取这两个值(例如使用正则表达式):1100
和 2000
。
LongRange 与 ContainsQuery
向每个文档添加一个新字段(例如名为“tcpRange”)并将其定义为 LongRange
字段。
(如果不需要长值,还有 IntRange
。)
long[] min = { 1100 };
long[] max = { 2000 };
Field tcpRange = new LongRange("tcpRange", min, max);
这些值在数组中定义,因为此范围类型可以处理一个字段中的多个范围。但在我们的例子中,我们只需要一个范围。
然后您可以使用“contains ”查询来搜索您的特定值,例如1200
:
long[] searchValue = { 1200 };
Query containsQuery = LongRange.newContainsQuery("tcpRange", searchValue, searchValue);
注意:我的示例基于最新版本的 Lucene (8.5)。我相信这也应该适用于其他早期版本。
编辑
关于此答案的评论中提出的其他问题...
以下方法将 IPv4 地址转换为 long
值。使用此选项可以处理 IP 地址范围(并且可以使用与上面相同的 LongRange
方法):
public long ipToLong(String ipAddress) {
long result = 0;
String[] ipAddressInArray = ipAddress.split("\\.");
for (int i = 3; i >= 0; i--) {
long ip = Long.parseLong(ipAddressInArray[3 - i]);
// left shifting 24, 16, 8, 0 with bitwise OR
result |= ip << (i * 8);
}
return result;
}
这也意味着不必处理有效的子网范围 - 任何两个 IP 地址都将生成一组连续的数字。
归功于this mkyong site的方法。
关于Java Lucene 搜索 - 是否可以搜索某个范围内的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62303346/