1

我有一种情况,我的产品被描述为几个,并且使用了以下结构:

{
    "defaultDescription" : "Default Description",
    "i18nDescription" : {
        "pt" : "Descrição Padrão",
        "de" : "Standard-Beschreibung"
    }
}

现在我有以下要求:按照优先语言列表(3 种语言)执行搜索。如果第一种语言不在 中i18nDescription,则仅使用第二种语言,如果第二种语言不存在,则仅使用第三种语言,否则与defaultDescription.

我的解决方案是这样的:

// suppose request comes with the following languages: en, de, pt
{
    "size":10,
    "fields" : ["defaultDescription", "i18nDescription.en^50", "i18nDescription.de^20", "i18nDescription.pt^10"],
    "query": {
        "multi_match" : { "query" : "default", "fields" : ["description","descriptions.fr-CA"] }
    }
}

但是这个解决方案只会按优先级语言对结果进行排序,我想做类似的事情:i18nDescription.en:search OR (i18nDescription.de:search AND _empty_:i18nDescription.en) OR (i18nDescription.pt:search AND _empty_:i18nDescription.en AND _empty_:i18nDescription.de) OR (description:search AND _empty_:i18nDescription.pt AND _empty_:i18nDescription.en AND _empty_:i18nDescription.de)

有没有办法表示这是一个 ElasticSearch 查询?

4

1 回答 1

2

稍微玩一下 bool 查询,我们可以达到预期的效果。

It basically needs to check if one field has the text and others (that are more important) are empty, so it would consider just the most important present field.

Query would be something similar to:

{
    "size":10,
    "query": {
        "bool" : {
            "should" : [ 
                {
                    "bool" : {
                        "must" : [
                            { "multi_match" : { "fields":["defaultDescription"], "query" : "default" } },
                            { "query_string" : { "query" : "+_missing_:i18nDescription.en +_missing_:i18nDescription.de +_missing_:i18nDescription.pt" } }
                        ]
                    }
                },
                {
                    "bool" : {
                        "must" : [
                            { "multi_match" : { "fields":["i18nDescription.pt"], "query" : "default" } },
                            { "query_string" : { "query" : "+_missing_:i18nDescription.en +_missing_:i18nDescription.de" } }
                        ]
                    }
                },
                {
                    "bool" : {
                        "must" : [
                            { "multi_match" : { "fields":["i18nDescription.de"], "query" : "default" } },
                            { "query_string" : { "query" : "+_missing_:i18nDescription.en" } }
                        ]
                    }
                },
                {
                    "bool" : {
                        "must" : [
                            { "multi_match" : { "fields":["i18nDescription.en"], "query" : "default" } }
                        ]
                    }
                } 
            ]
        }
    }
}
于 2013-08-29T17:34:18.233 回答