Lucene Exploration-Query

Source: Internet
Author: User
Tags solr

Lucene is intended for the back of SOLR. If you skip Lucene and go straight to SOLR, you are probably a little bit ignorant.

Due to the time of the relationship, Lucene query method also has a number, so separate out.

I. EXACT query

     /** * Get Find Object * @return * @throws Exception */Private Indexsearcher Getsearcher () throws Exceptio        n {//1. Create a directory object, which is where the index inventory is placed directory directory = Fsdirectory.open (new File (Indexdir)); 2.        To create a Indexreader object, you need to specify directory Indexreader Indexreader = directoryreader.open (directory); 3.        To create a Indexsearcher object, you need to specify the Indexreader object indexsearcher indexsearcher = new Indexsearcher (Indexreader);    return indexsearcher; /** * Output information to console * @param indexsearcher * @param query * @throws Exception */public void Sout ( Indexsearcher indexsearcher, query query) throws Exception {//5. Execute query Topdocs Topdocs = Indexsearcher.searc        H (query, 5); 6.        Return query result scoredoc[] Scoredocs = Topdocs.scoredocs;            for (Scoredoc Scoredoc:scoredocs) {//Get document ID int doc = scoredoc.doc; Obtaining documents based on document ID documents document = Indexsearcher.doc (DOC);            File name String filename = document.get ("filename");            File size String fileSize = Document.get ("FileSize");            File path String FilePath = Document.get ("FilePath");            File contents String filecontent = Document.get ("filecontent");            SYSTEM.OUT.PRINTLN ("FileName:" + filename);            System.out.println ("fileSize:" + fileSize);            System.out.println ("FilePath:" + FilePath);            System.out.println ("filecontent:" + filecontent);        System.out.println ("-----------------------");        }}/** * Exact query * * @throws Exception */@Test public void Searchindex () throws Exception { 1.        Gets the query object Indexsearcher indexsearcher = Getsearcher (); 2.        Create a Termquery object that specifies the query's domain and the query's keywords query query = new Termquery (New term ("FileName", "Life"));        Sout (indexsearcher, query); 3. Close the Indexreader object Indexsearcher.getindexreader (). ClOSE (); }

In the query, create a new Term object, enter the exact match. As mentioned in the previous article, each word or passage that is divided by a word breaker is a term.

Here in the new term, the incoming domain name and the word to search.

Here, a term object, there is only one domain, then what if I want to query multiple domains?

Two. Combination Query

/** * Combination query */@Testpublic void Queryboolean () throws Exception {    Indexsearcher searcher = Getsearcher ();    Booleanquery query = new Booleanquery ();    Query Query1 = new Termquery (New term ("FileName", "Life"));    Query Query2 = new Termquery (New term ("Filecontent", "Life"));    Query.add (Query1, BooleanClause.Occur.MUST);    Query.add (Query2, BooleanClause.Occur.SHOULD);    System.out.println (query);    Sout (searcher, query);    Searcher.getindexreader (). Close ();}

Here the occur enumeration values, there are three, must, should, Must_not.

Must: equivalent to and connected to SQL

Should: equivalent to or, can have no

Must_not: Equals! =, does not contain

If you print query here, it will show: +filename: Life filecontent: Life

This is a syntax for Lucene, and Lucene can query the data according to the syntax. will be mentioned later. If Must_not, a minus sign is used.

Such as: The above query2 using Must_not connection, then displayed as: +filename: Life-filecontent: Life

Three. Query all

When querying a database generally, a getAll method is provided to query all data that satisfies the condition, and when the condition is not passed, it queries all

Lucene also provides a way to query all of the methods: Matchalldocsquery

/** * Query All * * @throws Exception */@Testpublic void Queryall () throws Exception {    Indexsearcher searcher = Getsearcher ();    Query query = new Matchalldocsquery ();    Sout (searcher, query);    Searcher.getindexreader (). Close ();}

Four. Numeric interval query

/** * Value Range Query * * @throws Exception */@Testpublic void Querynumericrange () throws Exception {    Indexsearcher searcher = Getsearcher ();    Query query = Numericrangequery.newlongrange ("FileSize", 10L, 647L, True, true);    Sout (searcher, query);    Searcher.getindexreader (). Close ();}

The syntax output here is: filesize:[40 to 647]

This is because the last two of me are set to True, which indicates the containment relationship. If all is set to False, it is {647}

Five. Word breaker parsing query

As mentioned earlier, I entered a sentence query, the results of the show is not according to my input of the full match results.

That is because before the query, the input information, the word breaker parsing, and then based on the results of the analysis, and then to query the data.

/** * Conditional Parse Object query * * @throws Exception */@Testpublic void Queryparser () throws Exception {    Indexsearcher searcher = GetS Earcher ();    Queryparser queryparser = new Queryparser ("FileName", New Ikanalyzer ());    Query query = Queryparser.parse ("*:*");    Query query = Queryparser.parse ("FileName: This flower is nice");    Query query = queryparser.parse ("flower");    Sout (searcher, query);    Searcher.getindexreader (). Close ();}

*:* means query all. Regardless of which domain.

FileName: This flower is beautiful: in the filename field, the word "This flower is nice" is parsed and queried

Flowers: In the filename field, query for flowers. Because when Queryparse was created, the domain was named FileName

Even if I specify the domain to query in Queryparser, I can reassign the domain in parse.

It is important to note that in the above value range query, if I directly write grammar into the query, is not found out. Because the numeric type has changed. Passed through the syntax into a string type.

From the results can be seen, I input this flower is very beautiful, find out is the army of green flowers. This is the role of participle.

Six. Multi-domain Word segmentation query

/** * Conditional Parse Object query * * @throws Exception */@Testpublic void Querymultiparser () throws Exception {    Indexsearcher searcher = Getsearcher ();    string[] fields = {"FileName", "filecontent"};    Multifieldqueryparser queryparser = new Multifieldqueryparser (fields, new Ikanalyzer ());    Query query = Queryparser.parse ("Life Big Bang");    Sout (searcher, query);    Searcher.getindexreader (). Close ();}

Multi-domain Word search, there is nothing to say.

Lucene Exploration-Query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.