0

I would like to find all objects which have a year value which starts with 20... so I would like to search for 20*, where * is a wild card. I tries something like

'match_phrase_prefix': { 'year': { 'query': '20*', 'max_expansions': 50}}

but I guess this only works for strings. How would I do this for integers?

EDIT: I found a solution... its not pretty but it works

year_query = '20'
if len(str(year_query)) < 4:
    try:
        low_year, high_year = extend_year(int(year_query))
        filter_list.append({"range": {"year": {"gte": low_year, "lte": high_year}}})
    except ValueError:
        print "Not a valid input for year"
        pass
else:
    for year in year_query.split(','):
        if '-' in year:
            year_range = year.split('-')
            high_year = year_range[1].strip()
            if len(high_year) < 4:
                low_year, high_year = extend_year(high_year)
                try:
                    filter_list.append({"range" : {"year" : {"gte" : int(year_range[0].strip()),"lte" : int(high_year),"format": "yyyy"}}})
                except ValueError:
                    print "Not a valid input for year"
                    pass
            else:
                try:
                    filter_list.append({ "match": {"year": int(year.strip()) }})
                except ValueError:
                    print "Not a valid input for year"
                    pass

with the function

def extend_year(input_year):
    length = len(str(input_year))
    if length == 4:
        return input_year, 0
    elif length == 3:
        return input_year*10, int(str(input_year) + '9')
    elif length == 2:
        return input_year*100, int(str(input_year) + '99')
    elif length == 1:
        return input_year*1000, int(str(input_year) + '999')
    elif length == 0:
        return 0, 9999
    else:
        return input_year, 0

if anybody can come up with a better solution please let me know

1
  • 1
    If your field is a year then I assume that you want to find all years greater than 2000. So maybe you can use a range query? Commented Jun 6, 2016 at 17:32

1 Answer 1

3

This should work

'range': { 'year': { 'gte': 2000, 'max_expansions': 50}}
Sign up to request clarification or add additional context in comments.

2 Comments

if somebody enters 19 I don't want it to match papers which start with 20... I implemented now a range query which is a bit convoluted... but works
It's a numerical range, so it should only match numbers larger than 2000. You could a "lt" for 2100 then. Although I'm confused as to what you really want.

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.