BOSTON — It’s at times been frustrating for Andrew Benintendi through an up-and-down first half of the season as he’s continued to find answers to his struggles at the plate.

He hadn’t been producing like the Red Sox wanted at his new spot as the leadoff hitter through the first two months, and admittedly he was pressing too much. He sought guidance from whatever source he could find.

At long last, though, it seems to be starting to come together for Benintendi.

The left fielder has been surging over the last two weeks, and Wednesday his bat was the difference. Benintendi went 3-for-4 with two doubles and a triple as he rallied his team from an early deficit, and Mookie Betts drew a walk-off walk in the bottom of the ninth as the Red Sox ended a three-game losing streak with a 4-3 win over the Texas Rangers at Fenway.

“I feel like it’s coming,” Benintendi said. “It’s been a grind for pretty much the whole year trying to find my swing again. I think I’ve been talking to a lot of guys and I think we found something the last few days which I’m going to try to keep working on so I feel like I’m barreling up more consistently the last few days. Just go from there. ...

“It’s definitely been the longest I’ve ever gone trying to find it.”

Benintendi said he was just trying to do too much and swinging at bad pitches through the first two months as he watched his batting average dip to .256. But he has talked to anyone he can find from coaches to players to figure it out.

A change in the order has seemed to help, too. He didn’t give it any merit, but since Alex Cora moved Betts back to the leadoff spot and slid Benintendi back to No. 2, where they were all of last season, he’s looked more comfortable. Since the manager made that change before the Yankees series that started May 31, Benintendi is batting .318.

Whatever the case, he’s looking more like himself.

“He’s in a good place right now,” Cora said.

Rick Porcello put the Red Sox (35-34) in an early 2-0 hole in the first, but Benintendi sparked the comeback. He hit a double in the first before Rafael Devers drove him in with a two-out single, and his RBI triple in the third tied the game.

In the fifth, Michael Chavis reached on an infield single before Benintendi came through again with two outs with a double off the Green Monster to put the Red Sox in front. Benintendi saw a total of six pitches over those three hits.

“He’s swinging the bat well,” Porcello said. “He’s wearing out that wall in left-center right now. He looks great.”

Porcello settled down after the first as he retired 15 in a row before Elvis Andrus doubled off him with two outs in the fifth. He sat down 18 of the final 20 batters he faced as he exited to a standing ovation with two outs in the seventh.

The Red Sox bullpen couldn’t protect the lead in the eighth. Brandon Workman hit Shin-Soo Choo with a pitch, and the next batter, Delino DeShields laid down a sacrifice bunt that Christian Vazquez threw away to put runners on second and third with one out. Elvis Andrus followed with a sacrifice fly to right that tied the game.

Vazquez made up for his blunder with a leadoff double in the ninth, and Rangers reliever Jesse Chavez struggled to find the strike zone. Jackie Bradley Jr. singled and Michael Chavis walked to load the bases with no outs, and it was almost a formality as Betts stepped to the plate. He walked on five pitches to win the game.

“Very, very important,” Cora said of the win after the Red Sox had lost five of their previous six.

The Red Sox avoided going under .500 for the first time since early May. They can salvage a split with the Rangers, who they’re two games behind for the second wild-card spot, as David Price takes the ball today.

“We need every win we can get right now,” Porcello said. “We’re trying to create some momentum, get rolling here. Hopefully we can reel off a couple in a row and get things headed in the right direction again.