1

Imagine i have some document that has relations to other type of document, say, building that is related to regions:

building:
  country: region
  city: region
  nearbyParks: [region]
  closestNuclearHideout: region

I need to be able to perform a search by any region that is related to building, so i'm converting this structure to:

building:
  countryId: <uuid>
  cityId: <uuid>
  nearbyParkIds: [<uuid>]
  closestNuclearHideoutId: <uuid>
  regions: [region]

However, by this very moment i need to perform searches only by related region id, and would love to forbid ElasticSearch to index every regions.* field (except for regions.id) to keep things clean. Is this possible using current mapping API? I would love to keep my elastic documents as small as possible and also keep them in sync with backend output, so adding regionIds uuid array field is an option, but an unwanted one.

6
  • If you don't need the info about regions, you can store only region id in ElasticSearch and retrieve the rest of the info from your other storage (database or whatever). Commented Dec 30, 2015 at 14:45
  • Can you share some more info, i.e. what client technology are you using? How are you creating your index and mapping types? Commented Dec 30, 2015 at 15:40
  • @Val i'm using official java client with standard zero-downtime aliasing concept, mappings are created with index. Commented Dec 30, 2015 at 15:53
  • @Ashalynd yes, that's the regionIds field solution. I'm trying to avoid it so backend (database) and elastic output will be identical for client. Commented Dec 30, 2015 at 15:54
  • Then can you show how you're creating your index and the mapping you're using? Commented Dec 30, 2015 at 15:59

1 Answer 1

2

Yes, you can turn off dynamic mappings for a given field. In the mappings, you can therefore define the fields you do want to map, while effectively disabling the rest for search.

{
  "mappings": {
    "type": {
      "properties": {
        "regions": {
          "type": "object",
          "dynamic": false,
          "properties": {
            "id": {
              "type": "string",
              "index": "not_analyzed",
              "doc_values" : true
            }
          }
        }
      }
    }
  }
}

So, imagining that you indexed this document:

{
  "regions" : [
    {
      "id" : "xyzabc",
      "field1" : "ignored",
      "field2" : "ignored"
    },
    {
      "id" : "abcdef",
      "field1" : "ignored",
      "field2" : "ignored",
      "field3" : "ignored"
    }
  ]
}

The non-id fields would be ignored by the mapping and they would therefore not be searchable (without a script), but you would get them back as _source results in your hits.

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

1 Comment

Fantastic, i can't imagine how did i miss this one. Lots of thanks!

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.