<p dir="auto">Continuing conversation started here: <a href="https://discuss.elastic.co/t/coerce-object-to-string/56748/3" rel="nofollow">https://discuss.elastic.co/t/coerce-object-to-string/56748/3</a></p>
<p dir="auto"><strong>Description of the problem including expected versus actual behavior</strong>:</p>
<p dir="auto">Would like to be able to treat an object field as String given the following mapping:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="          &quot;properties&quot;: {
            &quot;severity&quot;: {
              &quot;index&quot;: &quot;not_analyzed&quot;,
              &quot;type&quot;: &quot;string&quot;
            },
            &quot;metadata&quot;: {
              &quot;type&quot;: &quot;string&quot;
              &quot;coerce&quot;: true
            }
          }"><pre class="notranslate"><code class="notranslate">          "properties": {
            "severity": {
              "index": "not_analyzed",
              "type": "string"
            },
            "metadata": {
              "type": "string"
              "coerce": true
            }
          }
</code></pre></div>
<p dir="auto">Indexing the following document:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
    &quot;severity&quot;: &quot;ERROR&quot;,
    &quot;metadata&quot;: {
        &quot;someKey&quot;: &quot;someValue&quot;,
        &quot;someKey&quot;: {
            &quot;some nested key&quot;: &quot;some nested value&quot;
        } 
    }
}"><pre class="notranslate"><code class="notranslate">{
    "severity": "ERROR",
    "metadata": {
        "someKey": "someValue",
        "someKey": {
            "some nested key": "some nested value"
        } 
    }
}
</code></pre></div>
<p dir="auto">should store the document under <code class="notranslate">metadata</code> as a searchable String.</p>
<p dir="auto">Right now, it is not possible to coerce anything to a String.</p>
<p dir="auto"><strong>Describe the feature</strong>:<br>
There are cases where complex objects need to be passed to tokenizers.  I am writing a new feature that will allow a mapping to specify that a property of type string may coerce an object within that property to a string.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="PUT /test
{
  &quot;mappings&quot;: {
     &quot;test&quot;: {
        &quot;properties&quot;: {
           &quot;a&quot;: {
              &quot;type&quot;: &quot;string&quot;,
              &quot;coerce&quot;: true
           }
        }
     }
  }
}"><pre class="notranslate"><code class="notranslate">PUT /test
{
  "mappings": {
     "test": {
        "properties": {
           "a": {
              "type": "string",
              "coerce": true
           }
        }
     }
  }
}
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="PUT /test/test/1
{
    &quot;a&quot;: {
        &quot;b&quot;:&quot;2&quot;,
        &quot;c&quot;:&quot;3&quot;
    }
}"><pre class="notranslate"><code class="notranslate">PUT /test/test/1
{
    "a": {
        "b":"2",
        "c":"3"
    }
}
</code></pre></div>
<p dir="auto">In this example, the tokenizer will be passed "{"b":"2","c":"3"}".  It will be up to the tokenizer to decide how to treat that.  The default tokenizer returns the tokens "b", "2", "c" and "3".</p>
<p dir="auto">This situation occurs frequently in library data.  Complex objects have been created and the values of some of the properties effect how the other properties are treated.  A simple example occurs with book titles.  There is a piece of data that specifies how many leading characters should be stripped from the title to use it as a sort key.</p>
<p dir="auto">So far, the changes have been restricted to StringFieldMapper and TokenCountFieldMapper.  I am making the changes in version 2.1.0 and expect to merge them into all subsequent versions.</p>
<p dir="auto">I expect to be able to use the copy_to parameter, but don't know exactly how that will work yet.</p>