Java 如何使用lucene索引查询国家代码?

Java 如何使用lucene索引查询国家代码?,java,lucene,Java,Lucene,我正在为citynames和countrycodes创建一个lucene索引(取决于彼此)。我希望国家代码是小写搜索和精确匹配 首先,我现在尝试查询单个countrycode并查找与该代码匹配的所有索引元素。我的结果总是空的 //prepare VERSION = Version.LUCENE_4_9; IndexWriterConfig config = new IndexWriterConfig(VERSION, new SimpleAnalyzer()); //index Documen

我正在为citynames和countrycodes创建一个lucene索引(取决于彼此)。我希望国家代码是小写搜索和精确匹配

首先,我现在尝试查询单个countrycode并查找与该代码匹配的所有索引元素。我的结果总是空的

//prepare
VERSION = Version.LUCENE_4_9;
IndexWriterConfig config = new IndexWriterConfig(VERSION, new SimpleAnalyzer());

//index
Document doc = new Document();
doc.add(new StringField("countryCode", countryCode, Field.Store.YES));
writer.addDocument(doc);

//lookup
Query query = new QueryParser(VERSION, "countryCode", new SimpleAnalyzer()).parse(countryCode);
结果: 当我查询诸如“IT”、“DE”、“EN”等coutrycode时,结果总是空的。为什么?
SimpleAnalyzer
来自两个字母的国家代码吗?

我在这里有点困惑。我假设您的索引编写器是在未提供的代码的某些部分中初始化的,但是您不是正在将
Version
传递到
SimpleAnalyzer
?对于
SimpleAnalyzer
,从3.X开始就没有参数构造函数了

这是我看到的唯一真正的问题。下面是一个使用您的代码的工作示例:

private static Version VERSION;

public static void main(String[] args) throws IOException, ParseException {
    //prepare
    VERSION = Version.LUCENE_4_9;
    Directory dir = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(VERSION, new SimpleAnalyzer(VERSION));
    IndexWriter writer = new IndexWriter(dir, config);

    String countryCode = "DE";

    //index
    Document doc = new Document();
    doc.add(new TextField("countryCode", countryCode, Field.Store.YES));
    writer.addDocument(doc);
    writer.close();

    IndexSearcher search = new IndexSearcher(DirectoryReader.open(dir));
    //lookup
    Query query = new QueryParser(VERSION, "countryCode", new SimpleAnalyzer(VERSION)).parse(countryCode);

    TopDocs docs = search.search(query, 1);
    System.out.println(docs.totalHits);
}

我有点困惑。我假设您的索引编写器是在未提供的代码的某些部分中初始化的,但是您不是正在将
Version
传递到
SimpleAnalyzer
?对于
SimpleAnalyzer
,从3.X开始就没有参数构造函数了

这是我看到的唯一真正的问题。下面是一个使用您的代码的工作示例:

private static Version VERSION;

public static void main(String[] args) throws IOException, ParseException {
    //prepare
    VERSION = Version.LUCENE_4_9;
    Directory dir = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(VERSION, new SimpleAnalyzer(VERSION));
    IndexWriter writer = new IndexWriter(dir, config);

    String countryCode = "DE";

    //index
    Document doc = new Document();
    doc.add(new TextField("countryCode", countryCode, Field.Store.YES));
    writer.addDocument(doc);
    writer.close();

    IndexSearcher search = new IndexSearcher(DirectoryReader.open(dir));
    //lookup
    Query query = new QueryParser(VERSION, "countryCode", new SimpleAnalyzer(VERSION)).parse(countryCode);

    TopDocs docs = search.search(query, 1);
    System.out.println(docs.totalHits);
}

我有点困惑。我假设您的索引编写器是在未提供的代码的某些部分中初始化的,但是您不是正在将
Version
传递到
SimpleAnalyzer
?对于
SimpleAnalyzer
,从3.X开始就没有参数构造函数了

这是我看到的唯一真正的问题。下面是一个使用您的代码的工作示例:

private static Version VERSION;

