title: Phrase searching in an inverted index
body: <p>I am implementing a very rudimentary inverted index, and I am having trouble implementing a phrase searching method. </p>
  
  <p>I have the following structure: </p>
  
  <p>InvertedIndex.java: here I have a data structure:</p>
  
  <pre><code>private Map&lt;String, ArrayList&lt;Postings&gt;&gt; index = new HashMap&lt;String, ArrayList&lt;Postings&gt;&gt;(); 
  </code></pre>
  
  <p>where I store a word and a Posting list with all of the docId's and associated term positions in a document. </p>
  
  <p>My Postings.java class has the following structure: </p>
  
  <pre><code>private Map&lt;String, ArrayList&lt;Integer&gt;&gt; postings; 
  </code></pre>
  
  <p>I have getters and setters for all of these data structures, so I am not including them because it would be too much for this post. 
  The string is the docId, and the Arraylist holds all positions in a document for a word.</p>
  
  <p>I have an class where I am implementing the following method to search by phrase: </p>
  
  <pre><code>    public ArrayList&lt;String&gt; searchByPhrase(String...terms){
          if (terms == null || terms.length &lt; 2){
              return null; 
          }
  
          ArrayList&lt;String&gt; documents = new ArrayList&lt;String&gt;(); 
  
          for (int i = 0; i &lt; terms.length; i++){
              ArrayList&lt;Postings&gt; postings1 = index.getPostings(terms[i]);
              if ((i + 1) &lt; terms.length){
                  ArrayList&lt;Postings&gt; postings2 = index.getPostings(terms[i+1]);
  
                  int smaller = 0; 
                  if (postings2.size() &lt; postings1.size()){
                      smaller = postings2.size(); 
                  }
                  else {
                      smaller = postings1.size(); 
                  }
  
                  for (int j = 0; j &lt; smaller; j++){
                          Postings p1 = postings1.get(j); 
                          Postings p2 = postings2.get(j); 
                          if (p1.containsID(p2.getDocId())){
                              System.out.println("FOUND MATCHING DOC");
                              //Do position checking in here
                          }   
                  }
              }
  
          }
  
  
          return documents; 
      }
  </code></pre>
  
  <p>I know that within this method I have to check so that positions are within one spot away from each other. I have not yet implemented that because I want to first be able to find documents that are the same (which this is currently not doing). When I run this I get nothing back, and I have various documents that I know share words. </p>
  
  <p>I want this method to be able to search for terms of various sizes ("hello world", "Thank you so much for your help", etc....). I feel like I am overcomplicating this, but I am very lost as to how to tackle it. Any suggestions would be appreciated. </p>