mel
mel

Reputation: 2790

Elasticsearch: Search in an array of JSONs

I'm using Elasticsearch with the python library and I have a problem using the search query when the object become a little bit complex. I have objects build like that in my index:

{
   "id" : 120,
   "name": bob,
   "shared_status": {
       "post_id": 123456789,
       "text": "This is a sample",
       "urls" : [
           {
              "url": "http://test.1.com",
              "displayed_url": "test.1.com" 
           },
           {
              "url": "http://blabla.com",
              "displayed_url": "blabla.com" 
           }
       ]
   }
}

Now I want to do a query that will return me this document only if in one of the displayed URL's a substring "test" and there is a field "text" in the main document. So I did this query:

{
   "query": {
       "bool": {
           "must": [
                    {"exists": {"field": "text"}}
                   ]
           }
        }
   }
}

But I don't know what query to add for the part: one of the displayed URL's a substring "test"

Is that posssible? How does the iteration on the list works?

Upvotes: 0

Views: 42

Answers (1)

Rahul
Rahul

Reputation: 16355

If you didn't define an explicit mapping for your schema, elasticsearch creates a default mapping based on the data input.

As you don't need any association between url and displayed_url, the current schema will work fine.

You can use a match query for full text match

GET _search
{
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "text"
          }
        },
        {
          "match": {
            "urls.displayed_url": "test"
          }
        }
      ]
    }
  }
}

Upvotes: 2

Related Questions