@Test public void testExecuteWithNOTKeyword() { final JSONObject request = new JSONObject(); final JSONObject query = new JSONObject(); query.put("keywords", "NOT BACON"); request.put("query", query); final FullTextQuery ftq = context.mock(FullTextQuery.class); final List<Long> results = new ArrayList<Long>(); context.checking(new Expectations() { { oneOf(builder).buildQueryFromNativeSearchString( "+content:(" + Activity.CONSTANT_KEYWORD_IN_EVERY_ACTIVITY_CONTENT + " NOT BACON) "); will(returnValue(ftq)); oneOf(ftq).getResultList(); will(returnValue(results)); oneOf(builder).setPaging(ftq, 0, MAX_RESULTS); } }); assertSame(results, sut.fetch(request, 0L)); context.assertIsSatisfied(); }
@Override @SuppressWarnings("unchecked") public List<Long> fetch(final JSONObject inRequest, final Long userEntityId) { JSONObject jsonQuery = inRequest.getJSONObject("query"); Boolean hasTerm = false; for (String term : requestToField.keySet()) { if (jsonQuery.containsKey(term)) { hasTerm = true; break; } } if (!hasTerm && (!jsonQuery.containsKey("sortBy") || (jsonQuery.containsKey("sortBy") && jsonQuery.getString( "sortBy").equals("date")))) { log.debug("No search term found"); return null; } String luceneQuery = ""; FullTextQuery query = null; for (Entry<String, String> entry : requestToField.entrySet()) { String req = entry.getKey(); if (jsonQuery.containsKey(req)) { String searchWord = jsonQuery.getString(req); searchWord = searchWord.replace(":", ""); if (transformers.containsKey(req)) { searchWord = (String) transformers.get(req).transform(jsonQuery, userEntityId); } if (req.toLowerCase().equals("keywords")) { if (searchWord.contains("NOT ") || searchWord.contains("!") || searchWord.contains("-")) { log.info("User is querying for activity content with:(" + searchWord + ") and seems to be using a NOT/!/- component. Lucene doesn't allow " + "NOT queries with one component, so I'll add a constant keyword that " + "I know is present: " + Activity.CONSTANT_KEYWORD_IN_EVERY_ACTIVITY_CONTENT); luceneQuery += "+" + entry.getValue() + ":(" + Activity.CONSTANT_KEYWORD_IN_EVERY_ACTIVITY_CONTENT + " " + searchWord + ") "; } else { luceneQuery += "+" + entry.getValue() + ":(" + searchWord + ") "; } } else { luceneQuery += "+" + entry.getValue() + ":(" + searchWord + ") "; } } } if (luceneQuery.length() == 0) { log.debug("Returning all activity"); query = unstemmedRequestBuilder.buildQueryFromNativeSearchString("_hibernate_class:" + "org.eurekastreams.server.domain.stream.Activity"); } else { try { query = searchRequestBuilder.buildQueryFromNativeSearchString(luceneQuery); } catch (Exception ex) { return new ArrayList<Long>(); } } if (jsonQuery.containsKey("sortBy")) { query.setSort(new Sort(new SortField(jsonQuery.getString("sortBy"), true))); } if (log.isDebugEnabled()) { log.debug("Native Lucene Query: " + query.toString()); } searchRequestBuilder.setPaging(query, 0, maxResults); List<Long> activityIds = query.getResultList(); if (log.isInfoEnabled()) { log.info("Found " + activityIds.size() + " activities"); } return activityIds; }
LuceneDataSource implements SortedDataSource { @Override @SuppressWarnings("unchecked") public List<Long> fetch(final JSONObject inRequest, final Long userEntityId) { JSONObject jsonQuery = inRequest.getJSONObject("query"); Boolean hasTerm = false; for (String term : requestToField.keySet()) { if (jsonQuery.containsKey(term)) { hasTerm = true; break; } } if (!hasTerm && (!jsonQuery.containsKey("sortBy") || (jsonQuery.containsKey("sortBy") && jsonQuery.getString( "sortBy").equals("date")))) { log.debug("No search term found"); return null; } String luceneQuery = ""; FullTextQuery query = null; for (Entry<String, String> entry : requestToField.entrySet()) { String req = entry.getKey(); if (jsonQuery.containsKey(req)) { String searchWord = jsonQuery.getString(req); searchWord = searchWord.replace(":", ""); if (transformers.containsKey(req)) { searchWord = (String) transformers.get(req).transform(jsonQuery, userEntityId); } if (req.toLowerCase().equals("keywords")) { if (searchWord.contains("NOT ") || searchWord.contains("!") || searchWord.contains("-")) { log.info("User is querying for activity content with:(" + searchWord + ") and seems to be using a NOT/!/- component. Lucene doesn't allow " + "NOT queries with one component, so I'll add a constant keyword that " + "I know is present: " + Activity.CONSTANT_KEYWORD_IN_EVERY_ACTIVITY_CONTENT); luceneQuery += "+" + entry.getValue() + ":(" + Activity.CONSTANT_KEYWORD_IN_EVERY_ACTIVITY_CONTENT + " " + searchWord + ") "; } else { luceneQuery += "+" + entry.getValue() + ":(" + searchWord + ") "; } } else { luceneQuery += "+" + entry.getValue() + ":(" + searchWord + ") "; } } } if (luceneQuery.length() == 0) { log.debug("Returning all activity"); query = unstemmedRequestBuilder.buildQueryFromNativeSearchString("_hibernate_class:" + "org.eurekastreams.server.domain.stream.Activity"); } else { try { query = searchRequestBuilder.buildQueryFromNativeSearchString(luceneQuery); } catch (Exception ex) { return new ArrayList<Long>(); } } if (jsonQuery.containsKey("sortBy")) { query.setSort(new Sort(new SortField(jsonQuery.getString("sortBy"), true))); } if (log.isDebugEnabled()) { log.debug("Native Lucene Query: " + query.toString()); } searchRequestBuilder.setPaging(query, 0, maxResults); List<Long> activityIds = query.getResultList(); if (log.isInfoEnabled()) { log.info("Found " + activityIds.size() + " activities"); } return activityIds; } }
LuceneDataSource implements SortedDataSource { @Override @SuppressWarnings("unchecked") public List<Long> fetch(final JSONObject inRequest, final Long userEntityId) { JSONObject jsonQuery = inRequest.getJSONObject("query"); Boolean hasTerm = false; for (String term : requestToField.keySet()) { if (jsonQuery.containsKey(term)) { hasTerm = true; break; } } if (!hasTerm && (!jsonQuery.containsKey("sortBy") || (jsonQuery.containsKey("sortBy") && jsonQuery.getString( "sortBy").equals("date")))) { log.debug("No search term found"); return null; } String luceneQuery = ""; FullTextQuery query = null; for (Entry<String, String> entry : requestToField.entrySet()) { String req = entry.getKey(); if (jsonQuery.containsKey(req)) { String searchWord = jsonQuery.getString(req); searchWord = searchWord.replace(":", ""); if (transformers.containsKey(req)) { searchWord = (String) transformers.get(req).transform(jsonQuery, userEntityId); } if (req.toLowerCase().equals("keywords")) { if (searchWord.contains("NOT ") || searchWord.contains("!") || searchWord.contains("-")) { log.info("User is querying for activity content with:(" + searchWord + ") and seems to be using a NOT/!/- component. Lucene doesn't allow " + "NOT queries with one component, so I'll add a constant keyword that " + "I know is present: " + Activity.CONSTANT_KEYWORD_IN_EVERY_ACTIVITY_CONTENT); luceneQuery += "+" + entry.getValue() + ":(" + Activity.CONSTANT_KEYWORD_IN_EVERY_ACTIVITY_CONTENT + " " + searchWord + ") "; } else { luceneQuery += "+" + entry.getValue() + ":(" + searchWord + ") "; } } else { luceneQuery += "+" + entry.getValue() + ":(" + searchWord + ") "; } } } if (luceneQuery.length() == 0) { log.debug("Returning all activity"); query = unstemmedRequestBuilder.buildQueryFromNativeSearchString("_hibernate_class:" + "org.eurekastreams.server.domain.stream.Activity"); } else { try { query = searchRequestBuilder.buildQueryFromNativeSearchString(luceneQuery); } catch (Exception ex) { return new ArrayList<Long>(); } } if (jsonQuery.containsKey("sortBy")) { query.setSort(new Sort(new SortField(jsonQuery.getString("sortBy"), true))); } if (log.isDebugEnabled()) { log.debug("Native Lucene Query: " + query.toString()); } searchRequestBuilder.setPaging(query, 0, maxResults); List<Long> activityIds = query.getResultList(); if (log.isInfoEnabled()) { log.info("Found " + activityIds.size() + " activities"); } return activityIds; } LuceneDataSource(final ProjectionSearchRequestBuilder inSearchRequestBuilder,
            final ProjectionSearchRequestBuilder inUnstemmedRequestBuilder,
            final Map<String, String> inRequestToField,
            final Map<String, PersistenceDataSourceRequestTransformer> inTransformers, final int inMaxResults); }
