小编Tho*_*mas的帖子

使用C#排序的MongoDB文本搜索

我正在尝试在C#中使用MongoDB实现文本搜索.

该文档未涵盖有关如何在C#中对文本搜索结果进行排序的任何内容.

我有一个标签列表,用空格分隔,以匹配.
如果我提供像"Tag1 Tag2"这样的字符串,我希望按以下顺序提供结果:

  1. 任何有'Tag1'和'Tag2'的东西,接着是
  2. 任何只有'Tag1'的东西,接着是
  3. 任何只有'Tag2'的东西

我一直试图拼凑一些代码:

var F = Builders<MongoPost>.Filter.Text(Tags);
var S = Builders<MongoPost>.Sort.MetaTextScore("textScore");
return Mongo.Driver.Find(F).Sort(S).ToListAsync().Result;
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

{"QueryFailure标志为真(响应为{\"$ err \":\"无法规范化查询:BadValue必须对所有$ meta排序键都有$ meta投影\",\"code \":17287}). "}

没有适当的文档来解决此错误...

然后我在这个网站上找到了以下代码:

var pipeline = new List<BsonDocument>
{
    BsonSerializer.Deserialize<BsonDocument>("{ $match: { $text: { $search: \"" + Tags + "\" } } }"),
    BsonSerializer.Deserialize<BsonDocument>("{ $sort: { score: { $meta: \"textScore\" } } }")
};

var R = Mongo.Driver.AggregateAsync(new BsonDocumentStagePipelineDefinition<MongoPost, MongoPost>(pipeline)).Result;
return R.ToListAsync().Result;
Run Code Online (Sandbox Code Playgroud)

至少运行没有错误,但我试图将样式放在顶部的样式中,就像你可以为API的其余部分做的那样,而不必回到必须解析的控制台样式文本字符串每次执行.另外,我需要为搜索添加更多标准,因此这种语法对我来说不实用.

我错过了哪些适当的文件?如果没有,有没有人知道如何在顶部的风格实现这一点?

c# sorting mongodb

1
推荐指数
1
解决办法
2509
查看次数

C#到F#的双精度浮点型隐式转换

我有以下C#函数:

public (double? Average, int? Count) AverageQuotes(Candle.Intervals Interval, DateTime From, DateTime To)
Run Code Online (Sandbox Code Playgroud)

我在F#中获得数据:

let struct (average, count) = db.AverageQuotes(previous, time, time + timespan)
Run Code Online (Sandbox Code Playgroud)

问题在于,Average将C#留为两倍吗?并以Nullable的形式到达F#,因此在某处发生了double-> float转换。

如何使结果保持两倍?

f#

1
推荐指数
1
解决办法
43
查看次数

Having trouble converting some C# code to F# when having to do counts

I have the following C# code:

(no need to understand the details of it, it's just to illustrate the question)

            long VolumeBeforePrice = 0;
            long Volume = 0;
            var ContractsCount = 0.0;

            var VolumeRequested = Candle.ConvertVolumes(MinVolume);

            // go through all entries
            foreach (var B in Entries)
            {
                // can we add the whole block?
                if (Volume + B.VolumeUSD <= VolumeRequested)
                {
                    // yes, add the block and calculate the number of contracts
                    Volume += B.VolumeUSD;
                    ContractsCount += B.VolumeUSD / B.PriceUSD; …
Run Code Online (Sandbox Code Playgroud)

f#

1
推荐指数
1
解决办法
44
查看次数

在Python中,我想使用HTTP而不是HTTPS进行其余请求,包似乎不同意

我尝试过使用请求:

url = 'http://bot'    # this is a local service
import requests

r = requests.get(url)
Run Code Online (Sandbox Code Playgroud)

我得到

requests.exceptions.SSLError:HTTPSConnectionPool(主机='bot',端口=443)

我指定了 HTTP,而不是 HTTPS,但它尝试使用 SSL 进行连接。

所以,我尝试了httplib2:

url = 'http://bot'    # this is a local service
response, content = http.request(url, "GET")
Run Code Online (Sandbox Code Playgroud)

我得到:

ssl.SSLCertVerificationError:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败:无法获取本地颁发者证书(_ssl.c:1076)

我不知道为什么有人对 HTTPS 如此痴迷,但我不想要它。它是一个正在开发中运行的本地服务,并且是纯 HTTP。在 url 中添加“:80”不会改变任何内容。

当使用CURL或C#访问同一个服务时,通过HTTP,没有问题。

我如何告诉 python 库我想要一个纯 HTTP 连接而不是其他?

python httplib2 python-requests

1
推荐指数
1
解决办法
3552
查看次数

在 F# 中组合 2 个列表

我有 2 个带有坐标的列表(每个轴一个),我正在用它们制作正方形:

// create all combinations
let squares = List<int * int * int * int>()
seqX
|> Seq.pairwise
|> Seq.iter (fun (x1, x2) ->
    seqY
    |> Seq.pairwise
    |> Seq.iter (fun (y1, y2) -> squares.Add(x1, y1, x2, y2))
)
Run Code Online (Sandbox Code Playgroud)

有没有办法,使用集合函数来做到这一点?我不能使用 Seq.map 因为输出与迭代总数不匹配

|> Seq.map (fun (y1, y2) -> (x1, y1, x2, y2))
Run Code Online (Sandbox Code Playgroud)

不管用

f#

1
推荐指数
1
解决办法
60
查看次数

为什么我在 F# 的匹配语句中收到“值未使用”警告

我有以下代码:

   try
        let s = orderId.Split('|')
        match (s.[0], s.[1]) with
        | "G", coreGuid ->  OrderClass.Grid
        | "C", coreGuid ->  OrderClass.Close
        | _, _          ->  OrderClass.External
    with _ ->
        OrderClass.External
Run Code Online (Sandbox Code Playgroud)

它采用“letter|guid”形式的字符串,然后尝试匹配它。我想要实现的逻辑是:

if s.[0] = "G" && s.[1] = coreGuid   for the first line, and
if s.[0] = "C" && s.[1] = coreGuid   for the second line
Run Code Online (Sandbox Code Playgroud)

但是我的 IDE(Rider 2020.1 MacOs)给了我这个警告:

在此处输入图片说明

我不明白为什么?

f# rider

1
推荐指数
1
解决办法
73
查看次数

在 F# 中,集合函数可以直接用作它们支持的类型的方法吗?

这是一个纯粹出于好奇的问题:

假设我有这个对象:

let a = [1; 2; 3; 4]
Run Code Online (Sandbox Code Playgroud)

我可以通过以下方式获得最大值:

a |> List.max
List.max a
Run Code Online (Sandbox Code Playgroud)

但是有什么可以阻止将其设置为:

a.max
Run Code Online (Sandbox Code Playgroud)

同样,如果我想要一个排序列表,然后取前 2 个结果:

a |> List.sort |> List.truncate 2
Run Code Online (Sandbox Code Playgroud)

但是关于:

a.sort.truncate(2)
Run Code Online (Sandbox Code Playgroud)

我知道这不是 F# 的惯用语,但是既然库在那里,为什么也不允许这样做呢?我很好奇是否有明确的原因不启用此选项。

f#

1
推荐指数
1
解决办法
63
查看次数

对类型中的静态字典感到困惑,在 F# 中

使用这种类型:

type A =
    {
        S: string
    }

    static member private l = Dictionary<string, A>()
    static member add s = A.l.[s] <- { S=s }
    static member list () = l.Values
Run Code Online (Sandbox Code Playgroud)

如果我做:

A.add "hello"
A.add "world"
Run Code Online (Sandbox Code Playgroud)

我希望 A.list() 返回一些东西,因为字典是静态的,但它返回一个空列表。这是为什么?

为了澄清我正在尝试做的事情:我希望能够将类型 A 的对象注册到附加到类型本身的静态字典中,因为它会使对象存储库“自包含”在类型中, 在某种方式。

f#

1
推荐指数
1
解决办法
48
查看次数

F# 中的类型与模块

关于类型中的静态字典的困惑的答案,在 F# 中完成了一个建议:and just in general: try to use fewer classes and more modules and functions; they're more idiomatic in F# and lead to fewer problems in general

这是一个很好的观点,但我 30 年的 OO 只是还不想放弃课程(尽管当我们离开 C 时,我疯狂地与 C++ 作斗争......)

所以让我们拿一个实际的现实世界对象:

type Currency =
    {
        Ticker: string
        Symbol: char
    }

and MarginBracket =
    {
        MinSize:           decimal
        MaxSize:           decimal
        Leverage:          int
        InitialMargin:     decimal
        MaintenanceMargin: decimal
    }

and Instrument =
    {
        Ticker:             string
        QuantityTickSize:   int
        PriceTickSize:      int
        BaseCurrency:       Currency
        QuoteCurrency:      Currency …
Run Code Online (Sandbox Code Playgroud)

f#

1
推荐指数
1
解决办法
65
查看次数

“创建或替换触发器”在 AWS RDS/Postgres 上不可用

我有以下内容:

CREATE OR REPLACE TRIGGER trigger_commands
    AFTER INSERT ON commands
    FOR EACH ROW EXECUTE PROCEDURE on_commands_change();
Run Code Online (Sandbox Code Playgroud)

它可以在本地 Postgres 实例上运行,但在 AWS 上失败:

[42601] 错误:“TRIGGER”处或附近的语法错误

但是,如果我删除“或替换”部分:

CREATE TRIGGER trigger_commands
    AFTER INSERT ON commands
    FOR EACH ROW EXECUTE PROCEDURE on_commands_change();
Run Code Online (Sandbox Code Playgroud)

然后 RDS 接受它。这是为什么?每次代码重新启动时更新触发器的最佳方法是什么?

postgresql amazon-web-services amazon-rds

1
推荐指数
1
解决办法
838
查看次数