forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 3
/
BooleanModifiersQueryNodeProcessor.cs
125 lines (106 loc) · 4.64 KB
/
BooleanModifiersQueryNodeProcessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Lucene.Net.QueryParsers.Flexible.Core.Nodes;
using Lucene.Net.QueryParsers.Flexible.Core.Processors;
using Lucene.Net.QueryParsers.Flexible.Standard.Config;
using System;
using System.Collections.Generic;
using Operator = Lucene.Net.QueryParsers.Flexible.Standard.Config.StandardQueryConfigHandler.Operator;
namespace Lucene.Net.QueryParsers.Flexible.Precedence.Processors
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <summary>
/// This processor is used to apply the correct <see cref="ModifierQueryNode"/> to <see cref="BooleanQueryNode"/>s children.
/// <para>
/// It walks through the query node tree looking for <see cref="BooleanQueryNode"/>s. If an <see cref="AndQueryNode"/> is found,
/// every child, which is not a <see cref="ModifierQueryNode"/> or the <see cref="ModifierQueryNode"/>
/// is <see cref="Modifier.MOD_NONE"/>, becomes a <see cref="Modifier.MOD_REQ"/>. For any other
/// <see cref="BooleanQueryNode"/> which is not an <see cref="OrQueryNode"/>, it checks the default operator is <see cref="Operator.AND"/>,
/// if it is, the same operation when an <see cref="AndQueryNode"/> is found is applied to it.
/// </para>
/// </summary>
/// <seealso cref="ConfigurationKeys.DEFAULT_OPERATOR"/>
public class BooleanModifiersQueryNodeProcessor : QueryNodeProcessor
{
private List<IQueryNode> childrenBuffer = new List<IQueryNode>();
private bool usingAnd = false;
public BooleanModifiersQueryNodeProcessor()
{
// empty constructor
}
public override IQueryNode Process(IQueryNode queryTree)
{
Operator? op = GetQueryConfigHandler().Get(ConfigurationKeys.DEFAULT_OPERATOR);
if (op == null)
{
throw new ArgumentException(
"StandardQueryConfigHandler.ConfigurationKeys.DEFAULT_OPERATOR should be set on the QueryConfigHandler");
}
this.usingAnd = Operator.AND == op;
return base.Process(queryTree);
}
protected override IQueryNode PostProcessNode(IQueryNode node)
{
if (node is AndQueryNode)
{
this.childrenBuffer.Clear();
IList<IQueryNode> children = node.GetChildren();
foreach (IQueryNode child in children)
{
this.childrenBuffer.Add(ApplyModifier(child, Modifier.MOD_REQ));
}
node.Set(this.childrenBuffer);
}
else if (this.usingAnd && node is BooleanQueryNode
&& !(node is OrQueryNode))
{
this.childrenBuffer.Clear();
IList<IQueryNode> children = node.GetChildren();
foreach (IQueryNode child in children)
{
this.childrenBuffer.Add(ApplyModifier(child, Modifier.MOD_REQ));
}
node.Set(this.childrenBuffer);
}
return node;
}
private IQueryNode ApplyModifier(IQueryNode node, Modifier mod)
{
// check if modifier is not already defined and is default
if (!(node is ModifierQueryNode))
{
return new ModifierQueryNode(node, mod);
}
else
{
ModifierQueryNode modNode = (ModifierQueryNode)node;
if (modNode.Modifier == Modifier.MOD_NONE)
{
return new ModifierQueryNode(modNode.GetChild(), mod);
}
}
return node;
}
protected override IQueryNode PreProcessNode(IQueryNode node)
{
return node;
}
protected override IList<IQueryNode> SetChildrenOrder(IList<IQueryNode> children)
{
return children;
}
}
}