Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Lucene的正则表达式匹配_Java_Regex_Lucene - Fatal编程技术网

Java 使用Lucene的正则表达式匹配

Java 使用Lucene的正则表达式匹配,java,regex,lucene,Java,Regex,Lucene,我想用正则表达式查找Lucene的“Bug报告”,但每次尝试都不起作用 我使用来自的代码来避免错误的设置 这是我的密码: import java.util.regex.Pattern; import org.apache.lucene.analysis.SimpleAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.luc

我想用正则表达式查找Lucene的“Bug报告”,但每次尝试都不起作用

我使用来自的代码来避免错误的设置

这是我的密码:

import java.util.regex.Pattern;

import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.regex.JakartaRegexpCapabilities;
import org.apache.lucene.search.regex.RegexCapabilities;
import org.apache.lucene.search.regex.RegexQuery;
import org.apache.lucene.store.RAMDirectory;

public class Rege {

  private static IndexSearcher searcher;
  private static final String FN = "field";

  public static void main(String[] args) throws Exception {
    RAMDirectory directory = new RAMDirectory();
    try {

      IndexWriter writer = new IndexWriter(directory,
          new SimpleAnalyzer(), true,
          IndexWriter.MaxFieldLength.LIMITED);
      Document doc = new Document();
      doc
          .add(new Field(
              FN,
              "[Phpmyadmin-devel] Commits against bug 601721 (Cookie auth mode faulty with IIS)",
              Field.Store.NO, Field.Index.ANALYZED));
      writer.addDocument(doc);
      writer.optimize();
      writer.close();
      searcher = new IndexSearcher(directory, true);

    } catch (Exception e) {
      e.printStackTrace();
    }

    System.err.println(regexQueryNrHits("bug [0-9]+",null));

  }

  private static Term newTerm(String value) {
    return new Term(FN, value);
  }

  private static int regexQueryNrHits(String regex,
      RegexCapabilities capability) throws Exception {

    RegexQuery query = new RegexQuery(newTerm(regex));

    if (capability != null)
      query.setRegexImplementation(capability);

    return searcher.search(query, null, 1000).totalHits;
  }

}

我希望
bug[0-9]+
返回
1
,但它没有返回。我还用Java测试了正则表达式,它运行正常。

如果将字段索引为“字符串”类型(而不是“文本”类型),则正则表达式必须匹配整个字段值。
尝试此操作,将正则表达式带到字段的两端:

System.err.println(regexQueryNrHits("^.*bug [0-9]+.*$",null));

谢谢,但这并不能解决问题。问题在于
字段.Index.ANALYZED
标志:

lucene似乎没有以适当的方式对数字进行索引,以便可以使用正则表达式

我改变了:

doc.add(new Field(
FN,"[Phpmyadmin-devel] Commits against bug 601721 (Cookie auth mode faulty with IIS)",Field.Store.NO, Field.Index.ANALYZED));

使用改进的正则表达式:

    System.err.println(regexQueryNrHits("^.*bug #+[0-9]+.*$",
new JavaUtilRegexCapabilities()));

终于成功了!:)

问题不在于数字。问题在于正则表达式查询和分析如何协同工作。您的正则表达式必须匹配一个术语,而不是整个字段。这就是为什么它可以与
一起使用,而不是与
一起使用,因为您将整个字段变成了一个术语。不过有一个警告。当您对这样的字段进行未分析时,您首先放弃了使用搜索索引的大部分优势(例如性能)。
    System.err.println(regexQueryNrHits("^.*bug #+[0-9]+.*$",
new JavaUtilRegexCapabilities()));