2017-02-15 70 views
-1

我有一個用例,我必須檢查一個文本,如果我收到的輸入包含任何300萬字符串我有。匹配大量字符串的搜索文本

我想正則表達式匹配,但一旦字符串列表越過50K表現非常糟糕

我這樣做在搜索列表

inText = java.util.regex.Pattern.compile("\\b" + findStr + "\\b", 
     java.util.regex.Pattern.CASE_INSENSITIVE).matcher(intext).replaceAll(repl); 

我明白,我們可以使用搜索索引中的每個詞像lucene,但我覺得那些主要是從預定義的文本搜索特定的文本,但我的用例是相反的,我需要發送一個大文本,並檢查是否有任何預先定義的字符串中有文字

回答

1

我想,你可以拿以相反的方式。您預定義的字符串是存儲在倒排索引中的文檔,並且您的傳入文本是查詢,您將針對您的文檔進行測試。由於預定義的字符串不會有太大的改變,所以它會非常高效。

我準備了一些Elasticsearch代碼,這將做的伎倆。

public void add(String string, String id) { 
     IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, id); 
     indexRequest.source(string); 
     index(INDEX, TYPE, id, string); 
    } 

    @Test 
    public void scoring() throws Exception { 
     // adding your predefined strings 
     add("{\"str\":\"string1\"}", "1"); 
     add("{\"str\":\"alice\"}", "2"); 
     add("{\"str\":\"bob\"}", "3"); 
     add("{\"str\":\"string2\"}", "4"); 
     add("{\"str\":\"melanie\"}", "5"); 
     add("{\"str\":\"moana\"}", "6"); 

     refresh(); // otherwise we would not anything 

     indexExists(INDEX); // verifies that index exists 
     ensureGreen(INDEX); // ensures cluster status is green 


     // querying your text separated by space, if the hits length is bigger than 0, you're good 
     SearchResponse searchResponse = client().prepareSearch(INDEX).setQuery(QueryBuilders.termsQuery("str", "string1", "string3", "melani")).execute().actionGet(); 
     SearchHit[] hits = searchResponse.getHits().getHits(); 

     assertThat(hits.length, equalTo(1)); 

     for (SearchHit hit: hits) { 
      System.out.println(hit.getSource()); 
     } 

    }