【问题标题】:Mongodb match query with priority on full match完全匹配优先的MongoDB匹配查询
【发布时间】:2021-04-06 03:46:44
【问题描述】:

我正在尝试对字段进行 MongoDB 正则表达式查询。我希望查询优先考虑一个完整匹配,如果它找到一个然后部分匹配。

使用MongoTemplate,我编写了一个查询,它可以正确执行搜索而没有任何优先级:

query.addCriteria(
    new Criteria().orOperator(
        Criteria.where("name").regex(".*" + freeText + ".*", "i"),
        Criteria.where("identifier").regex(".*" + freeText + ".*", "i")));

我找到了this 解决方案,这似乎是我正在寻找的。我怎样才能用MongoTemplate 重写这个?

【问题讨论】:

    标签: mongodb spring-boot mongotemplate


    【解决方案1】:

    你可以试试这个:https://docs.mongodb.com/manual/text-search/

    1. 为要搜索的字段创建“文本索引”。
    2. 按 { score: { $meta: "textScore" } 排序。 “文本搜索查询将计算每个文档的相关性分数,以指定文档与查询的匹配程度。”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-01
      • 2017-08-23
      • 2020-01-09
      • 1970-01-01
      • 2020-06-18
      • 2022-01-06
      • 2021-06-28
      • 1970-01-01
      相关资源
      最近更新 更多