Elasticsearch:对嵌套字段

时间:2017-02-09 21:29:48

标签: php symfony sorting elasticsearch elastica

我有几个doc,每个doc都包含嵌套字段,我想根据主题id来定位 文件1:

{
...
    "thematics":[
        {
            "id": 1,
            "position": 100
        },
        {
             "id": 2,
             "position": 1
        }
    ]
}

文档2:

{
...
    "thematics":[
       {
             "id": 2,
             "position": 3
        }
    ]
}

文件3:

{
...
    "thematics":[
       {
             "id": 1,
             "position": 40
        }
    ]
}

例如,我想只获得包含id = 2的thematics的文档 所以我做了类似的事情

$filter = BoolQuery();
...
$filter->addMust(new Query\Term(["thematics.id" => 2]));

然后当我想在主题id = 2而不是别的东西时应用sort方法。

我尝试过类似的东西:

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的第一个主题位置进行排序。

编辑: 我试图用嵌套类型

更改de mapping
 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
         }
    ]
}

然后我在“thematics”上应用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

0 个答案:

没有答案