0

new to Elasticsearch. I see by default, ES creates indexes for every word in every field. But in my use-case, I will be searching only two fields, so am thinking to tell ES somehow that dont create index for everything in the document, but create indexes only the fields I specify in the mapping.

SO here is my mapping

PUT /benefits
  { 
     "mappings": {
       "benefit": {
         "_all": {"enabled": false},
          "properties": {
            "subCategory": {"type": "text"},
            "description": {"type": "text"}
         }  

       }
   }
}

and my document looks like this

{
"bpi" : "123456",
"category" : "CAT1",
"subCategory" : "Primary Care Physician Copay",
"planName" : "MyPlan HMO Individual",
"description" : "0 per office visit for Convenient Care & Minute Clinics Complete ABC Document",
"categoryId" : 200.0,
"desktop360Id" : 1001.0,
"createTimeStamp" : "2017-02-21T22:00:12.000Z"

}

So I will be searching only on "subCategory" and "description" (not on "planName" or any other fields text) and created the index/mapping as shown in mapping above, but I am still able to search text on other fields as well. for example if I search "MyPlan" from "planName" field, the search works and brings up this document. Does it mean it created index for all the words (except those 'a', 'of' types) on all the fields? I ask this question because, I don't want to spend the memory on the indexes that I don't need.

Any suggestions?

JBone
  • 1,724
  • 3
  • 20
  • 32
  • 1
    You just need to add `"dynamic": "false"` to your mapping (same level as "_all"). This will do it at object level, but you can also set it at index level or in templates to set it for all indices. Information about this available [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-mapping.html) – Olivier Apr 11 '17 at 20:47
  • Thank you Olivier. I think I got it. See the example that I am putting in 'Answer your Question' – JBone Apr 11 '17 at 21:07

1 Answers1

2

In order to exclude other fields from indexing you must specify them in mapping and set index property to no value. Here is an example

{
    "mappings": {
        "benefit": {
            "properties": {
                "subCategory": {
                    "type": "string"
                },
                "description": {
                    "type": "string"
                },
                "planName": {
                    "type": "string",
                    "index": "no"
                },
                "bpi": {
                    "type": "string",
                    "index": "no"
                },
                "category": {
                    "type": "string",
                    "index": "no"
                }
            }
        }
    }
}
Random
  • 3,807
  • 2
  • 30
  • 49
  • I have a follow up question. I know I should post it a different question but I have already typed up the context for the question here, let me ask you this question. How do I put more weightage to the search results found in 'subCategory' over 'description'? any suggestions? – JBone Apr 12 '17 at 12:46
  • I figured this one by adding "boost" values to those two fields while creating the index. Thanks – JBone Apr 12 '17 at 13:06