У меня есть несколько документов, и каждый документ содержит вложенные поля, и я хочу упорядочить их по позиции в зависимости от тематического идентификатора
Document1 :
{
...
"thematics":[
{
"id": 1,
"position": 100
},
{
"id": 2,
"position": 1
}
]
}
Документ2:
{
...
"thematics":[
{
"id": 2,
"position": 3
}
]
}
Документ3:
{
...
"thematics":[
{
"id": 1,
"position": 40
}
]
}
Например, я хотел бы получить только документы с тематикой, которая содержит id = 2, поэтому я сделал что-то вроде этого.
$filter = BoolQuery();
...
$filter->addMust(new Query\Term(["thematics.id" => 2]));
А затем, когда я хочу применить метод сортировки к позиции, где тематический идентификатор = 2, а не что-то еще.
Я пробовал что-то вроде этого:
case 'atp': // asc thematic position
$sort = [
"_score",
[
"thematics.position" => [
"order" => "asc",
"missing" => 0,
],
],
];
break;
...
$this->setSort($sort); // call parent method setSort(array()) of elastica-ruflin
Пример ответа:
Ожидается первый случай: если я хочу отобразить все документы из темы 1, порядок должен быть следующим: Document3, затем Document1.
Ожидается второй случай: если я хочу отобразить все документы из тематики 2, порядок должен быть следующим: Document1, затем Document2.
Но на данный момент у меня есть:
- Первый случай: Document3, Document1
- Второй случай: Document2, Document1
Я предполагаю, что для сортировки в обоих случаях требуется первая тематическая позиция document1.
Редактировать: я пытался изменить сопоставление с вложенным типом
thematics:
type: nested
properties:
label: { type: string, index: not_analyzed }
slug: { index: not_analyzed }
name: { type: string, index: not_analyzed }
position: { type: integer }
id: { type: integer }
И запрос, но все еще не работает
{
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"match_all": {}
}
]
}
}
],
"filter": [
{
"bool": {
"must": [
{
"term": {
"is_searchable": true
}
},
{
"nested": {
"path": "thematics",
"query": {
"term": {
"thematics.id": {
"value": 2
}
}
}
}
},
{
"exists": {
"field": "valuation"
}
},
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"exists": {
"field": "valuation.translations.fr.title"
}
}
]
}
},
{
"bool": {
"must": [
{
"exists": {
"field": "valuation.translations.en.title"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"commercial_subcategory.category.id": 33
}
}
]
}
}
]
}
}
]
}
}
]
}
},
"boost_mode": "multiply",
"functions": [
{
"field_value_factor": {
"field": "booster",
"modifier": "square"
}
}
]
}
},
"sort": [
"_score",
{
"thematics.position": {
"order": "asc",
"missing": 0,
"mode": "min",
"nested_filter": {
"term": {
"thematics.id": {
"value": 2
}
}
}
}
}
]
}
Edit2: я обхожу проблему. Я изменил свое сопоставление, чтобы каждый документ выглядел так:
{
...
"thematics":[
"1": {
"id": 1,
"position": 100
},
"2": {
"id": 2,
"position": 1
}
]
}
Затем я применяю Bool Query must / Exists Filter
к "тематике".$thematicId
И, наконец, мой метод сортировки выглядит так:
case 'atp': // asc thematic position
$sort = [
"_score",
[
"thematics." . $thematicId . ".position" => [
"order" => "asc",
"missing" => 0,
],
],
];
break;
...
$this->setSort($sort); // call parent method setSort(array()) of elastica-ruflin