england-and-wales
Initially, one should comply with the officer's instructions as he has the power under statutory Stop & Search powers to detain someone for the purpose of the search (discussed here). Failure to do so may be an offence.
If, subsequently, one considers the search and detention was unlawful, the first port of call is to lodge a complaint with the relevant police force who - depending on the circumstances - may escalate the complaint to the Independent Office for Police Conduct IOPC.
You can complain directly to the police/other organisation (see ‘Who can I complain about?’ below for a list of the other organisations) or via the IOPC. If you complain via the IOPC, your complaint will be sent direct to the organisation involved. They will assess your complaint and contact you about how it will be handled. The IOPC will not be involved with this initial assessment of your complaint.
If the complaint is found to be valid, then any offence committed by not complying with the office would (in all probability) be overturned on appeal. As well as any compensation awarded by the court, the Chief Constable may consider making an ex gratia payment (mentioned here).
Note that there is no obligation to do anything if the interaction falls within the Stop & Account provisions.