• <i id='lxlzd'><tr id='lxlzd'><dt id='lxlzd'><q id='lxlzd'><span id='lxlzd'><b id='lxlzd'><form id='lxlzd'><ins id='lxlzd'></ins><ul id='lxlzd'></ul><sub id='lxlzd'></sub></form><legend id='lxlzd'></legend><bdo id='lxlzd'><pre id='lxlzd'><center id='lxlzd'></center></pre></bdo></b><th id='lxlzd'></th></span></q></dt></tr></i><div id='lxlzd'><tfoot id='lxlzd'></tfoot><dl id='lxlzd'><fieldset id='lxlzd'></fieldset></dl></div>
    • <bdo id='lxlzd'></bdo><ul id='lxlzd'></ul>
    1. <small id='lxlzd'></small><noframes id='lxlzd'>

        <legend id='lxlzd'><style id='lxlzd'><dir id='lxlzd'><q id='lxlzd'></q></dir></style></legend>
        <tfoot id='lxlzd'></tfoot>

        如何让 Lucene 中的 QueryParser 处理数字范围?

        时间:2023-06-29

        • <legend id='3TNFx'><style id='3TNFx'><dir id='3TNFx'><q id='3TNFx'></q></dir></style></legend>
            • <bdo id='3TNFx'></bdo><ul id='3TNFx'></ul>
            • <tfoot id='3TNFx'></tfoot>

                  <small id='3TNFx'></small><noframes id='3TNFx'>

                    <tbody id='3TNFx'></tbody>
                  <i id='3TNFx'><tr id='3TNFx'><dt id='3TNFx'><q id='3TNFx'><span id='3TNFx'><b id='3TNFx'><form id='3TNFx'><ins id='3TNFx'></ins><ul id='3TNFx'></ul><sub id='3TNFx'></sub></form><legend id='3TNFx'></legend><bdo id='3TNFx'><pre id='3TNFx'><center id='3TNFx'></center></pre></bdo></b><th id='3TNFx'></th></span></q></dt></tr></i><div id='3TNFx'><tfoot id='3TNFx'></tfoot><dl id='3TNFx'><fieldset id='3TNFx'></fieldset></dl></div>

                  本文介绍了如何让 Lucene 中的 QueryParser 处理数字范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  new QueryParser(.... ).parse (somequery);
                  

                  它仅适用于字符串索引字段.假设我有一个名为 count 的字段,其中 count 是一个整数字段(在索引我认为数据类型的字段时)

                  it works only for string indexed fields. Say i have a field called count where count is a integer field (while indexing the field I considered the data type)

                  new QueryParser(....).parse("count:[1 TO 10]");
                  

                  上面的一个是行不通的.相反,如果我使用 "NumericRangeQuery.newIntRange" 这是有效的.但是,我只需要上面那个...

                  The above one is not works. Instead If i used "NumericRangeQuery.newIntRange" which is working. But, i need the above one only...

                  推荐答案

                  遇到同样的问题并解决了,在这里分享一下我的解决方案:

                  Had the same issue and solved it, so here I share my solution:

                  要创建一个自定义查询解析器,该解析器将解析以下查询INTFIELD_NAME:1203"或INTFIELD_NAME:[1 TO 10]"并将字段 INFIELD_NAME 作为 Intfield 处理,我用以下内容覆盖了 newTermQuery:

                  To create a custom query parser that will parse the following query "INTFIELD_NAME:1203" or "INTFIELD_NAME:[1 TO 10]" and handle the field INTFIELD_NAME as an Intfield, I overrided newTermQuery with the following:

                  public class CustomQueryParser extends QueryParser {
                  
                  public CustomQueryParser(String f, Analyzer a) {
                      super(f, a);
                  }
                  
                  protected Query newRangeQuery(String field, String part1, String part2, boolean startInclusive,
                      boolean endInclusive) {
                  
                      if (INTFIELD_NAME.equals(field)) {
                      return NumericRangeQuery.newIntRange(field, Integer.parseInt(part1), Integer.parseInt(part2),
                          startInclusive, endInclusive);
                      }
                      return (TermRangeQuery) super.newRangeQuery(field, part1, part2, startInclusive, endInclusive);
                  }
                  
                  
                  protected Query newTermQuery(Term term) {
                      if (INTFIELD_NAME.equals(term.field())) {
                  
                      BytesRefBuilder byteRefBuilder = new BytesRefBuilder();
                      NumericUtils.intToPrefixCoded(Integer.parseInt(term.text()), 0, byteRefBuilder);
                      TermQuery tq = new TermQuery(new Term(term.field(), byteRefBuilder.get()));
                  
                      return tq;
                      } 
                      return super.newTermQuery(term);
                  
                  }
                  }
                  

                  我从 http://www.mail-archive.com/search?l=java-user@lucene.apache.org&q=subject:%22Re%3A+How+do+you+properly+use+NumericField%22&o=newest&f=1 并做了3处修改:

                  I took the code quoted in that thread from http://www.mail-archive.com/search?l=java-user@lucene.apache.org&q=subject:%22Re%3A+How+do+you+properly+use+NumericField%22&o=newest&f=1 and made 3 modifications :

                  • 把 newRangeQuery 改写得更好一点

                  • rewrote newRangeQuery a little more nicely

                  替换为 newTermQuery 方法NumericUtils.intToPrefixCoded(Integer.parseInt(term.text()),NumericUtils.PRECISION_STEP_DEFAULT)));

                  replaced in newTermQuery method NumericUtils.intToPrefixCoded(Integer.parseInt(term.text()),NumericUtils.PRECISION_STEP_DEFAULT)));

                  by NumericUtils.intToPrefixCoded(Integer.parseInt(term.text()), 0, byteRefBuilder);

                  当我第一次在同一数值字段的过滤器中使用此方法时,我将 0 作为我发现它作为 lucene 类中的默认值并且它正常工作.

                  when I used this method for the first time in a filter on the same numeric field, I put 0 as I found it as a default value in a lucene class and it just worked.

                  • 替换为 newTermQuery

                  • replaced on newTermQuery

                  TermQuery tq = new TermQuery(new Term(field,

                  by TermQuery tq = new TermQuery(new Term(term.field(),

                  使用字段"是错误的,因为如果您的查询有多个子句(FIELD:text OR INFIELD:100),它会占用第一个或前一个子句字段.

                  using "field" is wrong, because if your query has several clauses (FIELD:text OR INTFIELD:100), it is taking the first or previous clause field.

                  这篇关于如何让 Lucene 中的 QueryParser 处理数字范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Java中数据规范化的拼写更正 下一篇:如何使用 Lucene 获取频繁出现的短语

                  相关文章

                    <bdo id='6iwUA'></bdo><ul id='6iwUA'></ul>

                    <tfoot id='6iwUA'></tfoot>

                    <small id='6iwUA'></small><noframes id='6iwUA'>

                    <i id='6iwUA'><tr id='6iwUA'><dt id='6iwUA'><q id='6iwUA'><span id='6iwUA'><b id='6iwUA'><form id='6iwUA'><ins id='6iwUA'></ins><ul id='6iwUA'></ul><sub id='6iwUA'></sub></form><legend id='6iwUA'></legend><bdo id='6iwUA'><pre id='6iwUA'><center id='6iwUA'></center></pre></bdo></b><th id='6iwUA'></th></span></q></dt></tr></i><div id='6iwUA'><tfoot id='6iwUA'></tfoot><dl id='6iwUA'><fieldset id='6iwUA'></fieldset></dl></div>

                    1. <legend id='6iwUA'><style id='6iwUA'><dir id='6iwUA'><q id='6iwUA'></q></dir></style></legend>