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

OR Query mongodb from java with "like" and "line break" and "case insensitive" at the same time

这是我 mongodb collection 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);

In line: java.util.regex.Pattern.compile("\b"+ stprocess.singularize(term) + "\b")); 我只假设换行。我不知道我应该如何编写正则表达式来考虑我的所有条件:, 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})

该查询将 return 得到以下结果:

{ "_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" }