Lucene QueryParse discards " when parsing

与世无争的帅哥 提交于 2021-01-29 13:49:35

问题


I have a query -license:"CC-BY-NC" AND -license:"CC-BY-ND 4.0 (Int)" to be passed into PrecedenceQueryParser.parse like this:

Query query = new PrecedenceQueryParser().parse(filter, '')

But in the generated query you can see, clauses are like -lincense:CC-BY-NC, "" are lost.

Is there any settings to keep the ""?

===================== UPDATE ===========================

I understand that since I'm looking for a match of CC-BY-ND 4.0 (Int), without double quotes (double quotes are just used to make it a phrase). That's why query.clauses[1].query doesn't have "" around the CC-BY-ND 4.0 (Int)

Now I do this:

    def bqb = new BooleanQuery.Builder()
    clauses.each { clause ->
        bqb.add(clause.query, clause.prohibited ? BooleanClause.Occur.SHOULD : BooleanClause.Occur.MUST_NOT)
    }

    String s = bqb.build().toString()

build a BooleanQuery and put clauses together,

the s then equals to license:CC-BY-ND 4.0 (Int) license:CC-BY-NC

This is definitely not what I want, I need CC-BY-ND 4.0 (Int) to be surrounded by double-quotes. Is there any way to do that?

Gibbs's could be a solution but a bit tricky I think.


回答1:


You need to escape them.

When you pass -license:"CC-BY-NC" AND -license:"CC-BY-ND 4.0 (Int)" this, use the below

-license:"\"CC-BY-NC\"" AND -license:"\"CC-BY-ND 4.0 (Int)\""



回答2:


Some additional information, to add to the answer provided by @gibbs:

When using the classic query parser syntax (which is what is being used in the question), then the following characters all have special meanings and therefore may need to be escaped, in any of the search terms where they appear:

+  -  &&  ||  !  (  )  {  }  [  ]  ^  "  ~  *  ?  :  \  /

Note that in 2 cases, these are groups of characters: && and ||.

This list is documented here.

The easiest way to handle all these characters is to pass each search term to the classic QueryParser, before building the search expression:

org.apache.lucene.queryparser.classic.QueryParser;

For example:

String escapedSearchTerm = QueryParser.escape(searchTerm)


来源:https://stackoverflow.com/questions/63276920/lucene-queryparse-discards-when-parsing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!