gpt4 book ai didi

java - 或者同时使用 "like"和 "line break"和 "case insensitive"从 java 查询 mongodb

转载 作者:行者123 更新时间:2023-12-01 18:14:43 24 4
gpt4 key购买 nike

这是我的 mongodb 集合 page_link_titles 中的一个文档的示例:

{
"_id" : ObjectId("553b11f30b81511d64152416"),
"id" : 36470831,
"linkTitles" : [
"Syrian civil war",
"Damascus",
"Geographic coordinate system",
"Bashar al-Assad",
"Al Jazeera English",
"Free Syrian Army",
...

"February 2012 Aleppo bombings",
"2012 Deir ez-Zor bombing",
"Aleppo University bombings"
]
}

我想找到linkTitles中文本的所有文档包含类似 '%term1%' 的短语或'%term2%'或(等等)。 term1 和 term2 两侧必须有换行符。例如查看 "Syrian civil war" 。如果term1 = "war"我希望此文档作为查询结果返回,但是如果 term1 = "yria"这是本文档中单词的一部分,不应返回。

这是我的java代码:

for (String term : segment.terms) {
DBObject clause1 = new BasicDBObject("linkTitles",
java.util.regex.Pattern.compile("\\b"
+ stprocess.singularize(term) + "\\b"));
or.add(clause1);
}

DBObject mongoQuery = new BasicDBObject("$or", or);
DBCursor cursor = pageLinks.find(mongoQuery);

在线:java.util.regex.Pattern.compile("\\b"+ stprocess.singularize(term) + "\\b"));我只假设换行。我不知道应该如何编写正则表达式来考虑我的所有条件:line break , case insensitive , like .

有什么想法吗?

最佳答案

可以使用正则表达式来实现您想要的效果。您还可以使用单个正则表达式,而不是使用 $or

我使用 shell 作为一个简单示例,并希望搜索 boxercat。首先插入测试数据:

db.test.drop()
db.test.insert([
{ "a" : "Boxer One" },
{ "a" : "A boxer dog" },
{ "a" : "A box shouldn't match" },
{ "a" : "should match BOXER" },
{ "a" : "wont match as this it the plural BOXERs" },
{ "a" : "also match on cat" }])

使用以下正则表达式,我们可以搜索所有术语:

                                       
      /(^|\b)(boxer|cat)(\b|$)/i       
       +---+ +-------+  +---+         
          |       |        |           
          |       |        |           
   Start or space |       Space or end 
                  |                    
              Search terms
                      

然后像这样查找:

db.test.find({a: /(^|\b)(boxer|cat)(\b|$)/i})

该查询将返回以下结果:

{ "_id" : ObjectId("555f18eee7b6d1b7e622de36"), "a" : "Boxer One" }
{ "_id" : ObjectId("555f18eee7b6d1b7e622de37"), "a" : "A boxer dog" }
{ "_id" : ObjectId("555f18eee7b6d1b7e622de39"), "a" : "should match BOXER" }
{ "_id" : ObjectId("555f18eee7b6d1b7e622de3b"), "a" : "also match on cat" }

在 Java 中,您可以像这样构建此查询:

StringBuilder singularizedTerms = new StringBuilder();
for (String term : terms) {
singularizedTerms.append("|").append(stprocess.singularize(term));
}
String regexPattern = format("(^|\\b)(%s)(\\b|$)", singularizedTerms.substring(1));
Pattern regex = Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE);

这种方法有两个问题。

  1. 会很慢它无法使用索引,因此将对集合进行全面扫描,如果您有 1000 万个文档,它将检查每个文档!

  2. 它不会匹配复数例如,它不会匹配包含“BOXERs”的文档,因为我们的正则表达式明确不允许部分匹配!

Text indexes支持这一点。使用索引将使操作更快以及匹配多个或单个值,例如:

db.test.createIndex( { a: "text" } )
db.test.find({ $text: { $search: "boxer cat"}})

{ "_id" : ObjectId("555f18eee7b6d1b7e622de3b"), "a" : "also match on cat" }
{ "_id" : ObjectId("555f18eee7b6d1b7e622de3a"), "a" : "wont match as this it the plural BOXERs" }
{ "_id" : ObjectId("555f18eee7b6d1b7e622de36"), "a" : "Boxer One" }
{ "_id" : ObjectId("555f18eee7b6d1b7e622de37"), "a" : "A boxer dog" }
{ "_id" : ObjectId("555f18eee7b6d1b7e622de39"), "a" : "should match BOXER" }

关于java - 或者同时使用 "like"和 "line break"和 "case insensitive"从 java 查询 mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30384539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com