{"id":261885,"date":"2020-07-31T21:25:27","date_gmt":"2020-07-31T21:25:27","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=261885"},"modified":"2024-03-04T12:05:17","modified_gmt":"2024-03-04T12:05:17","slug":"add-non-product-attribute-to-magento2-elastic-documents","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/","title":{"rendered":"Add Non-Product Attribute To Magento2 Elastic Documents"},"content":{"rendered":"\n<p>In this blog, we will learn how to add custom fields to <a href=\"https:\/\/webkul.com\/blog\/magento2-elasticsearch\/\">Magento 2 elastic search<\/a> documents (products). Sometimes you need to upload some data that is not a product attribute or it is a product attribute but it is not searchable neither filterable on the elastic search server, which can be later used for searching and filtering products in Magento.<\/p>\n\n\n\n<p>Lets see how to do that:<\/p>\n\n\n\n<p>First of all you need to create a class which is going to be the provider for the data that you want to index while Magento indexer runs:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n * Copyright \u00a9 Magento, Inc. All rights reserved.\n * See COPYING.txt for license details.\n *\/\ndeclare(strict_types=1);\n\nnamespace Vendor\\CustomModule\\Model\\Adapter\\BatchDataMapper;\n\nuse Magento\\AdvancedSearch\\Model\\Adapter\\DataMapper\\AdditionalFieldsProviderInterface;\n\n\/**\n * Provide data mapping for custom fields\n *\/\nclass CustomDataProvider implements AdditionalFieldsProviderInterface\n{\n\n    \/**\n     * @inheritdoc\n     *\/\n    public function getFields(array $productIds, $storeId)\n    {\n\n        $fields = &#091;];\n        \n        foreach ($productIds as $productId) {\n            $data = &quot;&quot;; \/\/you can get you data here\n            $fields&#091;$productId] = &#091;&quot;custom_field&quot; =&gt; $data];\n        }\n\n        return $fields;\n    }\n\n}<\/pre>\n\n\n\n<p>So in the above class you can see I have extended this class &#8220;Magento\\AdvancedSearch\\Model\\Adapter\\DataMapper\\AdditionalFieldsProviderInterface&#8221; which is required to create a provider class and also you need to define &#8220;getFields&#8221; method which will give you the access of all the products and store id.<\/p>\n\n\n\n<p>Now we have to add this class in the di.xml file of the module so that Magento can know at the time of indexing to add this field:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">     &lt;virtualType name=&quot;additionalFieldsProviderForElasticsearch&quot; type=&quot;Magento\\AdvancedSearch\\Model\\Adapter\\DataMapper\\AdditionalFieldsProvider&quot;&gt;\n        &lt;arguments&gt;\n            &lt;argument name=&quot;fieldsProviders&quot; xsi:type=&quot;array&quot;&gt;\n                &lt;item name=&quot;custom_field&quot; xsi:type=&quot;object&quot;&gt;Vendor\\CustomModule\\Model\\Adapter\\BatchDataMapper\\CustomDataProvider&lt;\/item&gt;\n            &lt;\/argument&gt;\n        &lt;\/arguments&gt;\n    &lt;\/virtualType&gt;<\/pre>\n\n\n\n<p>Now you have informed Magento to use you provider class to add your field to the elastic index and document.<\/p>\n\n\n\n<p>To check if your code is working simply open this URL in the browser to know the name of the current index:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>http:\/\/localhost:9200\/_all<\/p>\n<\/blockquote>\n\n\n\n<p>In the above URL in place of localhost use your elastic server host and in place of 9200 use your elastic server port. Executing the URL in the browser you will get the schema of the elastic index to find custom_field in that and you will find it under mappings &gt; properties keys.<\/p>\n\n\n\n<p>Thanks \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will learn how to add custom fields to Magento 2 elastic search documents (products). Sometimes you need to upload some data that is not a product attribute or it is a product attribute but it is not searchable neither filterable on the elastic search server, which can be later used for <a href=\"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":33,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,1],"tags":[],"class_list":["post-261885","post","type-post","status-publish","format-standard","hentry","category-magento2","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Add Non-Product Attribute To Magento2 Elastic Documents - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Add Non-Product Attribute To Magento2 Elastic Documents - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"In this blog, we will learn how to add custom fields to Magento 2 elastic search documents (products). Sometimes you need to upload some data that is not a product attribute or it is a product attribute but it is not searchable neither filterable on the elastic search server, which can be later used for [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-31T21:25:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-04T12:05:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ashutosh Srivastava\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashutosh Srivastava\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/\"},\"author\":{\"name\":\"Ashutosh Srivastava\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970\"},\"headline\":\"Add Non-Product Attribute To Magento2 Elastic Documents\",\"datePublished\":\"2020-07-31T21:25:27+00:00\",\"dateModified\":\"2024-03-04T12:05:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/\"},\"wordCount\":277,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"articleSection\":[\"Magento2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/\",\"url\":\"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/\",\"name\":\"Add Non-Product Attribute To Magento2 Elastic Documents - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2020-07-31T21:25:27+00:00\",\"dateModified\":\"2024-03-04T12:05:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Add Non-Product Attribute To Magento2 Elastic Documents\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970\",\"name\":\"Ashutosh Srivastava\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Ashutosh Srivastava\"},\"sameAs\":[\"http:\/\/webkul.com\"],\"url\":\"https:\/\/webkul.com\/blog\/author\/ashutosh\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Add Non-Product Attribute To Magento2 Elastic Documents - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/","og_locale":"en_US","og_type":"article","og_title":"Add Non-Product Attribute To Magento2 Elastic Documents - Webkul Blog","og_description":"In this blog, we will learn how to add custom fields to Magento 2 elastic search documents (products). Sometimes you need to upload some data that is not a product attribute or it is a product attribute but it is not searchable neither filterable on the elastic search server, which can be later used for [...]","og_url":"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2020-07-31T21:25:27+00:00","article_modified_time":"2024-03-04T12:05:17+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Ashutosh Srivastava","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Ashutosh Srivastava","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/"},"author":{"name":"Ashutosh Srivastava","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970"},"headline":"Add Non-Product Attribute To Magento2 Elastic Documents","datePublished":"2020-07-31T21:25:27+00:00","dateModified":"2024-03-04T12:05:17+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/"},"wordCount":277,"commentCount":2,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"articleSection":["Magento2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/","url":"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/","name":"Add Non-Product Attribute To Magento2 Elastic Documents - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2020-07-31T21:25:27+00:00","dateModified":"2024-03-04T12:05:17+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/add-non-product-attribute-to-magento2-elastic-documents\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Add Non-Product Attribute To Magento2 Elastic Documents"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/5555025750ec4e4df34fadc78b083970","name":"Ashutosh Srivastava","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2f5312e6903909ffeb33aa5eb38e1c0bed8f498f92144f5f84065adf7e8708a6?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Ashutosh Srivastava"},"sameAs":["http:\/\/webkul.com"],"url":"https:\/\/webkul.com\/blog\/author\/ashutosh\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/261885","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=261885"}],"version-history":[{"count":3,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/261885\/revisions"}],"predecessor-version":[{"id":418492,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/261885\/revisions\/418492"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=261885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=261885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=261885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}