LuceneDataSource implements SortedDataSource { @Override @SuppressWarnings("unchecked") public List<Long> fetch(final JSONObject inRequest, final Long userEntityId) { JSONObject jsonQuery = inRequest.getJSONObject("query"); Boolean hasTerm = false; for (String term : requestToField.keySet()) { if (jsonQuery.containsKey(term)) { hasTerm = true; break; } } if (!hasTerm && (!jsonQuery.containsKey("sortBy") || (jsonQuery.containsKey("sortBy") && jsonQuery.getString( "sortBy").equals("date")))) { log.debug("No search term found"); return null; } String luceneQuery = ""; FullTextQuery query = null; for (Entry<String, String> entry : requestToField.entrySet()) { String req = entry.getKey(); if (jsonQuery.containsKey(req)) { String searchWord = jsonQuery.getString(req); searchWord = searchWord.replace(":", ""); if (transformers.containsKey(req)) { searchWord = (String) transformers.get(req).transform(jsonQuery, userEntityId); } if (req.toLowerCase().equals("keywords")) { if (searchWord.contains("NOT ") || searchWord.contains("!") || searchWord.contains("-")) { log.info("User is querying for activity content with:(" + searchWord + ") and seems to be using a NOT/!/- component. Lucene doesn't allow " + "NOT queries with one component, so I'll add a constant keyword that " + "I know is present: " + Activity.CONSTANT_KEYWORD_IN_EVERY_ACTIVITY_CONTENT); luceneQuery += "+" + entry.getValue() + ":(" + Activity.CONSTANT_KEYWORD_IN_EVERY_ACTIVITY_CONTENT + " " + searchWord + ") "; } else { luceneQuery += "+" + entry.getValue() + ":(" + searchWord + ") "; } } else { luceneQuery += "+" + entry.getValue() + ":(" + searchWord + ") "; } } } if (luceneQuery.length() == 0) { log.debug("Returning all activity"); query = unstemmedRequestBuilder.buildQueryFromNativeSearchString("_hibernate_class:" + "org.eurekastreams.server.domain.stream.Activity"); } else { try { query = searchRequestBuilder.buildQueryFromNativeSearchString(luceneQuery); } catch (Exception ex) { return new ArrayList<Long>(); } } if (jsonQuery.containsKey("sortBy")) { query.setSort(new Sort(new SortField(jsonQuery.getString("sortBy"), true))); } if (log.isDebugEnabled()) { log.debug("Native Lucene Query: " + query.toString()); } searchRequestBuilder.setPaging(query, 0, maxResults); List<Long> activityIds = query.getResultList(); if (log.isInfoEnabled()) { log.info("Found " + activityIds.size() + " activities"); } return activityIds; } LuceneDataSource(final ProjectionSearchRequestBuilder inSearchRequestBuilder,
            final ProjectionSearchRequestBuilder inUnstemmedRequestBuilder,
            final Map<String, String> inRequestToField,
            final Map<String, PersistenceDataSourceRequestTransformer> inTransformers, final int inMaxResults); @Override @SuppressWarnings("unchecked") List<Long> fetch(final JSONObject inRequest, final Long userEntityId); }
