package com.google.sps.servlets;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.sps.utilities.CommonUtils;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Servlet that handles search queries and addition of event keywords to index. */
@WebServlet("/search-data")
public class SearchDataServlet extends HttpServlet {
  private ListMultimap<String, String> keywordToEventIds = ArrayListMultimap.create();
  private static final String KEYWORD = "keyword";
  private static final String EVENT_ID = "event-id";
  private static final String DESCRIPTION = "description";
  
  /**
   * Return search results for the keyword specified in the request.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws IOException if Input/Output error occurs
   */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String keyword = request.getParameter(KEYWORD);
    if (keyword == null) {
      response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("No keyword specified."));
      return;
    }

    response.setContentType("application/json;");
    response.getWriter().println(CommonUtils.convertToJson(keywordToEventIds.get(keyword)));
  }

  /**
   * Add keywords in the description specified to the index with mapping to the event id.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws IOException if Input/Output error occurs
   */
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String eventId = request.getParameter(EVENT_ID);
    if (eventId == null) {
      response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("No event ID specified."));
      return;
    }

    String description = CommonUtils.getParameter(request, DESCRIPTION, /* defaultValue= */ "");
    String[] keywordsInDescription = description.split("[^a-zA-Z0-9']+");
    addKeywordsToIndex(eventId, keywordsInDescription);

    response.sendRedirect("/events-feed.html");
  }

  /** Add keywords to the index with mapping to given event ID. */
  private void addKeywordsToIndex(String eventId, String[] keywords) {
    for (String keyword : keywords) {
      keywordToEventIds.put(keyword, eventId);
    }
  }
}

@@ -152,29 +152,117 @@
 sps.
-utilities.CommonUtils
+store.SearchStore;%0Aimport com.google.sps.utilities.CommonUtils;%0Aimport com.google.sps.utilities.KeywordHelper
 ;%0Aim
@@ -643,245 +643,46 @@
 ate 
-ListMultimap%3CString, String%3E keywordToEventIds = ArrayListMultimap.create();%0A  private static final String KEYWORD = %22keyword%22;%0A  private static final String EVENT_ID = %22event-id%22;%0A  private static final String DESCRIPTION = %22description
+static final String KEYWORD = %22keyword
 %22;%0A 
@@ -1323,1107 +1323,81 @@
 son(
-keywordToEventIds.get(keyword)));%0A  %7D%0A%0A  /**%0A   * Add keywords in the description specified to the index with mapping to the event id.%0A   *%0A   * @param request servlet request%0A   * @param response servlet response%0A   * @throws IOException if Input/Output error occurs%0A   */%0A  @Override%0A  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException %7B%0A    String eventId = request.getParameter(EVENT_ID);%0A    if (eventId == null) %7B%0A      response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format(%22No event ID specified.%22));%0A      return;%0A    %7D%0A%0A    String description = CommonUtils.getParameter(request, DESCRIPTION, /* defaultValue= */ %22%22);%0A    String%5B%5D keywordsInDescription = description.split(%22%5B%5Ea-zA-Z0-9'%5D+%22);%0A    addKeywordsToIndex(eventId, keywordsInDescription);%0A%0A    response.sendRedirect(%22/events-feed.html%22);%0A  %7D%0A%0A  /** Add keywords to the index with mapping to given event ID. */%0A  private void addKeywordsToIndex(String eventId, String%5B%5D keywords) %7B%0A    for (String keyword : keywords) %7B%0A      keywordToEventIds.put(keyword, eventId);%0A    %7D
+new SearchStore(KeywordHelper.getInstance()).getSearchResults(keyword)));
 %0A  %7D

Remove insertion functionality from search servlet.