<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Samples - Moses Dinakaran</title>
	<atom:link href="https://mosesdinakaran.in/category/code-samples/feed/" rel="self" type="application/rss+xml" />
	<link>https://mosesdinakaran.in</link>
	<description>Tutorials and Documentations on Magento</description>
	<lastBuildDate>Tue, 08 Nov 2022 08:48:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
	<item>
		<title>Magento2 - Create custom ElasticSearch Tokenizer &#038; Analyzer to search comma separated value in product attribute</title>
		<link>https://mosesdinakaran.in/magento2-create-custom-elasticsearch-tokenizer-analyzer-to-search-comma-separated-value-in-product-attribute/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=magento2-create-custom-elasticsearch-tokenizer-analyzer-to-search-comma-separated-value-in-product-attribute</link>
					<comments>https://mosesdinakaran.in/magento2-create-custom-elasticsearch-tokenizer-analyzer-to-search-comma-separated-value-in-product-attribute/#respond</comments>
		
		<dc:creator><![CDATA[mosesdinakaran@gmail.com]]></dc:creator>
		<pubDate>Tue, 08 Nov 2022 03:25:14 +0000</pubDate>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[GraphQl]]></category>
		<guid isPermaLink="false">https://mosesdinakaran.in/?p=1541</guid>

					<description><![CDATA[<p>In this blog we will see how to search a custom product attribute that contains comma separated value by building a custom elastic search tokenizer and analyzer. The default elastic search tokenizer that magento implements is space separated, i.e if you have a string "Love Magento" we can search by the word "Love" and "Magento" [&#8230;]</p>
<p>The post <a href="https://mosesdinakaran.in/magento2-create-custom-elasticsearch-tokenizer-analyzer-to-search-comma-separated-value-in-product-attribute/">Magento2 - Create custom ElasticSearch Tokenizer & Analyzer to search comma separated value in product attribute</a> first appeared on <a href="https://mosesdinakaran.in">Moses Dinakaran</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>In this blog we will see how to search a custom product attribute that contains comma separated value by building a custom elastic search tokenizer and analyzer.</p>



<p>The default elastic search tokenizer that magento implements is space separated, i.e if you have a string "Love Magento" we can search by the word "Love" and "Magento" because by default all the strings are tokenized using space.</p>



<p>Consider an requirement where we have multiple barcodes for a given product and these barcodes are stored in the product attribute "barcode" and searching any of these barcode values should return the respective product.</p>



<p>We have two solutions for this </p>



<figure class="wp-block-table"><table><tbody><tr><td>Method</td><td>GraphQl Query</td><td>Pros</td><td>Cons</td></tr><tr><td>Using Space Separated<br>The barcodes are stored with Space Separated<br>Ex 1001 1002 1003</td><td><div class="wp-block-syntaxhighlighter-code "><pre class="brush: css; title: ; notranslate">
{
  products(search: &quot;1002&quot;) {
    total_count
    items {
      sku
      barcode
    }
  }
}

</pre></div></td><td>No Customization Required</td><td>Possibility of returning Duplicate data</td></tr><tr><td>Using Comma Separated<br>The barcodes are separated with comma<br>Ex 1001,1002,1003</td><td>  <div class="wp-block-syntaxhighlighter-code "><pre class="brush: css; title: ; notranslate">
{
  products(filter: {barcode:{match:&quot;1002&quot;}}) {
    total_count
    items {
      sku
      barcode
    }
  }
}

</pre></div> </td><td>Customization Required</td><td>100% accuracy</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">Method 1 : Using Space Separated</h3>



<p>By default magento supports text search i.e All the words within the text field and tokenized and we can search for any word which will return the correct data</p>



<p>For ex if we have this data </p>



<p>barcode = "1001 1002 1003"</p>



<p>we can search by either 1001 or 1002 or 1003.</p>



<p>The advantage of this approach is that we don't need to do any kind of customization, we just need to update the barcodes with space separated</p>



<p>The problem with this is that in case if we have any value like 1001 in some other product name or any other searchable product attribute those product will also get returned which we really don't want.</p>



<h3 class="wp-block-heading">Method 2 : Using Comma Separated</h3>



<p>To use comma separated search for a field, we first need to define the new tokenizer and analyzer settings for the elastic search index. Lets see in detail below.</p>



<h5 class="wp-block-heading">Create new tokenizer and analyzer for comma separated</h5>



<p>The tokenizer and analyzer setting for the elastic search index in defined in this file </p>



<p>\Magento\Elasticsearch\Model\Adapter\Index\Builder</p>



<p>We need to updated these setting to support our new tokenizer so lets create a plugin for that</p>



<p>Create a new plugin in app/etc/di.xml</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;type name=&quot;Magento\Elasticsearch\Model\Adapter\Index\Builder&quot;&gt;
   &lt;plugin name=&quot;add_comma_tokenizer&quot; type=&quot;You\YourModule\Plugin\Magento\Elasticsearch\Model\Adapter\Index\BuilderPlugin&quot;/&gt;
&lt;/type&gt;
</pre></div>


<p>Now create the analyzer and tokenizer in that plugin class</p>



<p>You\YourModule\Plugin\Magento\Elasticsearch\Model\Adapter\Index\BuilderPlugin.php</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: php; title: ; notranslate">
class BuilderPlugin
{
    /**
     * @param Builder $subject
     * @param array $settings
     * @return array
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function afterBuild(
        Builder $subject,
        array $settings
    ) {
        /**
         * Define the tokenizer
         */
        $settings&#x5B;&#039;analysis&#039;]&#x5B;&#039;tokenizer&#039;] = array_merge($settings&#x5B;&#039;analysis&#039;]&#x5B;&#039;tokenizer&#039;] ?? &#x5B;], &#x5B;
            &quot;comma_tokenizer&quot; =&gt; &#x5B;
                &quot;type&quot; =&gt; &quot;pattern&quot;,
                &quot;pattern&quot; =&gt; &quot;,&quot;,
            ]
        ]);

        /**
         * Set the analyzer
         */
        $settings&#x5B;&#039;analysis&#039;]&#x5B;&#039;analyzer&#039;] = array_merge($settings&#x5B;&#039;analysis&#039;]&#x5B;&#039;analyzer&#039;], &#x5B;
            &#039;comma_separated&#039; =&gt; &#x5B;
                &#039;type&#039; =&gt; &#039;custom&#039;,
                &#039;tokenizer&#039; =&gt; &#039;comma_tokenizer&#039;,
                &#039;filter&#039; =&gt; array_merge(
                    &#x5B;&#039;lowercase&#039;, &#039;keyword_repeat&#039;]
                ),
            ]
        ]);
        return $settings;
    }
