[Update: It was a sweeping and complex criminal enterprise, prosecutors say. Read more.]

A three-year investigation ended Wednesday with the arrest of seven New York City police officers on prostitution, corruption and misconduct charges in connection with an illegal gambling and prostitution ring in Brooklyn and Queens, law enforcement officials said.

Two other officers, including a detective who until five months ago worked in the Internal Affairs Bureau, were stripped of their guns and shields and placed on administrative duty.

The police said that more than 40 civilians were also in custody or being sought in connection with the investigation, which began with an anonymous officer’s tip to the internal affairs unit in April 2015.

The arrested officers — three sergeants, two detectives and two officers — were indicted before they were taken into custody. They are suspected of providing protection for the ring’s activities in Sunset Park, Brooklyn, and along Roosevelt Avenue in Queens, the police said. Area residents and public officials have long complained about brothels operating out of local homes, spas and bars, with new establishments popping up as quickly as the police shut the old ones down.