Solr wildcard query with whitespace

前端 未结 9 1586
悲哀的现实
悲哀的现实 2020-12-06 04:57

I have a wildcard query that looks something like:

q=location:los a*

I\'d like it to match \"los angeles\" and \"los altos\". A query like

相关标签:
9条回答
  • 2020-12-06 05:04

    I've recently come across this problem myself, and it seems that all you need to do is escape the space in your query. Your original query would be interpreted by Solr as something like this:

    location:los id:a*
    

    (assuming "id" is your default search field)

    However, if you were to write your query as:

    location:los\ a*
    

    Then it would end up being parsed as:

    location:los a*
    

    And the above should yield the results that you desire (assuming your data is properly indexed).

    Tip: Figuring all this out is simple. Just add &debugQuery=on to the end of the url you use when submitting your query to see how it was parsed by Solr.

    0 讨论(0)
  • 2020-12-06 05:07

    Without seeing your config, I would say use a KeywordTokenizerFactory as you probably tokenize on whitespace now.

    0 讨论(0)
  • 2020-12-06 05:08

    I think you should use the config like this

      <fieldType name="text" class="solr.TextField" positionIncrementGap="100">
         <analyzer type="index">
            <tokenizer class="solr.KeywordTokenizerFactory"/>
            <filter class="solr.LowerCaseFilterFactory" />
            <filter class="solr.PatternReplaceFilterFactory" pattern="(\s+)" replacement=""   replace="all" />
        </analyzer>
      </fieldType>
    

    and you have to handle your input keyword for search as remove whitespace

    0 讨论(0)
  • 2020-12-06 05:10

    Solution for your problem using complex query parser:

    q={!complexphrase inOrder=true}location:"los a*"
    

    To know more about Complex phrase query parser, checkout this link! https://cwiki.apache.org/confluence/display/solr/Other+Parsers#OtherParsers-ComplexPhraseQueryParser

    0 讨论(0)
  • 2020-12-06 05:12

    The query (assuming you have whitespace tokenizer): q=location:los a* means that you search document with word "los" and a word that starts with "a"

    Solr (as much that I know) cannot determine if one word (or term) appear before another.

    0 讨论(0)
  • 2020-12-06 05:14

    Used this

    q=location:los/ a*
    

    instead of

    q=location:los a*
    
    0 讨论(0)
提交回复
热议问题