Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.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_Lucene - Fatal编程技术网

Java Lucene中数值范围查询与术语查询的结合

Java Lucene中数值范围查询与术语查询的结合,java,lucene,Java,Lucene,我想在Lucene中将数值范围查询与术语查询结合起来。例如,我想搜索我索引的文档,这些文档包含10到20页,标题为“Hello World” 它似乎不可能使用QueryParser为我生成这个查询;QueryParser生成的范围查询似乎是文本查询 我绝对希望能举一个如何将数值范围查询与术语查询相结合的例子。我也愿意选择搜索我的索引 谢谢 Lucene将数字视为单词,因此数字按字母顺序排列 1 12 123 1234 etc. 也就是说,您仍然可以使用范围查询,您只需要更加聪明 为了正确查询数

我想在Lucene中将数值范围查询与术语查询结合起来。例如,我想搜索我索引的文档,这些文档包含10到20页,标题为“Hello World”

它似乎不可能使用QueryParser为我生成这个查询;QueryParser生成的范围查询似乎是文本查询

我绝对希望能举一个如何将数值范围查询与术语查询相结合的例子。我也愿意选择搜索我的索引

谢谢

Lucene将数字视为单词,因此数字按字母顺序排列

1
12
123
1234
etc.
也就是说,您仍然可以使用范围查询,您只需要更加聪明

为了正确查询数值,需要填充整数,使其长度相同(无论支持的最大值是多少)

显然,这不适用于负数(因为-2<-1),希望您不必处理它们。下面是一篇有用的文章,如果你确实遇到了消极因素:

看来这是我自己想出来的。可以使用Query.combine()将一个或多个查询组合在一起。我在下面举了一个例子

String termQueryString = "title:\"hello world\"";
Query termQuery = parser.parse(termQueryString);

Query pageQueryRange = NumericRangeQuery.newIntRange("page_count", 10, 20, true, true);

Query query = termQuery.combine(new Query[]{termQuery, pageQueryRange});

您还可以创建一个自定义QueryParser重写
受保护查询getRangeQuery(…)
方法,当遇到
“page\u count”
字段时,该方法应返回
NumericRangeQuery
实例

就像这样

public class CustomQueryParser extends QueryParser {

    public CustomQueryParser(Version matchVersion, String f, Analyzer a) {
        super(matchVersion, f, a);
    }

    @Override
    protected Query getRangeQuery(final String field, final String part1, final String part2, final boolean inclusive) throws ParseException {

        if ("page_count".equals(field)) {
            return NumericRangeQuery.newIntRange(field, Integer.parseInt(part1), Integer.parseInt(part2), inclusive, inclusive);
        }

        // return default
        return super.getRangeQuery(field, part1, part2, inclusive);    
    }
}
...
final QueryParser parser = new CustomQueryParser(Version.LUCENE_35, "some_default_field", new StandardAnalyzer(Version.LUCENE_35));
final Query q = parser.parse("title:\"hello world\" AND page_count:[10 TO 20]");
...
然后在解析文本查询时使用
CustomQueryParser

就像这样

public class CustomQueryParser extends QueryParser {

    public CustomQueryParser(Version matchVersion, String f, Analyzer a) {
        super(matchVersion, f, a);
    }

    @Override
    protected Query getRangeQuery(final String field, final String part1, final String part2, final boolean inclusive) throws ParseException {

        if ("page_count".equals(field)) {
            return NumericRangeQuery.newIntRange(field, Integer.parseInt(part1), Integer.parseInt(part2), inclusive, inclusive);
        }

        // return default
        return super.getRangeQuery(field, part1, part2, inclusive);    
    }
}
...
final QueryParser parser = new CustomQueryParser(Version.LUCENE_35, "some_default_field", new StandardAnalyzer(Version.LUCENE_35));
final Query q = parser.parse("title:\"hello world\" AND page_count:[10 TO 20]");
...


当然,所有这些都假设在将
页面计数
值添加到文档时使用了
数值字段(…).setIntValue(…)
,您可以使用
布尔查询

var combinedQuery = new BooleanQuery();
combinedQuery.Add(new TermQuery(new Term("title","hello world")),Occur.MUST);
combinedQuery.Add(NumericRangeQuery.newIntRange("page_count", 10, 20, true, true),Occur.MUST);

luceneapi Javadoc中也建议这样做。谢谢你知道如何在Lucene4上实现它吗?看起来,Query.combine()在Lucene4Hey@Dewsworld中不起作用,解决方案是使用布尔查询:BooleanQuery Query=new BooleanQuery();Add(newtermquery(…),BooleanClause.occurrent.MUST);Add(newterm.query(…),BooleanClause.occurrent.NEVER);