0

I'm trying to index a nested field as a first-class attribute in my document so that I can search them using query_string without dot syntax. For example, if I have a document like "data": { "name": "Bob" } instead of searching for data.name:Bob I would like to be able to search for name:Bob

The root of my issue is that we index a jsonb column that may have varying attributes. In some instances the data property may contain a data.business attribute, etc. I would like users to be able to search on these attributes without needing to "dig" into the object.

The data field does not have to be indexed as a nested type unless necessary; I was indexing it as an object previously.

I have tried to leverage the _all field as suggested in this post.

I have also tried to use include_in_parent:true and set the datatype as nested for my data field as suggested in this post.

I have also looked into the inner_hits feature to no avail.

Here's an example of my mapping for the data attribute.

PUT my_index

{
  "mappings": {
    "my_type": {
      "properties": {
        "data": {
          "type": "object"
        }
      }
    }
  }
}

Example document

PUT my_index/_doc/1

{
  "data": {
    name: "bob",
    business: "None of yours"
  }
}

And how my query currently looks:

GET my_index/_search

{
  "query": {
    "query_string": {
      "query": "name:bob",
      "fields": ["data.*"]
    }
  }
}

With the current setup I almost get my desired results. I can search on individual properties like data.name:bob and data.business:"None of yours" and get back the correct documents. However I want to be able to get the exact same results with business:"None of yours" or name:bob.

Thanks in advance for any help!

1 Answer 1

0

I figured it out using dynamic templates. For anyone coming across this in the future, here is how I solved the issue: I used path_match to match the data object (data.*). Then using copy_to and {name} I dynamically created top-level fields on my parent object.

{
  "dynamic_templates":[
    {"template_1":
       {"mapping":
          {"copy_to":"{name}"},
        "path_match":"data.*"
       }
     }
   ]
}
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.