1

I am trying to retrieve the mentions of years between 1933 and 1949 from a string field called text. However, I cannot seem to find the working range query for that. What I tried to so far crashes:

   {"query":
        {"query_string":
              {
                "text": [1933 TO 1949]
               }
         }
    }

I have also tried it like this:

   {"query":
         {"filtered":
               {"query":{"match_all":{}},
                 "filter":{"range":{"text":[1933 TO 1949]}
                }
         }
    }

but it still crashes.

A sample text field looks like the one below, containing a mention of the year 1933:

"Primera División 1933 (Argentinië), seizoen in de Argentijnse voetbalcompetitie\n* Primera Divisió n 1933 (Chili), seizoen in de Chileense voetbalcompetitie\n* Primera División 1933 (Uruguay), seizoen in de Uruguayaanse voetbalcompetitie\n \n "

However, I also have documents not containing any years inside, and I would like to filter all the documents to preserve only the ones mentioning years in a given period. I read here http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html that the range query can be applied to text fields as well, and I don't want to use any intermediate solution to identify dates inside texts.

What I basically want to achieve is to be able to get the same results as when using a search URI query:

     urltomyindex/_search?q=text:%7B1933%20TO%201949%7D%27

which works perfectly. Is it still possible to achieve my goal? Any help much appreciated!

Crista23
  • 3,203
  • 9
  • 47
  • 60
  • Can you paste a sample document ? I dont understand the nature of text field.. – Vineeth Mohan Dec 30 '14 at 15:46
  • Yes, I have just edited the question. – Crista23 Dec 30 '14 at 15:49
  • 1
    You cant do a search in this fashion. Before you do an insert of document , you need to identify and parse out the year as 1933 and place it as a seprate number field called , say yearOfRelevance. After that you can do a range query on this field – Vineeth Mohan Dec 30 '14 at 15:51

1 Answers1

0

This should do it:

GET index1/type1/_search
{
  "query": {
    "filtered": {
      "filter": {
        "terms": {
          "fieldNameHere": [
            "1933",
            "1934",
            "1935",
            "1936",
            "1937",
            "1938",
            "1939",
            "1940",
            "1941",
            "1942",
            "1943",
            "1944",
            "1945",
            "1946",
            "1947",
            "1948",
            "1949"
          ]
        }
      }
    }
  }
}

If you know you're going to be needing this kind of search frequently it would be much better to create a new field "yearPublished" or something like that so you can search it as a number vs a text field.

jhilden
  • 12,207
  • 5
  • 53
  • 76