LuceneDataSource implements SortedDataSource { @Override @SuppressWarnings("unchecked") public List<Long> fetch(final JSONObject inRequest, final Long userEntityId) { JSONObject jsonQuery = inRequest.getJSONObject("query"); Boolean hasTerm = false; for (String term : requestToField.keySet()) { if (jsonQuery.containsKey(term)) { hasTerm = true; break; } } if (!hasTerm && (!jsonQuery.containsKey("sortBy") || (jsonQuery.containsKey("sortBy") && jsonQuery.getString( "sortBy").equals("date")))) { log.debug("No search term found"); return null; } String luceneQuery = ""; FullTextQuery query = null; for (Entry<String, String> entry : requestToField.entrySet()) { String req = entry.getKey(); if (jsonQuery.containsKey(req)) { String searchWord = jsonQuery.getString(req); searchWord = searchWord.replace(":", ""); if (transformers.containsKey(req)) { searchWord = (String) transformers.get(req).transform(jsonQuery, userEntityId); } if (req.toLowerCase().equals("keywords")) { if (searchWord.contains("NOT ") || searchWord.contains("!") || searchWord.contains("-")) { log.info("User is querying for activity content with:(" + searchWord + ") and seems to be using a NOT/!/- component. Lucene doesn't allow " + "NOT queries with one component, so I'll add a constant keyword that " + "I know is present: " + Activity.CONSTANT_KEYWORD_IN_EVERY_ACTIVITY_CONTENT); luceneQuery += "+" + entry.getValue() + ":(" + Activity.CONSTANT_KEYWORD_IN_EVERY_ACTIVITY_CONTENT + " " + searchWord + ") "; } else { luceneQuery += "+" + entry.getValue() + ":(" + searchWord + ") "; } } else { luceneQuery += "+" + entry.getValue() + ":(" + searchWord + ") "; } } } if (luceneQuery.length() == 0) { log.debug("Returning all activity"); query = unstemmedRequestBuilder.buildQueryFromNativeSearchString("_hibernate_class:" + "org.eurekastreams.server.domain.stream.Activity"); } else { try { query = searchRequestBuilder.buildQueryFromNativeSearchString(luceneQuery); } catch (Exception ex) { return new ArrayList<Long>(); } } if (jsonQuery.containsKey("sortBy")) { query.setSort(new Sort(new SortField(jsonQuery.getString("sortBy"), true))); } if (log.isDebugEnabled()) { log.debug("Native Lucene Query: " + query.toString()); } searchRequestBuilder.setPaging(query, 0, maxResults); List<Long> activityIds = query.getResultList(); if (log.isInfoEnabled()) { log.info("Found " + activityIds.size() + " activities"); } return activityIds; } LuceneDataSource(final ProjectionSearchRequestBuilder inSearchRequestBuilder,
            final ProjectionSearchRequestBuilder inUnstemmedRequestBuilder,
            final Map<String, String> inRequestToField,
            final Map<String, PersistenceDataSourceRequestTransformer> inTransformers, final int inMaxResults); @Override @SuppressWarnings("unchecked") List<Long> fetch(final JSONObject inRequest, final Long userEntityId);  }