elasticsearch Elastica按匹配顺序获取所有结果

zd287kbt  于 2022-12-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(121)

我是较新的弹性,我想得到所有的产品,但排序的最爱,
在我模式下:productDocument,我添加了一个集合字段来存储用户的id如何将此产品添加到收藏夹:

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;
    }
}

和我的Map:

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

在我的自定义过滤器中,我使用Query term来查找我最喜欢的产品

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);
}

这个代码是工作,但给予我只是最喜欢的产品,我想做的是让我所有的产品排序最喜欢的产品例如,如果我有4个产品,我有产品1和2作为最喜欢的我的代码给我:

product 1
product 2

我希望结果是:

product 1
product 2
product 3
product 4

有没有人帮忙

6uxekuva

6uxekuva1#

$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);

相关问题