0

Using the following document, I'm trying to perform an Elasticsearch keyword query, conditionally excluding field data from the scope of the search. Is this possible?

{
  "Name":"doc1",
  "UserData":[
      {
         "EnteredBy":"Eric",
         "Description":"Desc entered by Eric, abc"
      },
      {
         "EnteredBy":"Alex",
         "Description":"Desc entered by Alex, def"
      }
   ]
}

The Elasticsearch query I need will allow me to search across the whole document, except it should exclude from the search UserData items where EnteredBy does not match the specified user.

The following queries would return results:

User:Eric doc1
User:Eric abc
User:Alex doc1
User:Fred doc1

The following queries would not return results:

User:Eric def
User:Fred def

Everything I've tried thus far, ends up filtering content based on the presence of UserData nodes which apply to the specified user. I can't think of a way to specify that a field should be searched, only if the EnteredBy field matches.

I could restructure the document, if that would solve the problem.

Edit 1

The index..

PUT index1
{
  "settings": {
    "number_of_shards": 2,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties" : {
      "UserData" : {
        "type":"nested"
      },
      "Name": {
        "type":"text"
      }
    }
  }
}

Edit 2

The query below is providing the results that I need, except for the child entity, I have to search in a specific field. If I change the second condition of the nested search into a query_string search, then it no longer uses the EnteredBy condition.

GET index1/_search
{
 "query": {
      "bool": { 
        "should": [
        {
          "nested": 
          {
               "path": "UserData",
               "query": {
                  "bool": {
                    "must": [{
                      "match": {
                        "UserData.EnteredBy": "Eric"
                      }},
                      {
                        "match": {
                        "UserData.Description": "def"
                      }
                    }]

                  }
                }
          }
        },
        { 
          "query_string": 
          { 
            "query": "doc1x" 
          }

        }
      ]
     }
  }
}

1 Answer 1

1

This query appears to be working. I think I answered my own question.

GET index1/_search
{
 "query": {
      "bool": { 
        "should": [
        {
          "nested": 
          {
               "path": "UserData",
               "query": {
                  "bool": {
                    "must": [{
                      "match": {
                        "UserData.EnteredBy": "Eric"
                      }},
                      {
                        "query_string": {
                        "query": "def"
                      }
                    }]

                  }
                }
          }
        },
        { 
          "query_string": 
          { 
            "query": "doc1" 
          }

        }
      ]
     }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

If you found a solution for your issue, please consider accepting your own answer :)
Thanks @KevinQuinzel, will do.. after the SF waiting period of 2 days.

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.