Beispiel #1
0
        public void TestQueryPhraseMap1PhraseAnother()
        {
            Query query = PqF("search", "engines");

            // phraseHighlight = true, fieldMatch = true
            FieldQuery fq = new FieldQuery(query, true, true);
            Dictionary <String, QueryPhraseMap> map = fq.rootMaps;

            Assert.AreEqual(1, map.Count);
            Assert.Null(map.Get(null));
            Assert.NotNull(map.Get(F));
            QueryPhraseMap qpm = map.Get(F);

            Assert.AreEqual(1, qpm.subMap.Count);
            Assert.NotNull(qpm.subMap.Get("search"));
            QueryPhraseMap qpm2 = qpm.subMap.Get("search");

            Assert.IsFalse(qpm2.terminal);
            Assert.AreEqual(1, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("engines"));
            QueryPhraseMap qpm3 = qpm2.subMap.Get("engines");

            Assert.IsTrue(qpm3.terminal);
            Assert.AreEqual(1F, qpm3.boost);
        }
Beispiel #2
0
        public void TestQueryPhraseMapOverlapPhrases()
        {
            BooleanQuery query = new BooleanQuery();

            query.Add(PqF("a", "b", "c"), Occur.SHOULD);
            query.Add(PqF(2, "b", "c", "d"), Occur.SHOULD);
            query.Add(PqF(3, "b", "d"), Occur.SHOULD);

            // phraseHighlight = true, fieldMatch = true
            FieldQuery fq = new FieldQuery(query, true, true);
            Dictionary <String, QueryPhraseMap> map = fq.rootMaps;

            Assert.AreEqual(1, map.Count);
            Assert.Null(map.Get(null));
            Assert.NotNull(map.Get(F));
            QueryPhraseMap qpm = map.Get(F);

            Assert.AreEqual(2, qpm.subMap.Count);

            // "a b c"
            Assert.NotNull(qpm.subMap.Get("a"));
            QueryPhraseMap qpm2 = qpm.subMap.Get("a");

            Assert.IsFalse(qpm2.terminal);
            Assert.AreEqual(1, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("b"));
            QueryPhraseMap qpm3 = qpm2.subMap.Get("b");

            Assert.IsFalse(qpm3.terminal);
            Assert.AreEqual(1, qpm3.subMap.Count);
            Assert.NotNull(qpm3.subMap.Get("c"));
            QueryPhraseMap qpm4 = qpm3.subMap.Get("c");

            Assert.IsTrue(qpm4.terminal);
            Assert.AreEqual(1F, qpm4.boost);
            Assert.NotNull(qpm4.subMap.Get("d"));
            QueryPhraseMap qpm5 = qpm4.subMap.Get("d");

            Assert.IsTrue(qpm5.terminal);
            Assert.AreEqual(1F, qpm5.boost);

            // "b c d"^2, "b d"^3
            Assert.NotNull(qpm.subMap.Get("b"));
            qpm2 = qpm.subMap.Get("b");
            Assert.IsFalse(qpm2.terminal);
            Assert.AreEqual(2, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("c"));
            qpm3 = qpm2.subMap.Get("c");
            Assert.IsFalse(qpm3.terminal);
            Assert.AreEqual(1, qpm3.subMap.Count);
            Assert.NotNull(qpm3.subMap.Get("d"));
            qpm4 = qpm3.subMap.Get("d");
            Assert.IsTrue(qpm4.terminal);
            Assert.AreEqual(2F, qpm4.boost);
            Assert.NotNull(qpm2.subMap.Get("d"));
            qpm3 = qpm2.subMap.Get("d");
            Assert.IsTrue(qpm3.terminal);
            Assert.AreEqual(3F, qpm3.boost);
        }
Beispiel #3
0
        public void TestGetRootMapNotFieldMatch()
        {
            Query      dummy = PqF("DUMMY");
            FieldQuery fq    = new FieldQuery(dummy, true, false);

            QueryPhraseMap rootMap1 = fq.getRootMap(Tq("a"));
            QueryPhraseMap rootMap2 = fq.getRootMap(Tq("a"));

            Assert.IsTrue(rootMap1 == rootMap2);
            QueryPhraseMap rootMap3 = fq.getRootMap(Tq("b"));

            Assert.IsTrue(rootMap1 == rootMap3);
            QueryPhraseMap rootMap4 = fq.getRootMap(Tq(F1, "b"));

            Assert.IsTrue(rootMap4 == rootMap3);
        }
