Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
或者使用“从java查询mongodb”;例如;及;换行“;及;不区分大小写“;同时_Java_Mongodb_Line Breaks_Case Insensitive - Fatal编程技术网

或者使用“从java查询mongodb”;例如;及;换行“;及;不区分大小写“;同时

或者使用“从java查询mongodb”;例如;及;换行“;及;不区分大小写“;同时,java,mongodb,line-breaks,case-insensitive,Java,Mongodb,Line Breaks,Case Insensitive,这是我的mongodb收藏页面\链接\标题中的一个文档示例: { "_id" : ObjectId("553b11f30b81511d64152416"), "id" : 36470831, "linkTitles" : [ "Syrian civil war", "Damascus", "Geographic coordinate system", "Bashar al-Assad",

这是我的mongodb收藏页面\链接\标题中的一个文档示例:

{
    "_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"
    ]
}
我想找到所有文档,它们的
链接标题中的文本包含类似
'%term1%'
'%term2%'
或(依此类推)的短语。端子1和端子2的两侧必须有断线。例如,调查
“叙利亚内战”
。如果
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”)我只假设换行。我不知道该怎么写正则表达式来考虑我的所有条件:

有什么想法吗?

可以使用正则表达式实现您想要的结果。也可以使用单个正则表达式,而不是使用
$或

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

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);
这种方法有两个问题

  • 会很慢的 它不能使用索引,因此将对集合进行完整扫描,如果您有1000万个文档,它将检查每个文档

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

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

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

    可以使用正则表达式实现所需的功能。也可以使用单个正则表达式,而不是使用
    $或

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

    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);
    
    这种方法有两个问题

  • 会很慢的 它不能使用索引,因此将对集合进行完整扫描,如果您有1000万个文档,它将检查每个文档

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

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

    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中使用它?通过使用
    find
    $text
    操作符-请参阅:您提到的文本索引,我创建了它,但是我应该如何在java中使用它呢?通过使用
    find
    $text
    操作符-请参阅: