The short answer is that a criminal defense can argue that any evidence seized as a result of this search should be suppressed. From the info you provided this search sounds suspicious at best and blatantly illegal at worst. Consult with a criminal defense attorney to assist you and help fight your case.
The answer to the question is, sometimes the police do have the right to detain or search you, and sometimes they do not. Whether their actions are legal or not can depend on complex factors, interpretation of conflicting case law, what the witnesses including the officers testify to, and what testimony is believed. Sometimes, the legality of their actions will have nothing to do with you. For example, if someone was just shot or robbed nearby, that can give the police enhanced ability to stop and search people who might be escaping from the crime.
Since your description suggests that the police acted without probable cause, you should consult with an attorney in your area, and have him get all the reports and other facts, before deciding on whether to take action about this.
Contact me at 248-399-6930 for a free consultation. You and I do not have an attorney-client relationship formed by our communications on this website. Advice given by me on this website is general advice based on partial information. You should not rely on any advice given without first hiring a lawyer in the area where the case is pending, and providing that lawyer with full information.
The police have a right to detain someone under certain circumstances. Under certain circumstances, they can restrain someone but they do not have a right to beat up someone who is in custody. You need to immediately consult with an experienced criminal defense attorney. You may also need to consult with any attorney who specializes in police misconduct, but your immediate problem is to defend the action taken against you which brings you to court. It is not possible in this online forum to give more exact information and suggestions and there surely are more facts surrounding this situation. You should bring with you any paperwork and be prepared to discuss your case in detail with the attorney. Do not discuss your case online or with anyone other than your attorney and make sure your attorney has experience in this area of law.