Problem:
The Chinese word segmentation of the Chinese Emy of Sciences is used to index the "People's Republic of China". It is segmented into "China", "people", "Republic", and can be searched by "People's Republic, the search for "the republic of china" cannot be found, but the search for "the republic of china" can be found. Why?
Answer:
I downloaded the Republic of California. However, when building a Query Parser to build a Query Object, it was built into PhraseQuery -- contents: "The Republic of China", rather than BooleanQuery -- contents: China contents: according to PhraseQuery, it has a slop parameter to indicate the distance between two words. The default value is 0, that is, only when the document contains "China" and "Republic" and the two are adjacent to each other can it be returned. This is why the People's Republic of China can be searched (it builds PhraseQuery but is adjacent) AND the Republic of China can be searched (it builds BooleanQuery ), the reason why the "Republic of China" cannot be searched (it builds PhraseQuery but is not adjacent ).
Try to parse Query query = parser. parse ("\" Chinese Republic \"~ 1 ")
You can also use the API to set Slop to 1 to search for the result.
Query query = parser. parse ("the Republic of China "); PhraseQuery pquery = (PhraseQuery) query; Pquery. setSlop (1 ); |
Instance:
Analyzer ca = new ChineseAnalyzer ();
QueryParser parser = new QueryParser (field, ca );
Query query1 = parser. parse ("People's Republic of China ");
System. out. println ("Searching for:" + query1.toString (field ));
The query object is:
Query1 PhraseQuery (id = 39) Boost 1.0 Field "contents" MaxPosition 1 Positions ArrayList <E> (id = 45) Slop 0 Terms ArrayList <E> (id = 49) ElementData Object [4] (id = 74) [0] Term (id = 76) Field "contents" Text "people" [1] Term (id = 77) Field "contents" Text "Republic of China" |
Query statement:
Searching for: "People's Republic of China" |
Query query2 = parser. parse ("China AND Republic of China ");
System. out. println ("Searching for:" + query2.toString (field ));
The query object is:
Query2 BooleanQuery (id = 43) Boost 1.0 Krases ArrayList <E> (id = 56) ElementData Object [10] (id = 57) [0] BooleanClause (id = 59) Occur BooleanClause $ Occur (id = 62) Name "MUST" Query TermQuery (id = 65) Boost 1.0 Term Term (id = 70) Field "contents" Text "China" [1] BooleanClause (id = 61) Occur BooleanClause $ Occur (id = 62) Name "MUST" Query TermQuery (id = 64) Boost 1.0 Term Term (id = 68) Field "contents" Text "Republic of China" |
Query statement:
Searching for: + China + Republic |
Query query3 = parser. parse ("\" Chinese Republic \"~ 1 ");
System. out. println ("Searching for:" + query3.toString (field ));
The query object is:
Query3 PhraseQuery (id = 54) Boost 1.0 Field "contents" MaxPosition 1 Positions ArrayList <E> (id = 93) Slop 1 Terms ArrayList <E> (id = 94) ElementData Object [4] (id = 96) [0] Term (id = 97) Field "contents" Text "China" [1] Term (id = 98) Field "contents" Text "Republic of China" |
Query statement:
Searching for: "The Republic of China "~ 1 |
Query query4 = parser. parse ("Chinese Republic ");
PhraseQuery pquery = (PhraseQuery) query4;
Pquery. setSlop (1 );
System. out. println ("Searching for:" + query4.toString (field ));
The query object is:
Query4 PhraseQuery (id = 55) Boost 1.0 Field "contents" MaxPosition 1 Positions ArrayList <E> (id = 102) Slop 1 Terms ArrayList <E> (id = 103) ElementData Object [4] (id = 105) [0] Term (id = 107) Field "contents" Text "China" [1] Term (id = 108) Field "contents" Text "Republic of China" |
Query statement:
Searching for: "The Republic of China "~ 1 |