我尝试按语言过滤弹性查询。我的查询工作正常,但是当我添加过滤器时,我得到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);
我试图将我的查询直接放在控制器中,但结果相同。我真的不知道为什么我的过滤器没有返回结果。
希望有人可以提供帮助,因为我真的没有看到答案
答案 0 :(得分:2)
首先要做的是在Elastica之外测试您的查询,您可以从Sf2 Profiler或日志中获取JSON。
接下来,确保您的文档以您希望的方式在Elasticsearch中发送(在工具like Sense中执行简单的GET /youmiam/product/_search
请求以查看它们。)
我看到您的brandLanguage
字段正在使用标准分析器,您可以看到Elasticsearch如何使用以下查询对其进行索引:GET /youmiam/_analyze?field=brandLanguage&text=FR
- 是否存在具有确切值{{1}的标记}?如果没有,就没有匹配。
最佳做法是将此类字段设置为“not_analyzed”。
除了您的代码没有问题(您可以使用FilteredQuery而不是postFilter,但这不是问题),您应该更仔细地查看索引的内容以及如何通过Sense直接查询,然后翻译它与Elastica。