How do I search AND in Lucene.net when multiple words are used in a search?

I play with Lucene.net to try to get an idea of ​​how to implement it in my application.

I have the following code

            .....
            // Add 2 documents
            var doc1 = new Document();
            var doc2 = new Document();

            doc1.Add(new Field("id", "doc1", Field.Store.YES, Field.Index.ANALYZED));
            doc1.Add(new Field("content", "This is my first document", Field.Store.YES, Field.Index.ANALYZED));
            doc2.Add(new Field("id", "doc2", Field.Store.YES, Field.Index.ANALYZED));
            doc2.Add(new Field("content", "The big red fox jumped", Field.Store.YES, Field.Index.ANALYZED));

            writer.AddDocument(doc1);
            writer.AddDocument(doc2);

            writer.Optimize();
            writer.Close();

            // Search for doc2
            var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "content", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
            var query = parser.Parse("big abcdefg test1234");
            var searcher = new IndexSearcher(indexDirectory, true);
            var hits = searcher.Search(query);

            Assert.AreEqual(1, hits.Length());

            var document = hits.Doc(0);

            Assert.AreEqual("doc2", document.Get("id"));
            Assert.AreEqual("The big red fox jumped", document.Get("content"));

This test passes, which confuses me a bit. I assume this means that Lucene.Net uses OR to search between terms, not AND, but I cannot find any information on how to actually perform AND searches.

The end result that I am going to is that if someone is looking for "Matthew Anderson", I do not want him to pick up documents that refer to "Matthew Dow", as this has no relation, form or form.

+5
2

. , , , :

+big +red

* the big red fox jumped
* the red big fox jumped
* the big fast red fox jumped

* the small red fox jumped

. (.. , ):

+"big red"

* the big red fox jumped

* the red big fox jumped
* the big fast red fox jumped
* the small red fox jumped
+7

, var query = parser.Parse("+big +abcdefg +test1234"); , . - .

BooleanQuery query = new BooleanQuery();
query.add(new BooleanClause(new TermQuery(new Term("field", "big"))), Occur.MUST);
query.add(new BooleanClause(new TermQuery(new Term("field", "abcdefg"))), Occur.MUST);
query.add(new BooleanClause(new TermQuery(new Term("field", "test1234"))), Occur.MUST);
+2

All Articles