如何使用功能分数查询更新已弃用的自定义分数查询?

时间:2014-10-16 10:04:33

标签: elasticsearch nest

我正在从elasticsearch 0.90.10和NEST 0.12.0升级到elasticsearch 1.3.4和NEST 1.1.2,我正在尝试用custom score query, which was deprecated in 0.90.4替换function score query

这是我的旧代码:

descriptor.Search<IndexedProduct>(s => s
    .Index(index)
    .From(((page ?? 1) - 1) * paging)
    .Size(paging)
    .Fields(f => f.ID, f => f.NumberOfSalesLastTwelveMonths, f => f.Description, f => f.itemIsInStock)
    .Query(qsc => qsc
        .CustomScore(sc => sc
            .Query(q => q
                .QueryString(qs => qs
                    .OnFieldsWithBoost(f => f
                        .Add(entry => entry.Description, 2.0)
                        .Add(entry => entry.LongDescription, 1.0)
                        .Add(entry => entry.ShortFamilyAndNumber, 4.0)
                        .Add(entry => entry.FamilyAndNumber, 4.0)
                        .Add(entry => entry.Suppliers, 1.5)
                        .Add(entry => entry.CleanSupplierReferences, 4.0)
                        .Add(entry => entry.SupplierReferences, 4.0)
                        .Add(entry => entry.SupplierBrands, 2.0)
                        .Add(entry => entry.Categories, 1.5)
                        .Add(entry => entry.ParameterValues, 1.0)
                        .Add(entry => entry.KeyWords, 2.0)
                        .Add(entry => entry.EANReferencesWMS, 4.0)
                    )
                    .Query(CleanUpText(EscapeSearchString(searchquery)))
                    .Operator(Operator.and)
                )
            )
            .Script("_score + ((_score > 0.99 ) ? doc['numberOfSalesLastTwelveMonths'].value : 0) + ((_score > 0.99 ) ? doc['itemIsInStock'].value : 0)")
        )
    ));

这是我的新代码:

descriptor.Search<IndexedProduct>(s => s
    .Index(index)
    .From(((page ?? 1) - 1) * paging)
    .Size(paging)
    .Fields(f => f.ID, f => f.NumberOfSalesLastTwelveMonths, f => f.Description, f => f.itemIsInStock)
    .Query(qsc => qsc
        .FunctionScore(fs => fs
            .Query(q => q
                .QueryString(qs => qs
                    .OnFieldsWithBoost(f => f
                        .Add(entry => entry.Description, 2.0)
                        .Add(entry => entry.LongDescription, 1.0)
                        .Add(entry => entry.ShortFamilyAndNumber, 4.0)
                        .Add(entry => entry.FamilyAndNumber, 4.0)
                        .Add(entry => entry.Suppliers, 1.5)
                        .Add(entry => entry.CleanSupplierReferences, 4.0)
                        .Add(entry => entry.SupplierReferences, 4.0)
                        .Add(entry => entry.SupplierBrands, 2.0)
                        .Add(entry => entry.Categories, 1.5)
                        .Add(entry => entry.ParameterValues, 1.0)
                        .Add(entry => entry.KeyWords, 2.0)
                        .Add(entry => entry.EANReferencesWMS, 4.0)
                    )
                    .Query(CleanUpText(EscapeSearchString(searchquery)))
                    .DefaultOperator(Operator.And)
                )
            )
            .ScriptScore(sc => sc
                .Script("_score + ((_score > 0.99 ) ? doc['numberOfSalesLastTwelveMonths'].value : 0) + ((_score > 0.99 ) ? doc['itemIsInStock'].value : 0)")
            )
        )
    )
);

新代码失败并出现SearchParseException,这是查询(来自.log文件):

[{
    "from": 0,
    "size": 10,
    "fields": ["iD", "numberOfSalesLastTwelveMonths", "description", "itemIsInStock"],
    "query": {
        "function_score": {
            "query": {
                "query_string": {
                    "query": "*",
                    "fields": ["description^2", "longDescription^1", "shortFamilyAndNumber^4", "familyAndNumber^4", "suppliers^1.5", "cleanSupplierReferences^4", "supplierReferences^4", "supplierBrands^2", "categories^1.5", "parameterValues^1", "keyWords^2", "eANReferencesWMS^4"],
                    "default_operator": "and"
                }
            },
            "script_score": {
                "script": "_score + ((_score > 0.99 ) ? doc['numberOfSalesLastTwelveMonths'].value : 0) + ((_score > 0.99 ) ? doc['itemIsInStock'].value : 0)"
            }
        }
    }
}]

完整日志条目:

[STARTED]: Failed to execute [org.elasticsearch.action.search.SearchRequest@7a87107e] lastShard [true]
org.elasticsearch.search.SearchParseException: [sta_products_public_nl_635490239970034081][0]: from[0],size[10]: Parse Failure [Failed to parse source [{"from":0,"size":10,"fields":["iD","numberOfSalesLastTwelveMonths","description","itemIsInStock"],"query":{"function_score":{"query":{"query_string":{"query":"*","fields":["description^2","longDescription^1","shortFamilyAndNumber^4","familyAndNumber^4","suppliers^1.5","cleanSupplierReferences^4","supplierReferences^4","supplierBrands^2","categories^1.5","parameterValues^1","keyWords^2","eANReferencesWMS^4"],"default_operator":"and"}},"script_score":{"script":"_score + ((_score > 0.99 ) ? doc['numberOfSalesLastTwelveMonths'].value : 0) + ((_score > 0.99 ) ? doc['itemIsInStock'].value : 0)"}}}}]]
        at org.elasticsearch.search.SearchService.parseSource(SearchService.java:660)
        at org.elasticsearch.search.SearchService.createContext(SearchService.java:516)
        at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:488)
        at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:257)
        at org.elasticsearch.search.action.SearchServiceTransportAction$5.call(SearchServiceTransportAction.java:206)
        at org.elasticsearch.search.action.SearchServiceTransportAction$5.call(SearchServiceTransportAction.java:203)
        at org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:517)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:744)
