使用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
答案 0 :(得分:0)
这是一种方法,但是在Lucene可以对数据进行索引之前,它要求您将范围数据解析为单独的值。因此,例如,从此:
"TCP (6)/1100-2000"
您需要提取这两个值(例如,使用正则表达式):1100
和2000
。
向每个文档添加一个新字段(例如,名为“ 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。
答案 1 :(得分:0)
我设法添加了另一个字段,现在可以使用了。另外,您知道除了IPv4之外,我该如何进行相同的搜索?如果我在“ 192.168.0.1-192.168.0.255”字符串中搜索类似“ 192.168.0.100”的内容?
您好,@ CristianNicolaePerjescu,由于我的声誉,我无法发表评论,但是您可以创建一个扩展Field的类,并将其添加到您的Lucene索引中。例如:
public class InetAddressRange extends Field {
...
/**
* Create a new InetAddressRange from min/max value
* @param name field name. must not be null.
* @param min range min value; defined as an {@code InetAddress}
* @param max range max value; defined as an {@code InetAddress}
*/
public InetAddressRange(String name, final InetAddress min, final InetAddress max) {
super(name, TYPE);
setRangeValues(min, max);
}
...
}
然后添加到索引:
document.add(new InetAddressRange("field", InetAddressFrom, InetAddressTo));
在您的课程中,您可以添加自己的查询格式,例如:
public static Query newIntersectsQuery(String field, final InetAddress min, final InetAddress max) {
return newRelationQuery(field, min, max, QueryType.INTERSECTS);
}
/** helper method for creating the desired relational query */
private static Query newRelationQuery(String field, final InetAddress min, final InetAddress max, QueryType relation) {
return new RangeFieldQuery(field, encode(min, max), 1, relation) {
@Override
protected String toString(byte[] ranges, int dimension) {
return InetAddressRange.toString(ranges, dimension);
}
};
}
我希望这对您有帮助。