FAQ
Hello all,

Does anyone know if it is possible in Lucene to do a query based on the
string length of the value of a field?

For example, if I wanted all index matches where a specific field like
'first_name' was between 10 and 20 characters.

Thanks!
-Camden Daily

Search Discussions

  • Ian Lea at Jan 21, 2011 at 3:22 pm
    Not directly, but you could index a NumericField called "length" and
    do a NumericRangeQuery on it.

    Or loop through all the terms checking length. But that isn't a query
    and will be slow.


    --
    Ian.

    On Fri, Jan 21, 2011 at 3:15 PM, Camden Daily wrote:
    Hello all,

    Does anyone know if it is possible in Lucene to do a query based on the
    string length of the value of a field?

    For example, if I wanted all index matches where a specific field like
    'first_name' was between 10 and 20 characters.

    Thanks!
    -Camden Daily
    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • Jack Krupansky at Jan 21, 2011 at 3:27 pm
    A wildcard query with 10 leading question marks, each of which requires a
    single character. This would also depend on leading wildcards being enabled
    in your query parser (if you are using one.)

    first_name:??????????*

    The performance would not necessarily be great, but functionally it would do
    exactly what you asked.

    -- Jack Krupansky

    -----Original Message-----
    From: Camden Daily
    Sent: Friday, January 21, 2011 10:15 AM
    To: java-user@lucene.apache.org
    Subject: Performing a query on token length

    Hello all,

    Does anyone know if it is possible in Lucene to do a query based on the
    string length of the value of a field?

    For example, if I wanted all index matches where a specific field like
    'first_name' was between 10 and 20 characters.

    Thanks!
    -Camden Daily


    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • Jack Krupansky at Jan 21, 2011 at 3:31 pm
    Oops... I only solved half the problem, the other half was to limit length
    to 20, which would be done with a negated leading wildcard of 21 question
    marks:

    first_name:??????????* -first_name:?????????????????????*

    -- Jack Krupansky

    -----Original Message-----
    From: Jack Krupansky
    Sent: Friday, January 21, 2011 10:26 AM
    To: java-user@lucene.apache.org
    Subject: Re: Performing a query on token length

    A wildcard query with 10 leading question marks, each of which requires a
    single character. This would also depend on leading wildcards being enabled
    in your query parser (if you are using one.)

    first_name:??????????*

    The performance would not necessarily be great, but functionally it would do
    exactly what you asked.

    -- Jack Krupansky

    -----Original Message-----
    From: Camden Daily
    Sent: Friday, January 21, 2011 10:15 AM
    To: java-user@lucene.apache.org
    Subject: Performing a query on token length

    Hello all,

    Does anyone know if it is possible in Lucene to do a query based on the
    string length of the value of a field?

    For example, if I wanted all index matches where a specific field like
    'first_name' was between 10 and 20 characters.

    Thanks!
    -Camden Daily


    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org


    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • Ian Lea at Jan 21, 2011 at 3:36 pm
    Wouldn't that also match names with length > 20?


    --
    Ian.


    On Fri, Jan 21, 2011 at 3:26 PM, Jack Krupansky
    wrote:
    A wildcard query with 10 leading question marks, each of which requires a
    single character. This would also depend on leading wildcards being enabled
    in your query parser (if you are using one.)

    first_name:??????????*

    The performance would not necessarily be great, but functionally it would do
    exactly what you asked.

    -- Jack Krupansky

    -----Original Message----- From: Camden Daily
    Sent: Friday, January 21, 2011 10:15 AM
    To: java-user@lucene.apache.org
    Subject: Performing a query on token length

    Hello all,

    Does anyone know if it is possible in Lucene to do a query based on the
    string length of the value of a field?

    For example, if I wanted all index matches where a specific field like
    'first_name' was between 10 and 20 characters.

    Thanks!
    -Camden Daily

    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • Camden Daily at Jan 21, 2011 at 5:29 pm
    Thank you Ian and Jack,

    I believe I'll go with simply creating a NumericField for the length, as
    that will result in the best performance.

    -Camden
    On Fri, Jan 21, 2011 at 10:35 AM, Ian Lea wrote:

    Wouldn't that also match names with length > 20?


    --
    Ian.


    On Fri, Jan 21, 2011 at 3:26 PM, Jack Krupansky
    wrote:
    A wildcard query with 10 leading question marks, each of which requires a
    single character. This would also depend on leading wildcards being enabled
    in your query parser (if you are using one.)

    first_name:??????????*

    The performance would not necessarily be great, but functionally it would do
    exactly what you asked.

    -- Jack Krupansky

    -----Original Message----- From: Camden Daily
    Sent: Friday, January 21, 2011 10:15 AM
    To: java-user@lucene.apache.org
    Subject: Performing a query on token length

    Hello all,

    Does anyone know if it is possible in Lucene to do a query based on the
    string length of the value of a field?

    For example, if I wanted all index matches where a specific field like
    'first_name' was between 10 and 20 characters.

    Thanks!
    -Camden Daily

    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupjava-user @
categorieslucene
postedJan 21, '11 at 3:16p
activeJan 21, '11 at 5:29p
posts6
users3
websitelucene.apache.org

People

Translate

site design / logo © 2022 Grokbase