symfony - Fos elastica过滤器

标签 symfony filter elasticsearch elastica

我尝试按语言过滤我的Elastica查询。我的查询工作正常,但是当我添加过滤器时,我得到0个结果。

我的实体:

<?php

namespace Youmiam\RecipeBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Product
 *
 * @ORM\Table(name="product")
 * @ExclusionPolicy("all")
 * @ORM\Entity(repositoryClass="Youmiam\RecipeBundle\Entity\ProductRepository")
 */
class Product
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="createdAt", type="datetime")
     */
    private $createdAt;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="Youmiam\RecipeBundle\Entity\Brand", inversedBy="products")
     * @ORM\JoinColumn(name="brand_id", referencedColumnName="id")
    */
    private $brand;

    /**
     * @ORM\ManyToOne(targetEntity="Youmiam\RecipeBundle\Entity\Ingredient", inversedBy="products")
     * @ORM\JoinColumn(name="ingredient_id", referencedColumnName="id")
     * @Expose
     */
    private $ingredient;

    /**
     * @ORM\ManyToMany(targetEntity="Youmiam\RecipeBundle\Entity\Recipe", inversedBy="products")
     * @ORM\JoinTable(name="products__recipes")
     */
    private $recipes;

    /**
     * @ORM\OneToMany(targetEntity="Youmiam\RecipeBundle\Entity\Quantity", mappedBy="product", orphanRemoval=true, cascade={"all"})
     */
    private $quantities;

    /**
     * @var \stdClass
     *
     * @ORM\Column(name="photo", type="string", length=255, nullable=true)
     * @Expose
     */
     private $photo;

    /**
     * @Assert\File(maxSize="6000000")
     */
     private $file;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     * @return Product
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Get createdAt
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Product
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set ingredient
     *
     * @param \Youmiam\RecipeBundle\Entity\Ingredient $ingredient
     * @return Product
     */
    public function setIngredient(\Youmiam\RecipeBundle\Entity\Ingredient $ingredient = null)
    {
        $this->ingredient = $ingredient;

        return $this;
    }

    /**
     * Get ingredient
     *
     * @return \Youmiam\RecipeBundle\Entity\Ingredient 
     */
    public function getIngredient()
    {
        return $this->ingredient;
    }

    /**
     * Set brand
     *
     * @param \Youmiam\RecipeBundle\Entity\Brand $brand
     * @return Product
     */
    public function setBrand(\Youmiam\RecipeBundle\Entity\Brand $brand = null)
    {
        $this->brand = $brand;

        return $this;
    }

    /**
     * Get brand
     *
     * @return \Youmiam\RecipeBundle\Entity\Brand 
     */
    public function getBrand()
    {
        return $this->brand;
    }

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->recipes = new \Doctrine\Common\Collections\ArrayCollection();
        $this->quantities = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Set photo
     *
     * @param string $photo
     * @return Product
     */
    public function setPhoto($photo)
    {
        $this->photo = $photo;

        return $this;
    }

    /**
     * Get photo
     *
     * @return string 
     */
    public function getPhoto()
    {
        return $this->photo;
    }

    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
    }

    /**
     * Get file.
     *
     * @return UploadedFile
     */
    public function getFile()
    {
        return $this->file;
    }
    /**
     * Add recipes
     *
     * @param \Youmiam\RecipeBundle\Entity\Recipe $recipes
     * @return Product
     */
    public function addRecipe(\Youmiam\RecipeBundle\Entity\Recipe $recipes)
    {
        $this->recipes[] = $recipes;

        return $this;
    }

    /**
     * Remove recipes
     *
     * @param \Youmiam\RecipeBundle\Entity\Recipe $recipes
     */
    public function removeRecipe(\Youmiam\RecipeBundle\Entity\Recipe $recipes)
    {
        $this->recipes->removeElement($recipes);
    }

    /**
     * Get recipes
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getRecipes()
    {
        return $this->recipes;
    }

    /**
     * Add quantities
     *
     * @param \Youmiam\RecipeBundle\Entity\Quantity $quantities
     * @return Product
     */
    public function addQuantity(\Youmiam\RecipeBundle\Entity\Quantity $quantities)
    {
        $this->quantities[] = $quantities;

        return $this;
    }

    /**
     * Remove quantities
     *
     * @param \Youmiam\RecipeBundle\Entity\Quantity $quantities
     */
    public function removeQuantity(\Youmiam\RecipeBundle\Entity\Quantity $quantities)
    {
        $this->quantities->removeElement($quantities);
    }

    /**
     * Get quantities
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getQuantities()
    {
        return $this->quantities;
    }

    /**
     * get the class Name
     * @return string $className
     */
    public function getClass()
    {
        return "Product";
    }

    /**
     * @return Language
     */
    public function getBrandLanguage()
    {
        return $this->brand->getLanguage();
    }
}