</pre></div>


<h5 class="wp-block-heading">Assign the new analyzer to the field barcode</h5>



<p>Now that we have created and analyzer its time to assign this analyzer to the field "barcode".</p>



<p>The field specific analyzers are  defined in this class</p>



<p>\Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\StaticField::getField</p>



<p>So lets create a plugin to update the barcode field</p>



<p>app/etc/di.xml</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;type name=&quot;Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\StaticField&quot;&gt;
        &lt;plugin name=&quot;set_tokenizer&quot;
                type=&quot;You\YourModule\Plugin\Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\StaticFieldPlugin&quot; /&gt;
    &lt;/type&gt;
</pre></div>


<p>You\YourModule\Plugin\Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\StaticFieldPlugin.php</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: php; title: ; notranslate">
class StaticFieldPlugin
{
    /**
     * @param FieldProviderInterface $subject
     * @param $fieldMapping
     * @return array
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function afterGetField(
        FieldProviderInterface $subject,
        $fieldMapping
    ) {
        // Set the custom analyzer for barcode
        if(isset($fieldMapping&#x5B;&#039;aims_barcode&#039;])) {
            $fieldMapping&#x5B;&#039;barcode&#039;]&#x5B;&#039;analyzer&#039;] = &#039;comma_separated&#039;;
        }
        return $fieldMapping;
    }
}
</pre></div>


<h5 class="wp-block-heading">Do a Catalog Search Full Reindex</h5>



<p>A full reindex is required as we are changing the setting of the elastic search index.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
php bin/magento indexer:reindex catalogsearch_fulltext
</pre></div>


<p></p>



<h5 class="wp-block-heading">Validate in Elastic Search</h5>



<p>Lets see how the field mapping for our attribute "barcode" is set in elastic search.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
curl --location --request GET &#039;http://localhost:9200/magento2_product_1/_mapping/field/barcode&#039;
</pre></div>

<div class="wp-block-syntaxhighlighter-code "><pre class="brush: css; highlight: [17]; title: ; notranslate">
{
    &quot;magento2_product_1_v45&quot;: {
        &quot;mappings&quot;: {
            &quot;aims_barcode&quot;: {
                &quot;full_name&quot;: &quot;barcode&quot;,
                &quot;mapping&quot;: {
                    &quot;aims_barcode&quot;: {
                        &quot;type&quot;: &quot;text&quot;,
                        &quot;fields&quot;: {
                            &quot;keyword&quot;: {
                                &quot;type&quot;: &quot;keyword&quot;
                            }
                        },
                        &quot;copy_to&quot;: &#x5B;
                            &quot;_search&quot;
                        ],
                        &quot;analyzer&quot;: &quot;comma_separated&quot;
                    }
                }
            }
        }
    }
}
</pre></div>


<p>if you notice the analyzer is set to "comma_separated" so any data that you enter in to this field with comma will be tokenized.</p><p>The post <a href="https://mosesdinakaran.in/magento2-create-custom-elasticsearch-tokenizer-analyzer-to-search-comma-separated-value-in-product-attribute/">Magento2 - Create custom ElasticSearch Tokenizer & Analyzer to search comma separated value in product attribute</a> first appeared on <a href="https://mosesdinakaran.in">Moses Dinakaran</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://mosesdinakaran.in/magento2-create-custom-elasticsearch-tokenizer-analyzer-to-search-comma-separated-value-in-product-attribute/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Fetch the Complete Magento 2 Graphql Schema</title>
		<link>https://mosesdinakaran.in/how-to-fetch-the-complete-magento-2-graphql-schema/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-fetch-the-complete-magento-2-graphql-schema</link>
					<comments>https://mosesdinakaran.in/how-to-fetch-the-complete-magento-2-graphql-schema/#respond</comments>
		
		<dc:creator><![CDATA[mosesdinakaran@gmail.com]]></dc:creator>
		<pubDate>Sat, 15 Oct 2022 09:32:24 +0000</pubDate>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[GraphQl]]></category>
		<guid isPermaLink="false">https://mosesdinakaran.in/?p=1362</guid>

					<description><![CDATA[<p>You can use the below graphql request to fetch the complete graphql schema. Method: Post Url : yourdomain/graphql Body : Response The output would be some thing like the below, i.e you will get the the complete schema of the system. Ustually it will be around 50000+ lines</p>
<p>The post <a href="https://mosesdinakaran.in/how-to-fetch-the-complete-magento-2-graphql-schema/">How to Fetch the Complete Magento 2 Graphql Schema</a> first appeared on <a href="https://mosesdinakaran.in">Moses Dinakaran</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>You can use the below graphql request to fetch the complete graphql schema.</p>



<p>Method: Post</p>



<p>Url : yourdomain/graphql</p>



<p>Body : </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: php; title: ; notranslate">
query IntrospectionQuery {
  __schema {
    queryType {
      name
    }
    mutationType {
      name
    }
    subscriptionType {
      name
    }
    types {
      ...FullType
    }
    directives {
      name
      description
      locations
      args {
        ...InputValue
      }
    }
  }
}
fragment FullType on __Type {
  kind
  name
  description
  fields(includeDeprecated: true) {
    name
    description
    args {
      ...InputValue
    }
    type {
      ...TypeRef
    }
    isDeprecated
    deprecationReason
  }
  inputFields {
    ...InputValue
  }
  interfaces {
    ...TypeRef
  }
  enumValues(includeDeprecated: true) {
    name
    description
    isDeprecated
    deprecationReason
  }
  possibleTypes {
    ...TypeRef
  }
}
fragment InputValue on __InputValue {
  name
  description
  type {
    ...TypeRef
  }
  defaultValue
}
fragment TypeRef on __Type {
  kind
  name
  ofType {
    kind
    name
    ofType {
      kind
      name
      ofType {
        kind
        name
        ofType {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
              }
            }
          }
        }
      }
    }
  }
}

</pre></div>


<h5 class="wp-block-heading">Response</h5>



<p>The output would be some thing like the below, i.e you will get the the complete schema of the system. Ustually it will be around 50000+ lines</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="506" src="https://mosesdinakaran.in/wp-content/uploads/2022/10/image-42-1024x506.png" alt="" class="wp-image-1437" srcset="https://mosesdinakaran.in/wp-content/uploads/2022/10/image-42-1024x506.png 1024w, https://mosesdinakaran.in/wp-content/uploads/2022/10/image-42-300x148.png 300w, https://mosesdinakaran.in/wp-content/uploads/2022/10/image-42-768x380.png 768w, https://mosesdinakaran.in/wp-content/uploads/2022/10/image-42-1536x760.png 1536w, https://mosesdinakaran.in/wp-content/uploads/2022/10/image-42.png 1759w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: css; title: ; notranslate">

</pre></div><p>The post <a href="https://mosesdinakaran.in/how-to-fetch-the-complete-magento-2-graphql-schema/">How to Fetch the Complete Magento 2 Graphql Schema</a> first appeared on <a href="https://mosesdinakaran.in">Moses Dinakaran</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://mosesdinakaran.in/how-to-fetch-the-complete-magento-2-graphql-schema/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