Beispiel #4
0
        public void TestGetFieldTermMap()
        {
            Query      query = Tq("a");
            FieldQuery fq    = new FieldQuery(query, true, true);

            QueryPhraseMap pqm = fq.GetFieldTermMap(F, "a");

            Assert.NotNull(pqm);
            Assert.IsTrue(pqm.IsTerminal);

            pqm = fq.GetFieldTermMap(F, "b");
            Assert.Null(pqm);

            pqm = fq.GetFieldTermMap(F1, "a");
            Assert.Null(pqm);
        }
Beispiel #5
0
        public void TestQueryPhraseMapOverlap2gram()
        {
            Query query = paB.Parse("abc AND bcd");

            // phraseHighlight = true, fieldMatch = true
            FieldQuery fq = new FieldQuery(query, true, true);
            Dictionary <String, QueryPhraseMap> map = fq.rootMaps;

            Assert.AreEqual(1, map.Count);
            Assert.Null(map.Get(null));
            Assert.NotNull(map.Get(F));
            QueryPhraseMap qpm = map.Get(F);

            Assert.AreEqual(2, qpm.subMap.Count);

            // "ab bc"
            Assert.NotNull(qpm.subMap.Get("ab"));
            QueryPhraseMap qpm2 = qpm.subMap.Get("ab");

            Assert.IsFalse(qpm2.terminal);
            Assert.AreEqual(1, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("bc"));
            QueryPhraseMap qpm3 = qpm2.subMap.Get("bc");

            Assert.IsTrue(qpm3.terminal);
            Assert.AreEqual(1F, qpm3.boost);

            // "ab bc cd"
            Assert.AreEqual(1, qpm3.subMap.Count);
            Assert.NotNull(qpm3.subMap.Get("cd"));
            QueryPhraseMap qpm4 = qpm3.subMap.Get("cd");

            Assert.IsTrue(qpm4.terminal);
            Assert.AreEqual(1F, qpm4.boost);

            // "bc cd"
            Assert.NotNull(qpm.subMap.Get("bc"));
            qpm2 = qpm.subMap.Get("bc");
            Assert.IsFalse(qpm2.terminal);
            Assert.AreEqual(1, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("cd"));
            qpm3 = qpm2.subMap.Get("cd");
            Assert.IsTrue(qpm3.terminal);
            Assert.AreEqual(1F, qpm3.boost);

            // phraseHighlight = false, fieldMatch = true
            fq  = new FieldQuery(query, false, true);
            map = fq.rootMaps;
            Assert.AreEqual(1, map.Count);
            Assert.Null(map.Get(null));
            Assert.NotNull(map.Get(F));
            qpm = map.Get(F);
            Assert.AreEqual(3, qpm.subMap.Count);

            // "ab bc"
            Assert.NotNull(qpm.subMap.Get("ab"));
            qpm2 = qpm.subMap.Get("ab");
            Assert.IsTrue(qpm2.terminal);
            Assert.AreEqual(1F, qpm2.boost);
            Assert.AreEqual(1, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("bc"));
            qpm3 = qpm2.subMap.Get("bc");
            Assert.IsTrue(qpm3.terminal);
            Assert.AreEqual(1F, qpm3.boost);

            // "ab bc cd"
            Assert.AreEqual(1, qpm3.subMap.Count);
            Assert.NotNull(qpm3.subMap.Get("cd"));
            qpm4 = qpm3.subMap.Get("cd");
            Assert.IsTrue(qpm4.terminal);
            Assert.AreEqual(1F, qpm4.boost);

            // "bc cd"
            Assert.NotNull(qpm.subMap.Get("bc"));
            qpm2 = qpm.subMap.Get("bc");
            Assert.IsTrue(qpm2.terminal);
            Assert.AreEqual(1F, qpm2.boost);
            Assert.AreEqual(1, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("cd"));
            qpm3 = qpm2.subMap.Get("cd");
            Assert.IsTrue(qpm3.terminal);
            Assert.AreEqual(1F, qpm3.boost);

            // "cd"
            Assert.NotNull(qpm.subMap.Get("cd"));
            qpm2 = qpm.subMap.Get("cd");
            Assert.IsTrue(qpm2.terminal);
            Assert.AreEqual(1F, qpm2.boost);
            Assert.AreEqual(0, qpm2.subMap.Count);
        }