我的Config.yml
fos_elastica:
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        youmiam:
            settings:
                index:
                    analysis:
                        analyzer:
                            keyword_analyser:
                                type: custom
                                tokenizer: keyword
                            classic_analyser:
                                type: custom
                                tokenizer: lowercase
                                filter   : [my_snow,asciifolding]
                            ingr_analyser:
                                type: custom
                                tokenizer: lowercase
                                filter   : [my_ing_ngram,asciifolding]
                            recipe_analyser:
                                type: custom
                                tokenizer: lowercase
                                filter   : [my_recipe_ngram,asciifolding]
                            testfollower:
                                type: stop
                                stopwords : [',']
                        filter:
                            my_snow:
                                type : "snowball"
                                language : "French"
                            my_ing_ngram:
                                type: "nGram"
                                min_gram: 3
                                max_gram: 8
                            my_recipe_ngram:
                                type: "nGram"
                                min_gram: 4
                                max_gram: 10
                        char_filter:    
                            my_whtoa :
                                type : mapping
                                mappings : ["' '=>a",]
            client: default
            finder: ~
            types:
                product:
                    mappings:
                        name: { boost: 10, analyzer: classic_analyser }
                        brand: { boost: 10, analyzer: classic_analyser }
                        ingredient: { boost: 10, analyzer: classic_analyser }
                        brandLanguage: { boost: 10 }
                    persistence:
                        driver: orm
                        model: Youmiam\RecipeBundle\Entity\Product
                        provider: ~
                        listener: ~
                        finder: ~
                        repository: Youmiam\RecipeBundle\SearchRepository\ProductRepository

然后,在我的fos elastica存储库中,我有以下代码:
$boolQuery = new \Elastica\Query\Bool();
        $query = new \Elastica\Query;
        $queryString = new \Elastica\Query\QueryString();
        $queryString->setQuery($searchText);
        $queryString->setAnalyzer('classic_analyser');
        $queryString->setFields(array('product.name', 'product.brand', 'product.ingredient'));
        $boolQuery->addMust($queryString);
        $query->setQuery($boolQuery);

        $filter = new \Elastica\Filter\Term();
        $filter->setTerm('brandLanguage', 'fr');

        $query->setPostFilter($filter);
        return $this->find($query);

我试图将查询直接放在 Controller 中,但结果相同。我真的不知道为什么我的过滤器没有返回结果。

希望有人可以帮助,因为我真的看不到答案

最佳答案

首先要做的是在Elastica外部测试查询,您可以从Sf2 Profiler或日志中获取JSON。

接下来,确保您的文档以所需的方式在Elasticsearch中发送(在工具like Sense中执行一个简单的GET /youmiam/product/_search请求以查看它们)。

我看到您的brandLanguage字段正在使用标准分析器,您可以看到Elasticsearch如何通过这样的查询对它进行索引:GET /youmiam/_analyze?field=brandLanguage&text=FR-是否存在具有确切值fr的 token ?如果没有,将没有匹配项。

最佳做法是也将此类字段设置为“not_analyzed”。

除了代码没有问题(可以使用FilteredQuery而不是postFilter但这不是问题)之外,您还应该仔细研究索引的内容以及如何直接通过Sense查询它,然后使用Elastica对其进行翻译。

关于symfony - Fos elastica过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25895301/

相关文章:

php - Symfony 新包无法找到模板

javascript - 将数组传递给 JavaScript 过滤器函数

bash - 如何使用 Bash 从一组字符串 B 中过滤出一组字符串 A

javascript - 根据类名对表行进行分组

node.js - 如何每小时在AWS上运行Node.js项目?

php - 如何处理(MySQL查询): Only find newest rows by DISTINCT WHERE the newst one IS NOT a specific value (with symfony2)

php - Symfony2 : Routing ignores port in URL

php - Symfony HttpFoundation 请求 getBaseUrl() 与 getBasePath()

elasticsearch - Elasticsearch脚本查询无痛指数函数

elasticsearch - 从Kibana反规范化数据