I am newer with elastica and i would like to get all products but sorting by favorites,
in my mode : poductDocument, i added a collection field to store the ids of users how added this product to favorite :
class poductDocument implements DocumentInterface
{
private int $id;
private string $label;
private Collection $userIdsWhoAddedThisProductToFavorite;
public function getId(): int
{
return $this->id;
}
public function setId(int $id): self
{
$this->id = $id;
return $this;
}
public function getLabel(): string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function getUserIdsWhoAddedThisProductToFavorite(): Collection
{
return $this->userIdsWhoAddedThisProductToFavorite;
}
public function setUserIdsWhoAddedThisProductToFavorite(array $data): self
{
$this->userIdsWhoAddedThisProductToFavorite = new ArrayCollection($data);
return $this;
}
}
And my mapping :
settings:
number_of_replicas: 0
number_of_shards: 1
refresh_interval: 60s
mappings:
dynamic: false
properties:
id:
type: integer
label:
type: keyword
fields:
autocomplete:
type: text
analyzer: app_autocomplete
search_analyzer: standard
text:
type: text
analyzer: french
fielddata: true
user_ids_who_added_this_product_to_favorite:
type: integer
And in my custom filter i used Query term to find my favorite products
public function applySort(Query $query, Query\BoolQuery $boolQuery): void
{
$termQuery = new Query\Term();
$termQuery->setTerm('user_ids_who_added_this_product_to_favorite', $this->getUser()->getId());
$boolQuery->addMust($termQuery);
}
This code is working but give me just the favorite products, what i would like to do is to get all my product sorted by favorite product for example if i have 4 products and i have product 1 and 2 as favorite my code give me :
product 1
product 2
and i'd like that the result be :
product 1
product 2
product 3
product 4
Any help please
CodePudding user response:
$termQuery = new Query\Term();
$termQuery->setTerm('user_ids_who_added_this_product_to_favorite', $this->getUser()->getId());
$boolQuery->addShould($termQuery);
$filterBoolQuery = new Query\BoolQuery();
$matchQuery = new Query\MatchQuery('user_ids_who_added_this_product_to_favorite', $this->getUser()->getId());
$filterBoolQuery->addMust($matchQuery);