Relate search results with the matching filters in query ("Tagging" hits with matching filters)

Hey Everyone,

I have a problem that I have solved in a way that I am not sure is the most efficient, appropriate way to solve, and I would like to discuss it with you.

My scenario - I'm trying to search my index for documents that have a title that matches one of a list of titles I receive from user input, using a match_phrase query over an analyzed (using the standard analyzer) title.

The user input is of course "free text" and so, he can give me titles that are not the exact match of the document title. (For example, the user will input "and that" and expects to match "This and that").

I need to give back the user a map of the title he was looking for and the document it matched, IF the title had exactly one document matching it (so, if "and that" matched 3 documents, i will return nothing to the user).

If the user looked for title "and that", which matches Document A, and title "spot" which matches Document B, and "sun" which matched both Document A and Document B, I will return him a map looking like this -

"and that" -> Document A
"spot" -> Document B
("sun" will not be on the map, because it matched more than one document)

The naive way to do that is to query each title separately (match_phrase query), and if exactly one result returned, add the item to the map.
But, the user can give me a big list of 50+ titles, and I wouldn't want to query elastic 50 different times just for one user request.

I can do a multi search request (_msearch, "bulk search" api), which would let me send only one request, but will still be 50+ different queries on elastic. That's what I'm doing today to solve my problem.

I was wondering if there's a better way to do what I'm looking for.
What I am actually looking for is knowing which query part caused my search result to return. Some sort of "tagging" to the search result with the relevant query parts that got the result to return from the search.

That way, I could do a single query with a match_phrase for each title, and from the "query part tag" on the results I get back, I'll know which title belongs to which document. If a title matched upon several documents (several documents received the relevant query part "tag"), I will rule it out.

(Also, I could be wrong and having multiple match_phrase queries is the same as having a multi search with 50 different queries?)

Any suggestions on what's the right path to go here? What's the best way to do that?

Thanks in advance!

I have to admit I only scanned your post, but the two traditional ways to answer questions like this are _named queries and highlighting. They do different, but similar things. I think you want named queries.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.