<commit_before>//
//  SearchHelper.java
//  blogwt
//
//  Created by <NAME> (billy1380) on 20 Aug 2015.
//  Copyright © 2015 WillShex Limited. All rights reserved.
//
package com.willshex.blogwt.server.helper;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.search.Document;
import com.google.appengine.api.search.Index;
import com.google.appengine.api.search.IndexSpec;
import com.google.appengine.api.search.PutException;
import com.google.appengine.api.search.Results;
import com.google.appengine.api.search.ScoredDocument;
import com.google.appengine.api.search.SearchService;
import com.google.appengine.api.search.SearchServiceFactory;
import com.google.appengine.api.search.StatusCode;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.appengine.api.taskqueue.TaskOptions.Method;
import com.google.appengine.api.taskqueue.TransientFailureException;
import com.googlecode.objectify.cmd.Query;
import com.willshex.blogwt.server.service.page.PageServiceProvider;
import com.willshex.blogwt.server.service.post.PostServiceProvider;
import com.willshex.blogwt.server.service.user.UserServiceProvider;
import com.willshex.blogwt.shared.api.datatype.Page;
import com.willshex.blogwt.shared.api.datatype.Post;
import com.willshex.blogwt.shared.api.datatype.User;

/**
 * @author <NAME> (billy1380)
 *
 */
public class SearchHelper {

	public static final String ENTITY_NAME_KEY = "name";
	public static final String ENTITY_ID_KEY = "id";
	
	private final static String ALL_INDEX_NAME = "all";
	private static final int SEARCH_LIMIT = 3;
	private static final String INDEX_SEARCH_URL = "/searchindexer";
	private static SearchService searchService;

	public static void indexDocument (Document document) {
		if (document != null) {
			indexDocument(document, 0);
		}
	}

	private static void indexDocument (Document document, int count) {
		try {
			getIndex().put(document);
		} catch (PutException e) {
			if (StatusCode.TRANSIENT_ERROR
					.equals(e.getOperationResult().getCode())) {
				if (count < 2) {
					indexDocument(document, count++);
				}
			}
		}
	}

	private static SearchService ensureSearchService () {
		if (searchService == null) {
			searchService = SearchServiceFactory.getSearchService();
		}
		return searchService;
	}

	public static List<Post> searchPosts (String query) {
		Results<ScoredDocument> matches = getIndex().search(query);
		List<Post> posts = new ArrayList<Post>();
		String id;
		Post post;
		int limit = SEARCH_LIMIT;
		final String postName = PostServiceProvider.provide().getName();
		for (ScoredDocument scoredDocument : matches) {
			if (limit == 0) {
				break;
			}

			if ((id = scoredDocument.getId()).startsWith(postName)) {
				post = PostServiceProvider.provide()
						.getPost(Long.valueOf(id.replace(postName, "")));
				if (post != null) {
					posts.add(post);
				}
			}

			limit--;
		}

		return posts;
	}

	public static List<Page> searchPages (String query) {
		Results<ScoredDocument> matches = getIndex().search(query);
		List<Page> pages = new ArrayList<Page>();
		String id;
		Page page;
		int limit = SEARCH_LIMIT;
		final String pageName = PageServiceProvider.provide().getName();
		for (ScoredDocument scoredDocument : matches) {
			if (limit == 0) {
				break;
			}

			if ((id = scoredDocument.getId()).startsWith(pageName)) {
				page = PageServiceProvider.provide()
						.getPage(Long.valueOf(id.replace(pageName, "")));
				if (page != null) {
					pages.add(page);
				}
			}

			limit--;
		}

		return pages;
	}

	public static List<User> searchUsers (String query) {
		Results<ScoredDocument> matches = getIndex().search(query);
		List<User> users = new ArrayList<User>();
		String id;
		User user;
		int limit = SEARCH_LIMIT;
		final String userName = PostServiceProvider.provide().getName();
		for (ScoredDocument scoredDocument : matches) {
			if (limit == 0) {
				break;
			}

			if ((id = scoredDocument.getId()).startsWith(userName)) {
				user = UserServiceProvider.provide()
						.getUser(Long.valueOf(id.replace(userName, "")));
				if (user != null) {
					users.add(user);
				}
			}

			limit--;
		}

		return users;
	}

	public static void deleteSearch (String documentId) {
		getIndex().delete(documentId);
	}

