【发布时间】:2014-03-02 17:58:43
【问题描述】:
问题:
如何在 lucene 4.5 中将一个字段中的完全匹配和另一个字段中的模糊搜索结合起来?
问题:
我已在 lucene 索引中索引了 NGA Geonames 地名词典。我需要模糊查询一个字段(地名),但将查询限制为具有特定国家代码的记录。这是我正在运行的示例查询
我没有使用 SOLR,我已经做了很多研究和反复试验,但我没有明确的答案,可能是我速度慢。
FULL_NAME_ND_RO:india AND CC1:in
我想要在印度进行模糊搜索,但我只想要与“in”(国家代码)完全匹配的记录
Here is what I've tried:
1. Index every field as a textfield and boost the country code field using ^N. Still returns different country codes, and the one boosted does not always come first...
2. Index every field as text EXCEPT the country code, which I indexed as StringField. This way I get no results at all.
这里是 Gaz 的索引代码:
public void index(File outputIndexDir, File gazateerInputData, GazType type) throws Exception {
if (!outputIndexDir.isDirectory()) {
throw new IllegalArgumentException("outputIndexDir must be a directory.");
}
String indexloc = outputIndexDir + type.toString();
Directory index = new MMapDirectory(new File(indexloc));
Analyzer a = new StandardAnalyzer(Version.LUCENE_45);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_45, a);
IndexWriter w = new IndexWriter(index, config);
readFile(gazateerInputData, w, type);
w.commit();
w.close();
}
public void readFile(File gazateerInputData, IndexWriter w, GazType type) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader(gazateerInputData));
List<String> fields = new ArrayList<String>();
int counter = 0;
// int langCodeIndex = 0;
System.out.println("reading gazateer data from file...........");
while (reader.read() != -1) {
String line = reader.readLine();
String[] values = line.split(type.getSeparator());
if (counter == 0) {
for (String columnName : values) {
fields.add(columnName.replace("»¿", "").trim());
}
} else {
Document doc = new Document();
for (int i = 0; i < fields.size() - 1; i++) {
if (fields.get(i).equals("CC1")) {
doc.add(new StringField(fields.get(i), values[i], Field.Store.YES));
} else {
doc.add(new TextField(fields.get(i), values[i], Field.Store.YES));
}
}
w.addDocument(doc);
}
counter++;
if (counter % 10000 == 0) {
w.commit();
System.out.println(counter + " .........committed to index..............");
}
}
w.commit();
System.out.println("Completed indexing gaz! index name is: " + type.toString());
}
这是运行查询的代码
QueryParser parser = new QueryParser(Version.LUCENE_45, luceneQueryString, geonamesAnalyzer);
Query q = parser.parse(luceneQueryString);
TopDocs search = geonamesSearcher.search(q, rowsReturned);
geonamesAnalyzer 是一个 StandardAnalyzer....luceneQueryString 就像上面的查询。
任何建议都会很棒。
【问题讨论】:
标签: lucene