12

Can someone tell me if there's an equivalent of Solr copyField directive on ElasticSearch?

I know there is the multi-field type: http://www.elasticsearch.org/guide/reference/mapping/multi-field-type.html It is nice when you want to apply multiple analyzers on the same field.

But it's not exactly the same. Solr permits to "merge" multiple fields into one:

<field name="id" type="string" indexed="true" stored="true"/>
<field name="name" type="string" indexed="true" stored="true"/>
<field name="subject" type="string" indexed="true" stored="true"/>
<field name="location" type="string" indexed="true" stored="true"/>
<field name="all" type="text" indexed="true" stored="true" multiValued="true"/>
<copyField source="*" dest="all"/>

This plugin is pretty promising: https://github.com/yakaz/elasticsearch-analysis-combo

Because it permits to get back the results as a single field when using an ElasticSearch multi value field. But it's still not exactly the same because it doesn't permit to "merge" multiple fields.


I would like a combination of both Combo analyzer and Solr copyField.

I have a blog post model (title/description fields) and would like to copy both the title and description on a single field "blogContent" on which I'll apply 2 different analyzers.

Is there a solution in ElasticSearch?

vmaldosan
  • 444
  • 4
  • 14
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419

2 Answers2

8

There is special _all field that by default gets a copy of all other fields. You can control inclusion into the _all field using include_in_all attribute. However, you are limited to one field like this. If you need more then one, you would need to handle it on the search side by searching multiple fields.

It's also possible to achieve functionality similar to copyField by using multi_field with the "path": "just_name" attribute:

curl -XPUT localhost:9200/test-idx -d '{
    "settings": {
        "index": {
            "number_of_shards": 1,
            "number_of_replicas": 0
        }
    },
    "mappings": {
        "doc": {
            "properties": {
                "first_name": {
                    "type": "multi_field",
                    "path": "just_name",
                    "fields": {
                        "first_name": {"type": "string", "index": "analyzed"},
                        "name": {"type": "string","index": "analyzed"}
                    }
                },
                "last_name": {
                    "type": "multi_field",
                    "path": "just_name",
                    "fields": {
                        "last_name": {"type": "string", "index": "analyzed"},
                        "name": {"type": "string","index": "analyzed"}
                    }
                }
            }
        }
    }
}'
echo
curl -XPUT localhost:9200/test-idx/doc/1 -d '{
    "first_name": "Sebastien",
    "last_name": "Lorber"
}'
echo
curl -XPOST localhost:9200/test-idx/_refresh
echo
curl "localhost:9200/test-idx/doc/_search?q=name:Sebastien"
echo
curl "localhost:9200/test-idx/doc/_search?q=name:Lorber"
imotov
  • 28,277
  • 3
  • 90
  • 82
  • I know the _all but it would be better to be able to create many different _all if needed – Sebastien Lorber Oct 29 '12 at 15:47
  • 1
    I agree, it would be a nice feature to have. Unfortunately, it's not implemented yet https://github.com/elasticsearch/elasticsearch/issues/1169 – imotov Oct 29 '12 at 15:59
  • The `_all` feature has been deprecated in Elasticsearch 6+. They recommend using the `copy_to` mapping parameter. – vmaldosan Dec 20 '18 at 17:32
6

Elastic search supports copyTo:

You can add an analyzer on the field you want to copy to.

Karsten R.
  • 1,628
  • 12
  • 14
codingtim
  • 295
  • 1
  • 4
  • 13