0

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?

4
  • It is possible. Please provide output of _mapping : elastic.co/guide/en/elasticsearch/reference/current/… Commented Mar 19, 2017 at 15:54
  • The question is about query elasticsearch not iterate on a JSON Commented Mar 19, 2017 at 16:00
  • @rahulroc I simplified the situation using a fake example but my application is about tweets from the API twitter. And there is no mapping defined for those fields in my mapping. Commented Mar 19, 2017 at 16:03
  • @mel As you didn't provide explicit mapping, ES assumes some mapping types based on input data, more details in the answer Commented Mar 19, 2017 at 16:24

1 Answer 1

2

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"
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.