4

I'm trying to index a custom entity in Solr with Magento. I've followed this guide: http://www.summasolutions.net/blogposts/magento-apache-solr-integration-part-iii-indexing-custom-data, but I still can't make it work.

In my case I have an EAV entity that represents a news and I want the default Magento search showing results including this entity.

Similar to the guide I've created the "getSolrDocument" function:

protected function getNewsSolrDocument($id, $name, $storeId, $visibility, $category, $title, $subtitle, $description) {

    $document = new Apache_Solr_Document();
    $document->addField("id", $id);
    $document->addField("unique", $id);
    $document->addField("news_name_en", $name);

    $document->addField("news_category_en", $category);
    $document->addField("news_title_en", $title);
    $document->addField("news_subtitle_en", $subtitle);
    $document->addField("news_description_en", $description);

    return $document;
}

And a "addSolrDocument":

protected function addSolrDocument(&$solrClient, $document) {
    $solrClient->addDocument($document);
    $solrClient->commit();
}

But $solrClient->addDocument($document) throws an exception:

"400" Status: [doc=15] missing required field: visibility

I've noticed that in the Solr schema provided by Magento there is this part:

<!-- System required fields. -->
    <field name="id"            type="string"   indexed="true" required="true" stored="true"/>
    <field name="unique"        type="string"   indexed="true" required="true"/>
    <field name="store_id"      type="int"      indexed="true" required="true"/>
    <field name="in_stock"      type="boolean"  indexed="true" required="true"/>
    <field name="visibility"    type="int"      indexed="true" required="true"/>

The part defines which fields are mandatory but for my news entity "in_stock" and "visibility" are useless.

I'm using Magento EE 1.14.0.1 and Solr 3.6.2. What's the proper way to achive my goal? Thanks

unveloper
  • 260
  • 1
  • 4
  • 12

1 Answers1

2

Without knowing much about your scenario, you could simply add the following line to your function body:

$document->addField("visibility", $visibility);

Apparently, the $visibility variable is passed, so it doesn't hurt adding it. It may increase the index size, but an integer shouldn't hurt to much (besides, the name indicates that it's just some sort of a flag).

lxg
  • 12,375
  • 12
  • 51
  • 73
  • `// This fields are required. // Store the entity with a prefix in the id so we can distinguish them from the catalog products. $blodId = self::ENTITY_UNIQUE_PREFIX . $id; $document->addField(self::ENTITY_ID_FIELD, $blodId); $document->addField(self::ENTITY_UNIQUE_FIELD, $id); $document->addField(self::ENTITY_STORE_ID_FIELD, $storeId); // This fields are required but useless for our "news entity". $document->addField(self::ENTITY_STORED_FIELD, true); ` – unveloper Sep 12 '14 at 15:55
  • `// This fields are the ones we really want. $document->addField(self::ENTITY_VISIBILITY_FIELD, $enabled); $document->addField(self::NEWS_TITLE_FIELD, $title); $document->addField(self::NEWS_SUBTITLE_FIELD, $subtitle); $document->addField(self::NEWS_DESCRIPTION_FIELD, $description);` – unveloper Sep 12 '14 at 15:56