Beispiel #6
0
        public void TestQueryPhraseMap2PhrasesFields()
        {
            BooleanQuery query = new BooleanQuery();

            query.Add(Pq(F1, "a", "b"), Occur.SHOULD);
            query.Add(Pq(2F, F2, "c", "d"), Occur.SHOULD);

            // phraseHighlight = true, fieldMatch = true
            FieldQuery fq = new FieldQuery(query, true, true);
            HashMap <String, QueryPhraseMap> map = fq.rootMaps;

            Assert.AreEqual(2, map.Count);
            Assert.Null(map.Get(null));

            // "a b"
            Assert.NotNull(map.Get(F1));
            QueryPhraseMap qpm = map.Get(F1);

            Assert.AreEqual(1, qpm.subMap.Count);
            Assert.NotNull(qpm.subMap.Get("a"));
            QueryPhraseMap qpm2 = qpm.subMap.Get("a");

            Assert.IsFalse(qpm2.terminal);
            Assert.AreEqual(1, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("b"));
            QueryPhraseMap qpm3 = qpm2.subMap.Get("b");

            Assert.IsTrue(qpm3.terminal);
            Assert.AreEqual(1F, qpm3.boost);

            // "c d"^2
            Assert.NotNull(map.Get(F2));
            qpm = map.Get(F2);
            Assert.AreEqual(1, qpm.subMap.Count);
            Assert.NotNull(qpm.subMap.Get("c"));
            qpm2 = qpm.subMap.Get("c");
            Assert.IsFalse(qpm2.terminal);
            Assert.AreEqual(1, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("d"));
            qpm3 = qpm2.subMap.Get("d");
            Assert.IsTrue(qpm3.terminal);
            Assert.AreEqual(2F, qpm3.boost);

            // phraseHighlight = true, fieldMatch = false
            fq  = new FieldQuery(query, true, false);
            map = fq.rootMaps;
            Assert.AreEqual(1, map.Count);
            Assert.Null(map.Get(F1));
            Assert.Null(map.Get(F2));
            Assert.NotNull(map.Get(null));
            qpm = map.Get(null);
            Assert.AreEqual(2, qpm.subMap.Count);

            // "a b"
            Assert.NotNull(qpm.subMap.Get("a"));
            qpm2 = qpm.subMap.Get("a");
            Assert.IsFalse(qpm2.terminal);
            Assert.AreEqual(1, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("b"));
            qpm3 = qpm2.subMap.Get("b");
            Assert.IsTrue(qpm3.terminal);
            Assert.AreEqual(1F, qpm3.boost);

            // "c d"^2
            Assert.NotNull(qpm.subMap.Get("c"));
            qpm2 = qpm.subMap.Get("c");
            Assert.IsFalse(qpm2.terminal);
            Assert.AreEqual(1, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("d"));
            qpm3 = qpm2.subMap.Get("d");
            Assert.IsTrue(qpm3.terminal);
            Assert.AreEqual(2F, qpm3.boost);
        }