public static void main(String[] args) throws IOException, ParseException {
    //prepare
    VERSION = Version.LUCENE_4_9;
    Directory dir = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(VERSION, new SimpleAnalyzer(VERSION));
    IndexWriter writer = new IndexWriter(dir, config);

    String countryCode = "DE";

    //index
    Document doc = new Document();
    doc.add(new TextField("countryCode", countryCode, Field.Store.YES));
    writer.addDocument(doc);
    writer.close();

    IndexSearcher search = new IndexSearcher(DirectoryReader.open(dir));
    //lookup
    Query query = new QueryParser(VERSION, "countryCode", new SimpleAnalyzer(VERSION)).parse(countryCode);

    TopDocs docs = search.search(query, 1);
    System.out.println(docs.totalHits);
}

我有点困惑。我假设您的索引编写器是在未提供的代码的某些部分中初始化的,但是您不是正在将
Version
传递到
SimpleAnalyzer
?对于
SimpleAnalyzer
,从3.X开始就没有参数构造函数了

这是我看到的唯一真正的问题。下面是一个使用您的代码的工作示例:

private static Version VERSION;

public static void main(String[] args) throws IOException, ParseException {
    //prepare
    VERSION = Version.LUCENE_4_9;
    Directory dir = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(VERSION, new SimpleAnalyzer(VERSION));
    IndexWriter writer = new IndexWriter(dir, config);

    String countryCode = "DE";

    //index
    Document doc = new Document();
    doc.add(new TextField("countryCode", countryCode, Field.Store.YES));
    writer.addDocument(doc);
    writer.close();

    IndexSearcher search = new IndexSearcher(DirectoryReader.open(dir));
    //lookup
    Query query = new QueryParser(VERSION, "countryCode", new SimpleAnalyzer(VERSION)).parse(countryCode);

    TopDocs docs = search.search(query, 1);
    System.out.println(docs.totalHits);
}
对于StringField,可以使用TermQuery而不是QueryParser

对于StringField,可以使用TermQuery而不是QueryParser

对于StringField,可以使用TermQuery而不是QueryParser

对于StringField,可以使用TermQuery而不是QueryParser


我发现了问题:我使用的是
StringField
而不是
TextField
,因为文档中说:“一个已索引但未标记的字段:整个字符串值作为单个标记进行索引。例如,这可能用于“国家”字段或“id”。你知道这为什么不起作用吗?按照您的建议使用
TextField
时,它会起作用。+是的,该编辑确实解释了这一点。StringField没有得到分析,索引表示仍然是大写的。您的查询仍在分析中,因此SimpleAnalyzer会将其小写。我发现了问题:我使用的是
StringField
,而不是
TextField
,因为文档中说:索引但未标记化的字段:整个字符串值作为单个标记进行索引。例如,这可能用于“国家”字段或“id”“.你知道这为什么不起作用吗?按照您的建议使用
TextField
时,它会起作用。+是的,该编辑确实解释了这一点。StringField没有得到分析,索引表示仍然是大写的。您的查询仍在分析中,因此SimpleAnalyzer会将其小写。我发现了问题:我使用的是
StringField
,而不是
TextField
,因为文档中说:索引但未标记化的字段:整个字符串值作为单个标记进行索引。例如,这可能用于“国家”字段或“id”“.你知道这为什么不起作用吗?按照您的建议使用
TextField
时,它会起作用。+是的,该编辑确实解释了这一点。StringField没有得到分析,索引表示仍然是大写的。您的查询仍在分析中,因此SimpleAnalyzer会将其小写。我发现了问题:我使用的是
StringField
,而不是
TextField
,因为文档中说:索引但未标记化的字段:整个字符串值作为单个标记进行索引。例如,这可能用于“国家”字段或“id”“.你知道这为什么不起作用吗?按照您的建议使用
TextField
时,它会起作用。+是的,该编辑确实解释了这一点。StringField没有得到分析,索引表示仍然是大写的。您的查询仍在分析中,因此SimpleAnalyzer会将其小写。