	private static Index getIndex () {
		return ensureSearchService().getIndex(
				IndexSpec.newBuilder().setName(ALL_INDEX_NAME).build());
	}<commit_msg>fixed a bug that was causing users search to happen usring wrong service name<commit_after>//
//  SearchHelper.java
//  blogwt
//
//  Created by <NAME> (billy1380) on 20 Aug 2015.
//  Copyright © 2015 WillShex Limited. All rights reserved.
//
package com.willshex.blogwt.server.helper;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.search.Document;
import com.google.appengine.api.search.Index;
import com.google.appengine.api.search.IndexSpec;
import com.google.appengine.api.search.PutException;
import com.google.appengine.api.search.Results;
import com.google.appengine.api.search.ScoredDocument;
import com.google.appengine.api.search.SearchService;
import com.google.appengine.api.search.SearchServiceFactory;
import com.google.appengine.api.search.StatusCode;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.appengine.api.taskqueue.TaskOptions.Method;
import com.google.appengine.api.taskqueue.TransientFailureException;
import com.googlecode.objectify.cmd.Query;
import com.willshex.blogwt.server.service.page.PageServiceProvider;
import com.willshex.blogwt.server.service.post.PostServiceProvider;
import com.willshex.blogwt.server.service.user.UserServiceProvider;
import com.willshex.blogwt.shared.api.datatype.Page;
import com.willshex.blogwt.shared.api.datatype.Post;
import com.willshex.blogwt.shared.api.datatype.User;

/**
 * @author <NAME> (billy1380)
 *
 */
public class SearchHelper {

	public static final String ENTITY_NAME_KEY = "name";
	public static final String ENTITY_ID_KEY = "id";

	private final static String ALL_INDEX_NAME = "all";
	private static final int SEARCH_LIMIT = 3;
	private static final String INDEX_SEARCH_URL = "/searchindexer";
	private static SearchService searchService;

	public static void indexDocument (Document document) {
		if (document != null) {
			indexDocument(document, 0);
		}
	}

	private static void indexDocument (Document document, int count) {
		try {
			getIndex().put(document);
		} catch (PutException e) {
			if (StatusCode.TRANSIENT_ERROR
					.equals(e.getOperationResult().getCode())) {
				if (count < 2) {
					indexDocument(document, count++);
				}
			}
		}
	}

	private static SearchService ensureSearchService () {
		if (searchService == null) {
			searchService = SearchServiceFactory.getSearchService();
		}
		return searchService;
	}

	public static List<Post> searchPosts (String query) {
		Results<ScoredDocument> matches = getIndex().search(query);
		List<Post> posts = new ArrayList<Post>();
		String id;
		Post post;
		int limit = SEARCH_LIMIT;
		final String postServiceName = PostServiceProvider.provide().getName();
		for (ScoredDocument scoredDocument : matches) {
			if (limit == 0) {
				break;
			}

			if ((id = scoredDocument.getId()).startsWith(postServiceName)) {
				post = PostServiceProvider.provide()
						.getPost(Long.valueOf(id.replace(postServiceName, "")));
				if (post != null) {
					posts.add(post);
				}
			}

			limit--;
		}

		return posts;
	}

	public static List<Page> searchPages (String query) {
		Results<ScoredDocument> matches = getIndex().search(query);
		List<Page> pages = new ArrayList<Page>();
		String id;
		Page page;
		int limit = SEARCH_LIMIT;
		final String pageServiceName = PageServiceProvider.provide().getName();
		for (ScoredDocument scoredDocument : matches) {
			if (limit == 0) {
				break;
			}

			if ((id = scoredDocument.getId()).startsWith(pageServiceName)) {
				page = PageServiceProvider.provide()
						.getPage(Long.valueOf(id.replace(pageServiceName, "")));
				if (page != null) {
					pages.add(page);
				}
			}

			limit--;
		}

		return pages;
	}

	public static List<User> searchUsers (String query) {
		Results<ScoredDocument> matches = getIndex().search(query);
		List<User> users = new ArrayList<User>();
		String id;
		User user;
		int limit = SEARCH_LIMIT;
		final String userServiceName = UserServiceProvider.provide().getName();
		for (ScoredDocument scoredDocument : matches) {
			if (limit == 0) {
				break;
			}

			if ((id = scoredDocument.getId()).startsWith(userServiceName)) {
				user = UserServiceProvider.provide()
						.getUser(Long.valueOf(id.replace(userServiceName, "")));
				if (user != null) {
					users.add(user);
				}
			}

			limit--;
		}

		return users;
	}

	public static void deleteSearch (String documentId) {
		getIndex().delete(documentId);
	}

	private static Index getIndex () {
		return ensureSearchService().getIndex(
				IndexSpec.newBuilder().setName(ALL_INDEX_NAME).build());
	}
{'fraction_non_alphanumeric': 0.12562704471101419, 'fraction_numerical': 0.005234460196292257, 'mean_word_length': 4.1842849067269645, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2496717', 'n_tokens_mistral': 3233, 'n_tokens_neox': 2848, 'n_words': 611}