craft-scout icon indicating copy to clipboard operation
craft-scout copied to clipboard

Document how to to multiple sites in 1 index

Open riasvdv opened this issue 6 years ago • 16 comments

  • Use ->site('*') in criteria
  • Make sure to return unique objectID from transformer
  • Add an example

riasvdv avatar Sep 16 '19 07:09 riasvdv

I just started making indices for this! Can i do this now or dot you have to do some code changes? Just saw the other issue in this repo..

ghost avatar Sep 16 '19 11:09 ghost

Should all be possible, just not documented everything yet!

riasvdv avatar Sep 16 '19 12:09 riasvdv

Doesn't seem to work. if i use ->site('*') it still only indexes the default site. But saw a new issue came in now: #98

ghost avatar Sep 16 '19 14:09 ghost

Yeah, it will be fixed when #99 is merged, a little oversight I had when rewriting

riasvdv avatar Sep 16 '19 14:09 riasvdv

How would this work? If I have an entry that is enabled for two sites, it only pushes one record to Algolia. I even tried passing in a unique objectID

$indices[] = ScoutIndex::create(CRAFT_ENVIRONMENT . '_Search')
    ->elementType(Entry::class)
    ->criteria(function (EntryQuery $query) {
        return $query->section([
            'news',
            'pressReleases',
            'pages',
            'resources',
            'events',
        ])
        ->siteId([
            Craft::$app->sites->getSiteByHandle('en')->id,
            Craft::$app->sites->getSiteByHandle('es')->id,
        ]);
    })
    ->transformer(function (Entry $entry) {
        return [
            'objectID' => implode('_', [
                $entry->id,
                $entry->site->handle,
            ]),
            'title' => $entry->title,
            'url' => $entry->url,
        ];
    });

davist11 avatar Jul 08 '20 14:07 davist11

@davist11 can you test with this branch? Should be fixed.

"rias/craft-scout": "dev-97-multisite-single-index as 2.3.2",

You've got the right idea with including your site in you objectID. The issue stems from when before Craft allowed for querying multiple sites in a single query.

Unrelated: you could just pass site(['en', 'es']) in yoru query, instead of getting the ids yourself.

timkelty avatar Jul 09 '20 15:07 timkelty

Hmm, is that branch name correct?

  [UnexpectedValueException]                                                                                           
  Could not parse version constraint 97-multisite-single-index as 2.3.2: Invalid version string "97-multisite-single-  
  index" in "97-multisite-single-index as 2.3.2", the alias source must be an exact version, if it is a branch name y  
  ou should prefix it with dev-     

Unrelated: you could just pass site(['en', 'es']) in yoru query, instead of getting the ids yourself.

Niiiice thanks

davist11 avatar Jul 09 '20 15:07 davist11

whoops - use dev-97-multisite-single-index

timkelty avatar Jul 09 '20 15:07 timkelty

Hmm, I'm still not seeing the spanish version getting created in Algolia. Dunno if the new distinct stuff I've added would have impacted that at all. Here's my index

$indices[] = ScoutIndex::create(CRAFT_ENVIRONMENT . '_Search')
    ->elementType(Entry::class)
    ->criteria(function (EntryQuery $query) {
        return $query->section([
            'news',
            'pressReleases',
            'pages',
            'resources',
            'events',
        ])
        ->site([
            'en',
            'es'
        ]);
    })
    ->transformer(function (Entry $entry) {
        return [
            'objectID' => implode('_', [
                $entry->id,
                $entry->site->handle,
            ]),
            'title' => $entry->title,
            'url' => $entry->url,
            'summary' => self::_formatSummary($entry),
            'postDate' => self::_formatDate($entry->postDate),
            'expirationDate' => self::_formatDate($entry->expiryDate),
            'dateUpdated' => self::_formatDate($entry->dateUpdated),
            'type' => self::_formatType($entry),
            'eyebrow' => self::_formatEyebrow($entry->section->name),
            'content' => self::_formatPageBlocks($entry->pageBlocks),
        ];
    })
    ->splitElementsOn([
        'content',
    ])
    ->indexSettings(IndexSettings::create()
        ->hitsPerPage(10)
        ->attributeForDistinct('distinctID')
        ->distinct(true)
        ->attributesForFaceting([
            'type',
            'distinctID',
        ])
        ->replicas([
            CRAFT_ENVIRONMENT . '_Search_dateAsc',
            CRAFT_ENVIRONMENT . '_Search_dateDesc',
        ])
    );

davist11 avatar Jul 09 '20 15:07 davist11

@davist11 weird…I'm using your $indices and it's working for me…

As a sanity check, can you go to your plugins page and make sure it looks like: Screen Shot 2020-07-09 at 11 44 35 AM

Dunno if the new distinct stuff I've added would have impacted that at all

Hmm could be…what are you trying to do there? As it is, your transformer doesn't define a distinctID, so references to it aren't going to work. In fact they're probably just seeing it always as null and not indexing. Since all elements will have unique objectIDs, you shouldn't need any of the distinct stuff, if you're just want a 1-1 match of entry to algolia object.

timkelty avatar Jul 09 '20 15:07 timkelty

As a sanity check, can you go to your plugins page and make sure it looks like

Confirmed

As it is, your transformer doesn't define a distinctID

That value automatically gets added (per the Scout documentation and what I'm seeing in Algolia records). I tried removing the distinct stuff and still not seeing the spanish version.

davist11 avatar Jul 09 '20 15:07 davist11

Is every entry (from each site) being passed separately to the transformer?

davist11 avatar Jul 09 '20 15:07 davist11

Is every entry (from each site) being passed separately to the transformer?

Yes - everything from the criteria gets passed to the transformer, so unless you have a unique parm on your criteria, you'll get back the elements from each site.

timkelty avatar Jul 09 '20 16:07 timkelty

That value automatically gets added (per the Scout documentation and what I'm seeing in Algolia records).

derp – right you are.

If it's possible to share your composer files and db, that'd be helpful. Or even sync to a project.yaml file…

If that's an option, send em to [email protected].

timkelty avatar Jul 09 '20 16:07 timkelty

Sent, thanks!

davist11 avatar Jul 09 '20 17:07 davist11

What is the status for this issue?

Is this available in the main branch?

jornwildenbeest avatar Mar 18 '24 15:03 jornwildenbeest