Beispiel #7
0
        public void TestQueryPhraseMap1Phrase()
        {
            Query query = PqF("a", "b");

            // phraseHighlight = true, fieldMatch = true
            FieldQuery fq = new FieldQuery(query, true, true);
            HashMap <String, QueryPhraseMap> map = fq.rootMaps;

            Assert.AreEqual(1, map.Count);
            Assert.Null(map.Get(null));
            Assert.NotNull(map.Get(F));
            QueryPhraseMap qpm = map.Get(F);

            Assert.AreEqual(1, qpm.subMap.Count);
            Assert.NotNull(qpm.subMap.Get("a"));
            QueryPhraseMap qpm2 = qpm.subMap.Get("a");

            Assert.IsFalse(qpm2.terminal);
            Assert.AreEqual(1, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("b"));
            QueryPhraseMap qpm3 = qpm2.subMap.Get("b");

            Assert.IsTrue(qpm3.terminal);
            Assert.AreEqual(1F, qpm3.boost);

            // phraseHighlight = true, fieldMatch = false
            fq  = new FieldQuery(query, true, false);
            map = fq.rootMaps;
            Assert.AreEqual(1, map.Count);
            Assert.Null(map.Get(F));
            Assert.NotNull(map.Get(null));
            qpm = map.Get(null);
            Assert.AreEqual(1, qpm.subMap.Count);
            Assert.NotNull(qpm.subMap.Get("a"));
            qpm2 = qpm.subMap.Get("a");
            Assert.IsFalse(qpm2.terminal);
            Assert.AreEqual(1, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("b"));
            qpm3 = qpm2.subMap.Get("b");
            Assert.IsTrue(qpm3.terminal);
            Assert.AreEqual(1F, qpm3.boost);

            // phraseHighlight = false, fieldMatch = true
            fq  = new FieldQuery(query, false, true);
            map = fq.rootMaps;
            Assert.AreEqual(1, map.Count);
            Assert.Null(map.Get(null));
            Assert.NotNull(map.Get(F));
            qpm = map.Get(F);
            Assert.AreEqual(2, qpm.subMap.Count);
            Assert.NotNull(qpm.subMap.Get("a"));
            qpm2 = qpm.subMap.Get("a");
            Assert.IsTrue(qpm2.terminal);
            Assert.AreEqual(1F, qpm2.boost);
            Assert.AreEqual(1, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("b"));
            qpm3 = qpm2.subMap.Get("b");
            Assert.IsTrue(qpm3.terminal);
            Assert.AreEqual(1F, qpm3.boost);

            Assert.NotNull(qpm.subMap.Get("b"));
            qpm2 = qpm.subMap.Get("b");
            Assert.IsTrue(qpm2.terminal);
            Assert.AreEqual(1F, qpm2.boost);

            // phraseHighlight = false, fieldMatch = false
            fq  = new FieldQuery(query, false, false);
            map = fq.rootMaps;
            Assert.AreEqual(1, map.Count);
            Assert.Null(map.Get(F));
            Assert.NotNull(map.Get(null));
            qpm = map.Get(null);
            Assert.AreEqual(2, qpm.subMap.Count);
            Assert.NotNull(qpm.subMap.Get("a"));
            qpm2 = qpm.subMap.Get("a");
            Assert.IsTrue(qpm2.terminal);
            Assert.AreEqual(1F, qpm2.boost);
            Assert.AreEqual(1, qpm2.subMap.Count);
            Assert.NotNull(qpm2.subMap.Get("b"));
            qpm3 = qpm2.subMap.Get("b");
            Assert.IsTrue(qpm3.terminal);
            Assert.AreEqual(1F, qpm3.boost);

            Assert.NotNull(qpm.subMap.Get("b"));
            qpm2 = qpm.subMap.Get("b");
            Assert.IsTrue(qpm2.terminal);
            Assert.AreEqual(1F, qpm2.boost);

            // boost != 1
            query = PqF(2, "a", "b");
            // phraseHighlight = false, fieldMatch = false
            fq   = new FieldQuery(query, false, false);
            map  = fq.rootMaps;
            qpm  = map.Get(null);
            qpm2 = qpm.subMap.Get("a");
            Assert.AreEqual(2F, qpm2.boost);
            qpm3 = qpm2.subMap.Get("b");
            Assert.AreEqual(2F, qpm3.boost);
            qpm2 = qpm.subMap.Get("b");
            Assert.AreEqual(2F, qpm2.boost);
        }
        /// <summary>
        /// a constructor.
        /// </summary>
        /// <param name="fieldTermStack">FieldTermStack object</param>
        /// <param name="fieldQuery">FieldQuery object</param>
        public FieldPhraseList(FieldTermStack fieldTermStack, FieldQuery fieldQuery)
        {
            String field = fieldTermStack.GetFieldName();

            LinkedList <TermInfo> phraseCandidate = new LinkedList <TermInfo>();
            QueryPhraseMap        currMap         = null;
            QueryPhraseMap        nextMap         = null;

            while (!fieldTermStack.IsEmpty())
            {
                phraseCandidate.Clear();

                TermInfo ti = fieldTermStack.Pop();
                currMap = fieldQuery.GetFieldTermMap(field, ti.GetText());

                // if not found, discard top TermInfo from stack, then try next element
                if (currMap == null)
                {
                    continue;
                }

                // if found, search the longest phrase
                phraseCandidate.AddLast(ti);
                while (true)
                {
                    ti      = fieldTermStack.Pop();
                    nextMap = null;
                    if (ti != null)
                    {
                        nextMap = currMap.GetTermMap(ti.GetText());
                    }
                    if (ti == null || nextMap == null)
                    {
                        if (ti != null)
                        {
                            fieldTermStack.Push(ti);
                        }
                        if (currMap.IsValidTermOrPhrase(new List <TermInfo>(phraseCandidate)))
                        {
                            AddIfNoOverlap(new WeightedPhraseInfo(phraseCandidate, currMap.GetBoost(), currMap.GetTermOrPhraseNumber()));
                        }
                        else
                        {
                            while (phraseCandidate.Count > 1)
                            {
                                TermInfo last = phraseCandidate.Last.Value;
                                phraseCandidate.RemoveLast();
                                fieldTermStack.Push(last);
                                currMap = fieldQuery.SearchPhrase(field, new List <TermInfo>(phraseCandidate));
                                if (currMap != null)
                                {
                                    AddIfNoOverlap(new WeightedPhraseInfo(phraseCandidate, currMap.GetBoost(), currMap.GetTermOrPhraseNumber()));
                                    break;
                                }
                            }
                        }
                        break;
                    }
                    else
                    {
                        phraseCandidate.AddLast(ti);
                        currMap = nextMap;
                    }
                }
            }
        }