gpt4 book ai didi

php - FOS Elastica嵌套过滤器不起作用

转载 作者:行者123 更新时间:2023-12-03 02:04:54 24 4
gpt4 key购买 nike

我正在努力使自己的嵌套过滤器能够正常工作,

我有一个人实体,这是它的映射

http://localhost:9200/search/person/_mapping
{
"search": {
"mappings": {
"person": {
"_meta": {
"model": "Foo\\CoreBundle\\Entity\\Person"
},
"properties": {
"adresses": {
"type": "nested",
"properties": {
"city": {
"type": "string",
"store": true
}
}
},
"certified": {
"type": "string",
"store": true
},
"completeness": {
"type": "string",
"store": true
},
"fullname": {
"type": "string",
"store": true
},
"lastName": {
"type": "string",
"store": true
},
"name": {
"type": "string",
"store": true
},
"source": {
"type": "string",
"store": true
},
"tags": {
"type": "nested",
"properties": {
"name": {
"type": "string",
"store": true
}
}
},
"type": {
"type": "string",
"store": true
}
}
}
}
}
}

数据似乎正确填充,有一个带有以下标签的人员实体
{
"people": [
{
"id": 13355,
"created_at": "2014-12-27T09:30:54+0100",
"updated_at": "2014-12-27T09:30:54+0100",
"name": "Vorname",
"last_name": "nachname",
"ms": "Anrede",
"title": "Titel",
"source": "Quelle",
"description": "info",
"email": "email",
"language": "EN",
"status": "unready",
"links": [
"link"
],
"tags": [
{
"id": 4176,
"created_at": "2014-12-27T09:30:54+0100",
"updated_at": "2014-12-27T09:30:54+0100",
"name": "position",
"type": "function"
},
{
"id": 4177,
"created_at": "2014-12-27T09:30:54+0100",
"updated_at": "2014-12-27T09:30:54+0100",
"name": "kategorie",
"type": "category"
}
],
"type": "kategorie",
"slug": "vorname_nachname",
"certified": "certified"
}
]
}

您会看到两个名称分别为“position”和“kategorie”的标签

这是我的代码,我的basequery是fullname属性的通配符,可以完美工作
$finder = $this->container->get('fos_elastica.finder.search.person');
$query = new \Elastica\Query();
$baseQuery=new \Elastica\Query\Wildcard();
$baseQuery->setValue("fullname", "*".trim(mb_strtolower($term))."*", $boost = 1.0);

$nestedFilter = new \Elastica\Filter\Nested();
$termFilter = new \Elastica\Filter\Term();
$termFilter->setTerm("name","position");
$nestedFilter->setPath("tags");
$nestedFilter->setFilter($termFilter);
$baseQuery = new \Elastica\Query\Filtered($baseQuery, $nestedFilter);
$query->setQuery($baseQuery);
$people = $finder->find($query);

这是结果查询:
elastica.INFO: search/person/_search (GET) 3.30 ms 
{
"query": {
"filtered": {
"query": {
"wildcard": {
"fullname": {
"value": "**",
"boost": 1
}
}
},
"filter": {
"nested": {
"path": "tags",
"filter": {
"term": {
"name": "position"
}
}
}
}
}
},
"size": "10",
"from": 0
}

但是没有结果,如果我省略了嵌套的术语过滤器,它会起作用

知道我在做什么错吗?这是我的orm映射:
/**
* @ORM\ManyToMany(targetEntity="Foo\CoreBundle\Entity\Tag", inversedBy="tags")
* @ORM\JoinTable(name="person_has_tags")
**/
private $tags;

最佳答案

所以我几个月前实际上遇到了这个问题。简而言之, Elasticsearch 索引会破坏标签的名称,因此“位置”不再完全匹配。 Elasticsearch 的索引实际上更接近“pos”,“it”和“tion”。您可能需要让 Elasticsearch 知道将对象移交给索引时会破坏该字段,或者您只是搜索根据标签的ID而不是名称。我最终选择了id路线,因为它对我来说似乎更有效。

这是我的代码示例,请注意,我将嵌套的过滤器嵌入到bool过滤器中,因为我还有其他未在此处显示的搜索参数:

$query = new Query\QueryString('*' . $searchText .'*');
$boolFilter = new Bool();
$nestedFilter = new Nested();
$nestedFilter->setPath('categories');
$categoryFilter = new Term(['id' => $categoryId]);
$nestedFilter->setFilter($categoryFilter);
$boolFilter->addMust($nestedFilter);
$filteredQuery = new Filtered($query, $boolFilter);

和我的 flex 对象的一小段:
"name": "Sample Object",
"published": true
"categories": [
{
"id": 1,
"name": "cool"
},
{
"id": 3,
"name": "awesome"
}
]

关于php - FOS Elastica嵌套过滤器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27665866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com