3

Is it possible to query Elasticsearch giving it a range of strings?

Something that I'd imagine like:

Sample Mapping:

     {
      "resource" : {
        "properties" : {
          "Title" : {
            "type" : "string"
          },
          "type_" : {
            "index" : "not_analyzed",
            "type" : "string"
          },
           "Summary" : {
            "format" : "dateOptionalTime",
            "type" : "date"
          }
        }
      }
    }

Sample Query:

{
  "size" : 10,
  "query" : {
    "filtered" : {
      "query" : {
        "bool" : {
          "should" : [ {
            "text" : {
              "Title" : {
                "query" : "AAA",
                "type" : "phrase_prefix"
              }
            }
          }, {
            "range" : {
              "Title" : {
                "from" : "BBB",
                "to" : "CCC",
                "include_lower" : true,
                "include_upper" : true
              }
            }
          } ],
          "minimum_number_should_match" : 1
        }
      },
      "filter" : {
        "and" : {
          "filters" : [{
            "or" : {
              "filters" : [ {
                "term" : {
                  "type_" : "personType"
                }
              } ]
            }
          } ]
        }
      }
    }
  }
}

Data Indexed: Resources with Titles 'AAA', 'BBB', 'CCC', 'DDD'

Result Resource with Title 'AAA' (range didn't select 'BBB' and 'CCC')

Any help appreciated

2 Answers 2

2

Yes, it is possible. The RangeQuery provided by the elasticsearch query DSL internally uses the lucene TermRangeQuery for field of type string. On the other hand, it uses the lucene NumericRangeQuery for field of type number/date.

Sign up to request clarification or add additional context in comments.

4 Comments

Well theres something wrong with my ES, because if I index some resorurces with "name" being dates, then a range query works. If, on the other hand, the "name" are just strings, then the range does not work, always 0 results. How come?
It probably depends on the way you are indexing data. Have you tried lowercasing the terms in your query?
well, I have a very basic index mapping because the resources I am indexing have very varied information/fields inside e.g. some have 'name', other have 'title' etc. So I leave it to ES to figure things out and it worked perfectly. But I cant make a range search with strings, even when I have indexed only the names "a","b", "c" and I query with a rage <"a","c"> - no results
Weird, I just tried what you described and it worked. I'd suggest to check again your data, maybe using the analyze API to see what you're actually indexing.
1

By default ElasticSearch keeps all indexed data lowercase. It does not lowercase the queries, though. Great.

To wrap up: after sending the queries (from and to values) all in lowercase the range searching works like a charm.

1 Comment

I tried to tell you it was about lowercasing ;) Some queries are actually analyzed, which means that the query goes into the same analysis process as the document field. The Range Query is not one of them. You are relying on the default mapping (which uses the lucene StandardAnalyzer underneath) for the title field, but the query is used as it is, so lowercasing it manually is a good choice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.