Lucene LongPoint Range搜索不起作用 [英] Lucene LongPoint Range search doesn't work

查看:94
本文介绍了Lucene LongPoint Range搜索不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Java 11中使用Lucene 8.2.0.

I am using Lucene 8.2.0 in Java 11.

我正在尝试为Long值建立索引,以便可以使用范围查询对它进行过滤,例如:+my_range_field:[1 TO 200].但是,在此最小示例中,即使是my_range_field:[* TO *]的任何变体都返回0.一旦从中删除+使其成为OR,我就会得到2结果.

I am trying to index a Long value so that I can filter by it using a range query, for example like so: +my_range_field:[1 TO 200]. However, any variant of that, even my_range_field:[* TO *], returns 0 results in this minimal example. As soon as I remove the + from it to make it an OR, I get 2 results.

所以我认为我在索引方式上必须犯错误,但是我无法弄清楚它可能是什么.

So I am thinking I must make a mistake in how I index it, but I can't make out what it might be.

LongPoint JavaDoc :

用于快速范围过滤器的索引长字段.如果还需要存储该值,则应添加一个单独的StoredField实例. 在搜索时查找N维形状或范围内的所有文档非常有效.允许在一个文档中的同一字段使用多个值.

An indexed long field for fast range filters. If you also need to store the value, you should add a separate StoredField instance. Finding all documents within an N-dimensional shape or range at search time is efficient. Multiple values for the same field in one document is allowed.

这是我的最小示例:

public static void main(String[] args) {
    Directory index = new RAMDirectory();
    StandardAnalyzer analyzer = new StandardAnalyzer();

    try {
        IndexWriter indexWriter = new IndexWriter(index, new IndexWriterConfig(analyzer));

        Document document1= new Document();
        Document document2= new Document();

        document1.add(new LongPoint("my_range_field", 10));
        document1.add(new StoredField("my_range_field", 10));
        document2.add(new LongPoint("my_range_field", 100));
        document2.add(new StoredField("my_range_field", 100));

        document1.add(new TextField("my_text_field", "test content 1", Field.Store.YES));
        document2.add(new TextField("my_text_field", "test content 2", Field.Store.YES));

        indexWriter.deleteAll();
        indexWriter.commit();
        indexWriter.addDocument(document1);
        indexWriter.addDocument(document2);
        indexWriter.commit();
        indexWriter.close();

        QueryParser parser = new QueryParser("text", analyzer);
        IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(index));

        String luceneQuery = "+my_text_field:test* +my_range_field:[1 TO 200]";
        Query query = parser.parse(luceneQuery);

        System.out.println(indexSearcher.search(query, 10).totalHits.value);
    } catch (IOException e) {

    } catch (ParseException e) {

    }
}

推荐答案

我找到了解决问题的方法.

I found the solution to my problem.

我的印象是查询解析器可以正确解析任何查询字符串.似乎并非如此.

I was under the impression that the query parser could just parse any query string correctly. That doesn't seem to be the case.

使用

Query rangeQuery = LongPoint.newRangeQuery("my_range_field", 1L, 11L);
Query searchQuery = new WildcardQuery(new Term("my_text_field", "test*"));
Query build = new BooleanQuery.Builder()
    .add(searchQuery, BooleanClause.Occur.MUST)
    .add(rangeQuery, BooleanClause.Occur.MUST)
    .build();

返回正确的结果.

这篇关于Lucene LongPoint Range搜索不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