How can I tell Solr to return the hit search terms per document?

后端 未结 3 864
半阙折子戏
半阙折子戏 2020-11-29 07:50

I have a question about queries in Solr. When I perform a query with multiple search terms that are all logically linked by OR (e.g. q=content:(foo OR bar OR foobar)

相关标签:
3条回答
  • 2020-11-29 08:10

    My comment as an answer:

    I use the Function Queries and it seems that performance is not an issue :) For those who are interested: I'm using theexists function and add a pseudo field for every search term like so: fl=exists(query({!v='content:(foo)'})),exists(query({!v='content:(bar)'})). From the response I parse the search term with an Regex.

    As Paul stated above, you can alias pseudo fields to avoid the regex parsing, e.g. fl=foo:exists(query({!v='content:(foo)'}))

    0 讨论(0)
  • 2020-11-29 08:12

    Kind of depends on your requirements, but as far as I know there is no specific support for this in Solr. You can however hack it together in a few other ways. Not sure what you can expect for performance for these, tho..

    Use Highlightning

    If you use highlighting you can parse the returned highlighted snippets for the start/end tags of the highlighted text. This will be the term that matched something in your query.

    Use debugQuery Information

    You can parse the information returned by a query with debugQuery=true to determine that a term was associated with a result by looking at termWeight (iirc). This might be a filtered version of your original term (if you have stemming etc. active for the field).

    Use Field Collapsing

    By using group.query you can build lists of documents that matches each term, instead of issuing several requests. You can also build queries that feature several of the terms OR-ed together if you need lists for "contains either". Might not be effective for a large amount of fields.

    Parse the returned document yourself

    Get the document, then extract the terms by yourself. Will require a bit of fuzzy matching, since you'll have to deal with text processing on the Solr side as well.

    Use Function Queries

    You can get metavalues for each document with each term from a FunctionQuery that looks up the number occurences of a term in that document. Will require quite a few function queries for a large number of terms, but might be fast.

    .. neither option is perfect, but might work for the problem at hand.

    0 讨论(0)
  • 2020-11-29 08:24

    In my case solr6.6 the query fl=foo:exists(query({!v='content:(foo)'})) not seems to be working it always return 0 documents and I was having foo in my document so I need to change this query to ?q=*:*&fl=foo:exists(query({!v='content:(foo)'})) and I started work for me.

    0 讨论(0)
提交回复
热议问题