But Judge James Ware of the US District for the Northern District of California said on Friday that the company must produce some web addresses indexed in its system.
In a 21-page ruling, Ware said the privacy considerations of Google users led him to deny part of the justice department's request.
He said: "To the extent the motion seeks an order compelling Google to disclose search queries of its users the motion is denied."
Alberto Gonzales, US attorney-general, had demanded that Google should turn over data the government wanted from the company as part of the Bush administration's attempt to defend a federal law on child pornography on the internet.
Andy Serwin, a privacy law expert, said: "You have to disclose what your robots find, but you don't have to disclose what people search for.
"The order does get the government what it probably needed, not what it wanted."
During a court hearing on Tuesday the government reduced the number of Google searches it wanted data on to 50,000 web addresses and roughly 5000 search terms from the millions or potentially billions of addresses it had initially sought.
Ware said: "The court grants the government's motion to compel only as to the sample of 50,000 URLs [uniform resource locators], from Google's search index."
"The government is not entitled to go on a fishing expedition through millions of Google searches any time it wants"
Nicole Wong, Google's associate general counsel, said: "What his ruling means is that neither the government nor anyone else has carte blanche when demanding data from internet companies."
The full comment is at http://googleblog.blogspot.com/2006/03/judge-tells-doj-no-on-search-queries.html
The government wants the data for a statistical study it is doing to show the effectiveness of filtering software at issue in a separate case - ACLU v Gonzales - that concerns a federal law on online child pornography.
In a statement on the American Civil Liberties Union (ACLU) website, Aden Fine, the union’s lawyer said: "The government is not entitled to go on a fishing expedition through millions of Google searches any time it wants, just because it claims that it needs that information.
"Given the government’s continued vagueness about why it needs these vast quantities of consumer records, Google has rightly denied the request."
The Google controversy arose in connection with the ACLU's challenge to the Child Online Protection Act, which would impose sanctions, with penalties of up to $50,000 a day and up to six months' imprisonment, for online material judged "harmful to minors".
The government said it needed the Google records for its defence of the law. But the ACLU said that the government had failed to describe how the millions of Google user records would help it to determine whether the filtering software was effective and if the law was constitutional.
"Given their initial request, obviously it is a victory for privacy to the extent that no information entered from the users is being offered"
T Barton Carter,
professor of communication
In his decision, Ware wrote of the "three vital interests" that needed to be weighed in the case: national interest, proprietary business information and privacy concerns.
Victory for privacy
T Barton Carter, a professor of communication at Boston University's College of Communication, said that beyond privacy issues there remain further concerns.
He said: "It is still a little disturbing that essentially the government can compel information from a party that is not involved in a lawsuit.
"Given their initial request, obviously it is a victory for privacy to the extent that no information entered from the users is being offered."
Court ruling in full (pdf): http://www.google.com/press/images/ruling_20060317.pdf
More coverage at the American Civil Liberties Union, the Electronic Privacy Information Centre and the Electronic Frontier Foundation.