Python上的Elasticsearch DSL无法生成分数

我有一个Elasticsearch数据库,其中包含几个可以包含名称信息的字段,并且正在尝试像这样进行搜索:

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search

client = Elasticsearch()
s = Search(using=client,index="names")
query = 'smith'
fields = ['name1','name2']

results = s.query("multi_match",query=query,fields=fields,fuzziness='AUTO')

for hit in results.scan():
    print(hit.meta.score)

结果是:

None
None
None
...

但是,如果我手动构建它:

results = client.search(index="names",body={"size": 100,"query":{
        "multi_match": {
            "query": query,"fields": fields,"fuzziness": 'AUTO'
        }
    }
})

我的结果是:

{'_index': 'names','_type': 'Name1','_id': '1MtYSW4BXryTHXwQ1xBS','_score': 14.226202,'_source': {...}
{'_index': 'names','_id': 'N8tZSW4BXryTHXwQHBfw','_id': '8MtZSW4BXryTHXwQeR-i','_source': {...}

如果可能的话,我宁愿使用elasticsearch-dsl,但我需要分数信息。

lovekiesa 回答:Python上的Elasticsearch DSL无法生成分数

尝试一下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">

<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />

<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="70dp">
<Button
    android:id="@+id/btnStart"
    android:layout_width="130dp"
    android:layout_height="130dp"
    android:onClick="socorro"
    android:background="@drawable/rec"

    />

</LinearLayout>

</RelativeLayout>
,

该代码的第一个版本不等同于该代码的第二个版本。第一个版本不执行查询,而是使用Scroll API(elasticsearch.helpers.scan)。

Search.query()方法将构建或扩展搜索对象,而不是将查询发送到elasticsearch。因此,以下代码行具有误导性:

results = s.query("multi_match",query=query,fields=fields,fuzziness='AUTO')

应该是这样的:

# execute() added at the end
results = s.query("multi_match",fuzziness='AUTO').execute()
# scan() removed 
for hit in results:
    print(hit.meta.score)
,

尝试像这样:

using System;
using Microsoft.Management.Infrastructure;
using Microsoft.Management.Infrastructure.Options;
using System.Linq;

namespace MachineTimeStamps
{
    class Program
    {
        /// <summary>
        /// Print the system Uptime and Last Bootup Time (using Win32_OperatingSystem LocalDateTime & LastBootUpTime properties).
        /// </summary>
        public static void Main(string[] args)
        {
            var uptime = GetSystemUptime("COMPUTER_NAME");

            if (!uptime.HasValue)
            {
                throw new NullReferenceException("GetSystemUptime() response was null.");
            }

            var lastBootUpTime = GetSystemLastBootUpTime("COMPUTER_NAME");

            if (!lastBootUpTime.HasValue)
            {
                throw new NullReferenceException("GetSystemLastBootUpTime() response was null.");
            }

            Console.WriteLine($"Uptime: {uptime}");
            Console.WriteLine($"BootupTime: {lastBootUpTime}");
            Console.ReadKey();
        }

        /// <summary>
        /// Retrieves the duration (TimeSpan) since the system was last started.
        /// Note: can be used on a local or a remote machine.
        /// </summary>
        /// <param name="computerName">Name of computer on network to retrieve uptime for</param>
        /// <returns>WMI Win32_OperatingSystem LocalDateTime - LastBootUpTime</returns>
        private static TimeSpan? GetSystemUptime(string computerName)
        {
            string namespaceName = @"root\cimv2";
            string queryDialect = "WQL";

            DComSessionOptions SessionOptions = new DComSessionOptions();
            SessionOptions.Impersonation = ImpersonationType.Impersonate;

            CimSession session = CimSession.Create(computerName,SessionOptions);

            string query = "SELECT * FROM Win32_OperatingSystem";
            var cimInstances = session.QueryInstances(namespaceName,queryDialect,query);

            if (cimInstances.Any())
            {
                var cimInstance = cimInstances.First();
                var lastBootUpTime = Convert.ToDateTime(cimInstance.CimInstanceProperties["LastBootUpTime"].Value);
                var localDateTime = Convert.ToDateTime(cimInstance.CimInstanceProperties["LocalDateTime"].Value);

                var uptime = localDateTime - lastBootUpTime;
                return uptime;
            }

            return null;
        }

        /// <summary>
        /// Retrieves the last boot up time from a system.
        /// Note: can be used on a local or a remote machine.
        /// </summary>
        /// <param name="computerName">Name of computer on network to retrieve last bootup time from</param>
        /// <returns>WMI Win32_OperatingSystem LastBootUpTime</returns>
        private static DateTime? GetSystemLastBootUpTime(string computerName)
        {
            string namespaceName = @"root\cimv2";
            string queryDialect = "WQL";

            DComSessionOptions SessionOptions = new DComSessionOptions();
            SessionOptions.Impersonation = ImpersonationType.Impersonate;

            CimSession session = CimSession.Create(computerName,query);

            if (cimInstances.Any())
            {
                var lastBootUpTime = Convert.ToDateTime(cimInstances.First().CimInstanceProperties["LastBootUpTime"].Value);
                return lastBootUpTime;
            }

            return null;
        }
    }
}
,

试试这个:

s = s.params(preserve_order=True).sort("_score")

然后 scan 可以返回 score

Scan 默认会用 ['_doc'] 填充排序,这就是它不会返回分数的原因。

本文链接:https://www.f2er.com/3136416.html

大家都在问