Caused by: org.elasticsearch.index.query.QueryParsingException: [sta_products_public_nl_635490239970034081] script_score the script could not be loaded
        at org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionParser.parse(ScriptScoreFunctionParser.java:97)
        at org.elasticsearch.index.query.functionscore.FunctionScoreQueryParser.parse(FunctionScoreQueryParser.java:128)
        at org.elasticsearch.index.query.QueryParseContext.parseInnerQuery(QueryParseContext.java:239)
        at org.elasticsearch.index.query.IndexQueryParserService.innerParse(IndexQueryParserService.java:342)
        at org.elasticsearch.index.query.IndexQueryParserService.parse(IndexQueryParserService.java:268)
        at org.elasticsearch.index.query.IndexQueryParserService.parse(IndexQueryParserService.java:263)
        at org.elasticsearch.search.query.QueryParseElement.parse(QueryParseElement.java:33)
        at org.elasticsearch.search.SearchService.parseSource(SearchService.java:644)
        ... 9 more
Caused by: org.elasticsearch.script.ScriptException: dynamic scripting for [mvel] disabled
        at org.elasticsearch.script.ScriptService.verifyDynamicScripting(ScriptService.java:374)
        at org.elasticsearch.script.ScriptService.compile(ScriptService.java:335)
        at org.elasticsearch.script.ScriptService.search(ScriptService.java:509)
        at org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionParser.parse(ScriptScoreFunctionParser.java:94)
        ... 16 more

如果我省略这样的FunctionScore,它可以正常工作:

descriptor.Search<IndexedProduct>(s => s
    .Index(index)
    .From(((page ?? 1) - 1) * paging)
    .Size(paging)
    .Fields(f => f.ID, f => f.NumberOfSalesLastTwelveMonths, f => f.Description, f => f.itemIsInStock)
    .Query(q => q
        .QueryString(qs => qs
            .OnFieldsWithBoost(f => f
                .Add(entry => entry.Description, 2.0)
                .Add(entry => entry.LongDescription, 1.0)
                .Add(entry => entry.ShortFamilyAndNumber, 4.0)
                .Add(entry => entry.FamilyAndNumber, 4.0)
                .Add(entry => entry.Suppliers, 1.5)
                .Add(entry => entry.CleanSupplierReferences, 4.0)
                .Add(entry => entry.SupplierReferences, 4.0)
                .Add(entry => entry.SupplierBrands, 2.0)
                .Add(entry => entry.Categories, 1.5)
                .Add(entry => entry.ParameterValues, 1.0)
                .Add(entry => entry.KeyWords, 2.0)
                .Add(entry => entry.EANReferencesWMS, 4.0)
            )
            .Query(CleanUpText(EscapeSearchString(searchquery)))
            .DefaultOperator(Operator.And)
        )
    )

这是查询(通过Wireshark捕获,我无法将日志/慢速日志配置为记录所有查询):

{
    "index": "sta_products_public_nl",
    "type": "indexedproduct"
}
{
    "from": 0,
    "size": 10,
    "fields": ["iD", "numberOfSalesLastTwelveMonths", "description", "itemIsInStock"],
    "query": {
        "query_string": {
            "query": "*",
            "fields": ["description^2", "longDescription^1", "shortFamilyAndNumber^4", "familyAndNumber^4", "suppliers^1.5", "cleanSupplierReferences^4", "supplierReferences^4", "supplierBrands^2", "categories^1.5", "parameterValues^1", "keyWords^2", "eANReferencesWMS^4"],
            "default_operator": "and"
        }
    }
}

我做错了什么?

2 个答案:

答案 0 :(得分:2)

好的,这里有一个很大的提示(应该立即点击我说实话:))

Caused by: org.elasticsearch.script.ScriptException: dynamic scripting for [mvel] disabled

默认情况下,Elasticsearch用于启用脚本编写,但由于人们向全世界公开了弹性搜索(相当于向世界公开数据库),他们对此有所了解。

我们发布了一篇博文,详细介绍了如何重新启用脚本:

http://www.elasticsearch.org/blog/scripting/

答案 1 :(得分:1)

添加

script.disable_dynamic: false 

总是没有解决问题,我已经在我的elasticsearch.yml中已经有了这个但我继续收到错误输出:

Caused by: org.elasticsearch.script.ScriptException: dynamic scripting for [mvel] disabled
    at org.elasticsearch.script.ScriptService.verifyDynamicScripting(ScriptService.java:374)
    at org.elasticsearch.script.ScriptService.compile(ScriptService.java:335)
    at org.elasticsearch.script.ScriptService.search(ScriptService.java:509)
    at org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionParser.parse(ScriptScoreFunctionParser.java:94)