2

I'm relatively new to ElasticSearch and I need some advice. After several tries, i did not found the solution and that's why I need you.

I want to make a conditional query based on the document content.

Let me explain, I have those documents in ES:

{
   "name": "Product n°1",
   "type": "Mail",
   "sub": "Letter"
},
{
   "name": "Product n°2",
   "type": "Video",
   "sub": null
},
{
   "name": "Product n°3",
   "type": "Mail",
   "sub": "Postcard"
}

The user can filter by types and sub with checkboxes (so, the user can search more than one type and sub at the same time)

Edited

Customers can selects with checkbox the types of the products they want to get from ES (For ex: Video, Image, Mail), they can select all of them.

The "Document" type has 4 sub-types: Letter, Postal Card, Paper, Printed and they can select which sub type they want to retrieve too.

So, we admit a customer selected Video and Mail, and Letter as sub-type of Mail.

I want to ES returns all of the documents within the selected types AND just the Letters when the document is a "Mail" type.

Sorry for my mistakes, I'm very new to ES.

Thanks to all of you trying to help !


UPDATE 2

Here's my query with Andrei Stefan's solution

{
  "query": {
    "filtered": {
      "query": {
        "match_all": []
      },
      "filter": {
        "bool": {
          "should": [
            {
              "terms": {
                "type": [
                  "Video",
                  "Mail"
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "type": Mail
                    }
                  },
                  {
                    "terms": {
                      "sub": [
                        "Letters"
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

ES returns all of documents of type Video and Mail, it does not apply the fillter Letters when the type is Mail.

11
  • Can you rephrase or provide more details on "I want to make a conditional query like that : I want to retrieve the products having 1, or 2 in type but if the document has a type 1 let I want to retrieve only the documents having 2 in sub."? Commented May 12, 2015 at 8:42
  • I sure can. Sorry for my english by the way. Not my native language. Commented May 12, 2015 at 8:43
  • Ok, I tried to make myself clear, I hope you'll understand. Commented May 12, 2015 at 9:18
  • 4
    I would try this: { "query": { "bool": { "should": [ { "terms": { // ['type' => $types] } }, { "bool": { "must": [ { "term": { // 'type' => 2 } }, { "terms": { // 'sub' => [1] } } ] } } ] } } } Commented May 12, 2015 at 9:22
  • I don't know what you used to write that pseudo-query, so I gave you the JSON representation of what I think it would work. Commented May 12, 2015 at 9:23

1 Answer 1

1

A thousand tanks to @Andrei Stefan. The difference between update 2 is I had to delete "Mail" from the first "should" query.

{
  "query": {
    "filtered": {
      "query": {
        "match_all": []
      },
      "filter": {
        "bool": {
          "should": [
            {
              "terms": {
                "type": [
                  "Video"
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "type": Mail
                    }
                  },
                  {
                    "terms": {
                      "sub": [
                        "Letters"
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}
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.