studioespresso / craft-scout

Craft Scout provides a simple solution for adding full-text search to your entries. Scout will automatically keep your search indexes in sync with your entries.
MIT License
81 stars 55 forks source link

Document how to to multiple sites in 1 index #97

Open riasvdv opened 5 years ago

riasvdv commented 5 years ago
ghost commented 5 years ago

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..

riasvdv commented 5 years ago

Should all be possible, just not documented everything yet!

ghost commented 5 years ago

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

riasvdv commented 5 years ago

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

davist11 commented 4 years ago

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,
        ];
    });
timkelty commented 4 years ago

@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.

davist11 commented 4 years ago

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

timkelty commented 4 years ago

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

davist11 commented 4 years ago

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',
        ])
    );
timkelty commented 4 years ago

@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.

davist11 commented 4 years ago

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 commented 4 years ago

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

timkelty commented 4 years ago

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 commented 4 years ago

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 tim@timkelty.com.

davist11 commented 4 years ago

Sent, thanks!

jornwildenbeest commented 6 months ago

What is the status for this issue?

Is this available in the main branch?