This is the next step to learn from me Lucene ()---lucene search facet index principle and Facet query instance (http://blog.csdn.net/wuyinggui10000/article/details/ 45973769), the previous article is mainly statistical facet of the dim and the corresponding number of each species, the personal feeling that the group is different from Lucene is the storage of facet is similar to the hash (key-field-value) Form, Group is a single map (Key-value), although the number of a certain category can be counted, it is obvious that facets are more extensible.
Key-field-value Query
Facets can count the results of a certain dimension that satisfy a condition, as follows:
@Test public void Testdrilldownslide () {try {Directoryreader Indexreader = directoryreader.open (directory); Indexsearcher searcher = new Indexsearcher (Indexreader); Drillsideways ds = new Drillsideways (Searcher, config, taxoreader); Drilldownquery DDQ = new drilldownquery (config); Ddq.add ("FilePath", "ik"); Drillsidewaysresult r = Ds.search (DDQ, 10); Topdocs hits = R.hits; for (Scoredoc scoreDoc:hits.scoreDocs) {Document doc = Searcher.doc (scoredoc.doc); System.out.println (Doc.get ("path")); }} catch (Exception e) {e.printstacktrace (); } }
Here we search for the dim is filepath, look for the range is IK associated data, the corresponding query result is all contained in the IK folder data
C:\users\lenovo\desktop\lucene\ik-analyzer-2012ff\dist\ikanalyzer.cfg.xmlc:\users\lenovo\desktop\lucene\ Ik-analyzer-2012ff\dist\ikanalyzer2012ff_u1.jarc:\users\lenovo\desktop\lucene\ik-analyzer-2012ff\dist\ Ikanalyzer2015.jarc:\users\lenovo\desktop\lucene\ik-analyzer-2012ff\dist\license.txtc:\users\lenovo\desktop\ Lucene\ik-analyzer-2012ff\dist\notice.txtc:\users\lenovo\desktop\lucene\ik-analyzer-2012ff\dist\stopword.dicc : \users\lenovo\desktop\lucene\ik-analyzer-2012ff\doc\allclasses-frame.htmlc:\users\lenovo\desktop\lucene\ Ik-analyzer-2012ff\doc\allclasses-noframe.htmlc:\users\lenovo\desktop\lucene\ik-analyzer-2012ff\doc\ Constant-values.htmlc:\users\lenovo\desktop\lucene\ik-analyzer-2012ff\doc\deprecated-list.html
Range Query
Facets also support range queries, and range queries include Doublerange and Longrange, and their corresponding facets are doublerangefacets and longrangefacets;
Take longrangefacetcounts as an example, longrangefacetcounts can sort the values of long type query
Here we group the number of words in each document, and the range query example is as follows:
@Testpublic void Testoverlappedendstart () {try {Indexreader reader = directoryreader.open (directory); Facetscollector FC = new Facetscollector (); Indexsearcher s = new Indexsearcher (reader); S.search (New Matchalldocsquery (), FC); Facets Facets = new Longrangefacetcounts ("ContentLength", FC, new Longrange ("0-100", 0L, True, 100L, true), new L Ongrange ("100-200", 100L, True, 200L, true), new Longrange ("200-300", 200L, True, 300L, true), new Longrange (" 300-400 ", 300L, True, 400L, true)); Facetresult result = Facets.gettopchildren (ten, "contentlength"); System.out.println (Result.tostring ());} catch (Exception e) {e.printstacktrace ();}}
The result of the execution is:
Dim=contentlength path=[] value=22 childcount=4 0-100 (7) 100-200 (9) 200-300 (3) 300-400 (3)
Multiple Dim queries
Facet drillsideways can define multiple Facetcount queries, and the result returned is the number of statistics corresponding to each facet
@Testpublic void Testmixedrangeandnonrangetaxonomy () {try {Indexreader reader = directoryreader.open (directory); Indexsearcher s = new Indexsearcher (reader); Drillsideways ds = new Drillsideways (S, config, taxoreader) {@Override protected Facets buildfacetsresult (Fa Cetscollector drilldowns, facetscollector[] drillsideways, string[] drillsidewaysdims) throws IOException { Facetscollector FIELDFC = drilldowns; Facetscollector dimfc = drilldowns; if (drillsideways! = null) {for (int i=0;i<drillsideways.length;i++) {String Dim = Drillsidewa Ysdims[i]; if (Dim.equals ("ContentLength")) {FIELDFC = drillsideways[i]; } else {dimfc = drillsideways[i]; }}} map<string,facets> Bydim = new hashmap<string,facets> (); Bydim.put ("ContentLength", New Longrangefacetcounts ("ContentLength", FIELDFC, New Longrange ("Less than", 0L, True, 100L, false), new Longrange ("between D ", 100L, True, 200L, false), new Longrange (" Over ", 200L, True, Integer.max_value, false) )); Bydim.put ("Dim", New Fasttaxonomyfacetcounts (taxoreader, config, dimfc)); return new Multifacets (Bydim); } @Override protected Boolean scoresubdocsatonce () {return false; } }; Drilldownquery DDQ = new drilldownquery (config); Drillsidewaysresult DSR = Ds.search (DDQ, 10); Facets facet = dsr.facets; list<facetresult> results = Facet.getalldims (Reader.maxdoc ()); for (Facetresult facetresult:results) {System.out.println (Facetresult.dim); Labelandvalue[] values = facetresult.labelvalues;for (Labelandvalue labelandvalue:values) {System.out.println ("\ T" + Labelandvalue.label + "" +labelandvalue.value);}}} catch (Exception e) {e.printstacktrace ();}}
The results of the program run as follows:
Dimodd 126even 119contentLengthless than 7between + 229 9over
List query support for a single range
Facet supports the interval query of a single range so that the Topdocs list of this range can be queried, equivalent to returning the list of document objects;
Here we search for data with a content length between 0 and 100
@Testpublic void Testdrilldownquerywithrange () {try {Indexreader reader = directoryreader.open (directory); Indexsearcher s = new Indexsearcher (reader); Drilldownquery DDQ = new drilldownquery (config); Ddq.add ("ContentLength", Numericrangequery.newlongrange ("ContentLength", 0l, 100l, True, false)); Topdocs docs = S.search (DDQ, Reader.maxdoc ()); SYSTEM.OUT.PRINTLN ("Total number of data queried:" +docs.totalhits); for (Scoredoc ScoreDoc:docs.scoreDocs) {System.out.println (S.doc (Scoredoc.doc). Get ("path"));}} catch (Exception e) {e.printstacktrace ();}}
The results of the operation are as follows:
Total data queried: 7c:\users\lenovo\desktop\lucene\jcseg\donate.txtc:\users\lenovo\desktop\lucene\jcseg\ jcseg-elasticsearch\src\main\resources\es-plugin.propertiesc:\users\lenovo\desktop\lucene\jcseg\lexicon\ Lex-autoload.todoc:\users\lenovo\desktop\lucene\jcseg\lexicon\lex-en-pun.lexc:\users\lenovo\desktop\lucene\ Jcseg\lexicon\lex-ln-adorn.lexc:\users\lenovo\desktop\lucene\ik-analyzer-2012ff\doc\resources\inherit.gifc:\ Users\lenovo\desktop\lucene\ik-analyzer-2012ff\src\ext.dic
This section is an example, the personal feel that this will be more intuitive, facet involves a wide range of facets, there is no facet of the sort and other related operations, will be in the follow-up, I hope you continue to pay attention.
Step by step with me to learn Lucene ()---lucene search query Example (2)