You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2016/12/07 13:48:26 UTC
[08/11] lucenenet git commit: Ported QueryParser.Xml + tests
Ported QueryParser.Xml + tests
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/2ae0b1bb
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/2ae0b1bb
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/2ae0b1bb
Branch: refs/heads/master
Commit: 2ae0b1bb080cd1bcf30609be66c3af67e82ef64d
Parents: 567c46c
Author: Shad Storhaug <sh...@shadstorhaug.com>
Authored: Wed Dec 7 19:22:56 2016 +0700
Committer: Shad Storhaug <sh...@shadstorhaug.com>
Committed: Wed Dec 7 19:24:32 2016 +0700
----------------------------------------------------------------------
.../Lucene.Net.QueryParser.csproj | 45 ++
.../Xml/Builders/BooleanFilterBuilder.cs | 59 +++
.../Xml/Builders/BooleanQueryBuilder.cs | 98 ++++
.../Xml/Builders/BoostingQueryBuilder.cs | 55 ++
.../Xml/Builders/BoostingTermBuilder.cs | 40 ++
.../Xml/Builders/CachedFilterBuilder.cs | 108 ++++
.../Xml/Builders/ConstantScoreQueryBuilder.cs | 44 ++
.../Xml/Builders/DisjunctionMaxQueryBuilder.cs | 60 +++
.../Xml/Builders/DuplicateFilterBuilder.cs | 66 +++
.../Xml/Builders/FilteredQueryBuilder.cs | 57 +++
.../Xml/Builders/FuzzyLikeThisQueryBuilder.cs | 71 +++
.../Xml/Builders/LikeThisQueryBuilder.cs | 116 +++++
.../Xml/Builders/MatchAllDocsQueryBuilder.cs | 33 ++
.../Xml/Builders/NumericRangeFilterBuilder.cs | 169 +++++++
.../Xml/Builders/NumericRangeQueryBuilder.cs | 136 +++++
.../Xml/Builders/RangeFilterBuilder.cs | 39 ++
.../Xml/Builders/SpanBuilderBase.cs | 40 ++
.../Xml/Builders/SpanFirstBuilder.cs | 47 ++
.../Xml/Builders/SpanNearBuilder.cs | 54 ++
.../Xml/Builders/SpanNotBuilder.cs | 52 ++
.../Xml/Builders/SpanOrBuilder.cs | 53 ++
.../Xml/Builders/SpanOrTermsBuilder.cs | 78 +++
.../Xml/Builders/SpanQueryBuilder.cs | 30 ++
.../Xml/Builders/SpanQueryBuilderFactory.cs | 52 ++
.../Xml/Builders/SpanTermBuilder.cs | 39 ++
.../Xml/Builders/TermQueryBuilder.cs | 38 ++
.../Xml/Builders/TermsFilterBuilder.cs | 77 +++
.../Xml/Builders/TermsQueryBuilder.cs | 77 +++
.../Xml/Builders/UserInputQueryBuilder.cs | 100 ++++
src/Lucene.Net.QueryParser/Xml/CoreParser.cs | 185 +++++++
.../Xml/CorePlusExtensionsParser.cs | 64 +++
src/Lucene.Net.QueryParser/Xml/DOMUtils.cs | 276 ++++++++++
src/Lucene.Net.QueryParser/Xml/FilterBuilder.cs | 30 ++
.../Xml/FilterBuilderFactory.cs | 53 ++
.../Xml/ParserException.cs | 49 ++
src/Lucene.Net.QueryParser/Xml/QueryBuilder.cs | 31 ++
.../Xml/QueryBuilderFactory.cs | 53 ++
.../Xml/QueryTemplateManager.cs | 191 +++++++
.../Lucene.Net.Tests.QueryParser.csproj | 31 ++
.../Util/QueryParserTestBase.cs | 2 +-
.../Xml/BooleanFilter.xml | 41 ++
.../Xml/BooleanQuery.xml | 28 ++
.../Xml/BoostingQuery.xml | 33 ++
.../Xml/BoostingTermQuery.xml | 18 +
.../Builders/TestNumericRangeFilterBuilder.cs | 217 ++++++++
.../Builders/TestNumericRangeQueryBuilder.cs | 177 +++++++
.../Xml/CachedFilter.xml | 57 +++
.../Xml/ConstantScoreQuery.xml | 20 +
.../Xml/DisjunctionMaxQuery.xml | 24 +
.../Xml/DuplicateFilterQuery.xml | 34 ++
.../Xml/FuzzyLikeThisQuery.xml | 23 +
.../Xml/LikeThisQuery.xml | 33 ++
.../Xml/MatchAllDocsQuery.xml | 27 +
.../Xml/NestedBooleanQuery.xml | 36 ++
.../Xml/NumericRangeFilterQuery.xml | 37 ++
.../Xml/NumericRangeQueryQuery.xml | 31 ++
.../Xml/RangeFilterQuery.xml | 37 ++
.../Xml/SpanQuery.xml | 55 ++
.../Xml/TermQuery.xml | 18 +
.../Xml/TermsFilterQuery.xml | 46 ++
.../Xml/TermsQuery.xml | 26 +
.../Xml/TestParser.cs | 279 +++++++++++
.../Xml/TestQueryTemplateManager.cs | 185 +++++++
.../Xml/UserInputQuery.xml | 26 +
.../Xml/UserInputQueryCustomField.xml | 18 +
.../Xml/albumBooleanQuery.xsl | 48 ++
.../Xml/albumFilteredQuery.xsl | 54 ++
.../Xml/albumLuceneClassicQuery.xsl | 29 ++
.../Xml/reuters21578.txt | 498 +++++++++++++++++++
69 files changed, 5052 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Lucene.Net.QueryParser.csproj
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Lucene.Net.QueryParser.csproj b/src/Lucene.Net.QueryParser/Lucene.Net.QueryParser.csproj
index a264871..bee959e 100644
--- a/src/Lucene.Net.QueryParser/Lucene.Net.QueryParser.csproj
+++ b/src/Lucene.Net.QueryParser/Lucene.Net.QueryParser.csproj
@@ -215,8 +215,49 @@
<Compile Include="Surround\Query\SrndTermQuery.cs" />
<Compile Include="Surround\Query\SrndTruncQuery.cs" />
<Compile Include="Surround\Query\TooManyBasicQueries.cs" />
+ <Compile Include="Xml\Builders\BooleanFilterBuilder.cs" />
+ <Compile Include="Xml\Builders\BooleanQueryBuilder.cs" />
+ <Compile Include="Xml\Builders\BoostingQueryBuilder.cs" />
+ <Compile Include="Xml\Builders\BoostingTermBuilder.cs" />
+ <Compile Include="Xml\Builders\CachedFilterBuilder.cs" />
+ <Compile Include="Xml\Builders\ConstantScoreQueryBuilder.cs" />
+ <Compile Include="Xml\Builders\DisjunctionMaxQueryBuilder.cs" />
+ <Compile Include="Xml\Builders\DuplicateFilterBuilder.cs" />
+ <Compile Include="Xml\Builders\FilteredQueryBuilder.cs" />
+ <Compile Include="Xml\Builders\FuzzyLikeThisQueryBuilder.cs" />
+ <Compile Include="Xml\Builders\LikeThisQueryBuilder.cs" />
+ <Compile Include="Xml\Builders\MatchAllDocsQueryBuilder.cs" />
+ <Compile Include="Xml\Builders\NumericRangeFilterBuilder.cs" />
+ <Compile Include="Xml\Builders\NumericRangeQueryBuilder.cs" />
+ <Compile Include="Xml\Builders\RangeFilterBuilder.cs" />
+ <Compile Include="Xml\Builders\SpanBuilderBase.cs" />
+ <Compile Include="Xml\Builders\SpanFirstBuilder.cs" />
+ <Compile Include="Xml\Builders\SpanNearBuilder.cs" />
+ <Compile Include="Xml\Builders\SpanNotBuilder.cs" />
+ <Compile Include="Xml\Builders\SpanOrBuilder.cs" />
+ <Compile Include="Xml\Builders\SpanOrTermsBuilder.cs" />
+ <Compile Include="Xml\Builders\SpanQueryBuilder.cs" />
+ <Compile Include="Xml\Builders\SpanQueryBuilderFactory.cs" />
+ <Compile Include="Xml\Builders\SpanTermBuilder.cs" />
+ <Compile Include="Xml\Builders\TermQueryBuilder.cs" />
+ <Compile Include="Xml\Builders\TermsFilterBuilder.cs" />
+ <Compile Include="Xml\Builders\TermsQueryBuilder.cs" />
+ <Compile Include="Xml\Builders\UserInputQueryBuilder.cs" />
+ <Compile Include="Xml\CoreParser.cs" />
+ <Compile Include="Xml\CorePlusExtensionsParser.cs" />
+ <Compile Include="Xml\DOMUtils.cs" />
+ <Compile Include="Xml\FilterBuilder.cs" />
+ <Compile Include="Xml\FilterBuilderFactory.cs" />
+ <Compile Include="Xml\ParserException.cs" />
+ <Compile Include="Xml\QueryBuilder.cs" />
+ <Compile Include="Xml\QueryBuilderFactory.cs" />
+ <Compile Include="Xml\QueryTemplateManager.cs" />
</ItemGroup>
<ItemGroup>
+ <ProjectReference Include="..\..\Lucene.Net.Sandbox\Lucene.Net.Sandbox.csproj">
+ <Project>{13274BA9-9052-4354-8FFE-E3F32593368F}</Project>
+ <Name>Lucene.Net.Sandbox</Name>
+ </ProjectReference>
<ProjectReference Include="..\Lucene.Net.Analysis.Common\Lucene.Net.Analysis.Common.csproj">
<Project>{4add0bbc-b900-4715-9526-d871de8eea64}</Project>
<Name>Lucene.Net.Analysis.Common</Name>
@@ -225,6 +266,10 @@
<Project>{5d4ad9be-1ffb-41ab-9943-25737971bf57}</Project>
<Name>Lucene.Net</Name>
</ProjectReference>
+ <ProjectReference Include="..\Lucene.Net.Queries\Lucene.Net.Queries.csproj">
+ <Project>{69d7956c-c2cc-4708-b399-a188fec384c4}</Project>
+ <Name>Lucene.Net.Queries</Name>
+ </ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Flexible\Core\Messages\QueryParserMessagesBundle.resx">
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/BooleanFilterBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/BooleanFilterBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/BooleanFilterBuilder.cs
new file mode 100644
index 0000000..4a12d6d
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/BooleanFilterBuilder.cs
@@ -0,0 +1,59 @@
+\ufeffusing Lucene.Net.Queries;
+using Lucene.Net.Search;
+using System;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="BooleanFilter"/>
+ /// </summary>
+ public class BooleanFilterBuilder : IFilterBuilder
+ {
+ private readonly IFilterBuilder factory;
+
+ public BooleanFilterBuilder(IFilterBuilder factory)
+ {
+ this.factory = factory;
+ }
+
+ public virtual Filter GetFilter(XmlElement e)
+ {
+ BooleanFilter bf = new BooleanFilter();
+ XmlNodeList nl = e.ChildNodes;
+
+ for (int i = 0; i < nl.Count; i++)
+ {
+ XmlNode node = nl.Item(i);
+ if (node.LocalName.Equals("Clause", StringComparison.Ordinal))
+ {
+ XmlElement clauseElem = (XmlElement)node;
+ BooleanClause.Occur occurs = BooleanQueryBuilder.GetOccursValue(clauseElem);
+
+ XmlElement clauseFilter = DOMUtils.GetFirstChildOrFail(clauseElem);
+ Filter f = factory.GetFilter(clauseFilter);
+ bf.Add(new FilterClause(f, occurs));
+ }
+ }
+
+ return bf;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/BooleanQueryBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/BooleanQueryBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/BooleanQueryBuilder.cs
new file mode 100644
index 0000000..973aa5a
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/BooleanQueryBuilder.cs
@@ -0,0 +1,98 @@
+\ufeffusing Lucene.Net.Search;
+using System;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="BooleanQuery"/>
+ /// </summary>
+ public class BooleanQueryBuilder : IQueryBuilder
+ {
+ private readonly IQueryBuilder factory;
+
+ public BooleanQueryBuilder(IQueryBuilder factory)
+ {
+ this.factory = factory;
+ }
+
+ /// <summary>
+ /// (non-Javadoc)
+ /// @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
+ /// </summary>
+ /// <param name="e"></param>
+ /// <returns></returns>
+ public virtual Query GetQuery(XmlElement e)
+ {
+ BooleanQuery bq = new BooleanQuery(DOMUtils.GetAttribute(e, "disableCoord", false));
+ bq.MinimumNumberShouldMatch = DOMUtils.GetAttribute(e, "minimumNumberShouldMatch", 0);
+ bq.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
+
+ XmlNodeList nl = e.ChildNodes;
+ for (int i = 0; i < nl.Count; i++)
+ {
+ XmlNode node = nl.Item(i);
+ if (node.LocalName.Equals("Clause", StringComparison.Ordinal))
+ {
+ XmlElement clauseElem = (XmlElement)node;
+ BooleanClause.Occur occurs = GetOccursValue(clauseElem);
+
+ XmlElement clauseQuery = DOMUtils.GetFirstChildOrFail(clauseElem);
+ Query q = factory.GetQuery(clauseQuery);
+ bq.Add(new BooleanClause(q, occurs));
+ }
+ }
+
+ return bq;
+ }
+
+ internal static BooleanClause.Occur GetOccursValue(XmlElement clauseElem)
+ {
+ string occs = clauseElem.GetAttribute("occurs");
+ BooleanClause.Occur occurs = BooleanClause.Occur.SHOULD;
+ if ("must".Equals(occs, StringComparison.OrdinalIgnoreCase))
+ {
+ occurs = BooleanClause.Occur.MUST;
+ }
+ else
+ {
+ if ("mustNot".Equals(occs, StringComparison.OrdinalIgnoreCase))
+ {
+ occurs = BooleanClause.Occur.MUST_NOT;
+ }
+ else
+ {
+ if (("should".Equals(occs, StringComparison.OrdinalIgnoreCase)) || ("".Equals(occs)))
+ {
+ occurs = BooleanClause.Occur.SHOULD;
+ }
+ else
+ {
+ if (occs != null)
+ {
+ throw new ParserException("Invalid value for \"occurs\" attribute of clause:" + occs);
+ }
+ }
+ }
+ }
+ return occurs;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/BoostingQueryBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/BoostingQueryBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/BoostingQueryBuilder.cs
new file mode 100644
index 0000000..96f4bcd
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/BoostingQueryBuilder.cs
@@ -0,0 +1,55 @@
+\ufeffusing Lucene.Net.Queries;
+using Lucene.Net.Search;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="BoostingQuery"/>
+ /// </summary>
+ public class BoostingQueryBuilder : IQueryBuilder
+ {
+ private static float DEFAULT_BOOST = 0.01f;
+
+ private readonly IQueryBuilder factory;
+
+ public BoostingQueryBuilder(IQueryBuilder factory)
+ {
+ this.factory = factory;
+ }
+
+ public virtual Query GetQuery(XmlElement e)
+ {
+ XmlElement mainQueryElem = DOMUtils.GetChildByTagOrFail(e, "Query");
+ mainQueryElem = DOMUtils.GetFirstChildOrFail(mainQueryElem);
+ Query mainQuery = factory.GetQuery(mainQueryElem);
+
+ XmlElement boostQueryElem = DOMUtils.GetChildByTagOrFail(e, "BoostQuery");
+ float boost = DOMUtils.GetAttribute(boostQueryElem, "boost", DEFAULT_BOOST);
+ boostQueryElem = DOMUtils.GetFirstChildOrFail(boostQueryElem);
+ Query boostQuery = factory.GetQuery(boostQueryElem);
+
+ BoostingQuery bq = new BoostingQuery(mainQuery, boostQuery, boost);
+
+ bq.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
+ return bq;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/BoostingTermBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/BoostingTermBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/BoostingTermBuilder.cs
new file mode 100644
index 0000000..72fb353
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/BoostingTermBuilder.cs
@@ -0,0 +1,40 @@
+\ufeffusing Lucene.Net.Index;
+using Lucene.Net.Search.Payloads;
+using Lucene.Net.Search.Spans;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="PayloadTermQuery"/>
+ /// </summary>
+ public class BoostingTermBuilder : SpanBuilderBase
+ {
+ public override SpanQuery GetSpanQuery(XmlElement e)
+ {
+ string fieldName = DOMUtils.GetAttributeWithInheritanceOrFail(e, "fieldName");
+ string value = DOMUtils.GetNonBlankTextOrFail(e);
+
+ PayloadTermQuery btq = new PayloadTermQuery(new Term(fieldName, value), new AveragePayloadFunction());
+ btq.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
+ return btq;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/CachedFilterBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/CachedFilterBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/CachedFilterBuilder.cs
new file mode 100644
index 0000000..93fff2d
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/CachedFilterBuilder.cs
@@ -0,0 +1,108 @@
+\ufeffusing Lucene.Net.Search;
+using Lucene.Net.Support;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Filters are cached in an LRU Cache keyed on the contained query or filter object. Using this will
+ /// speed up overall performance for repeated uses of the same expensive query/filter. The sorts of
+ /// queries/filters likely to benefit from caching need not necessarily be complex - e.g. simple
+ /// TermQuerys with a large DF (document frequency) can be expensive on large indexes.
+ /// A good example of this might be a term query on a field with only 2 possible values -
+ /// "true" or "false". In a large index, querying or filtering on this field requires reading
+ /// millions of document ids from disk which can more usefully be cached as a filter bitset.
+ /// <para/>
+ /// For Queries/Filters to be cached and reused the object must implement hashcode and
+ /// equals methods correctly so that duplicate queries/filters can be detected in the cache.
+ /// <para/>
+ /// The CoreParser.maxNumCachedFilters property can be used to control the size of the LRU
+ /// Cache established during the construction of CoreParser instances.
+ /// </summary>
+ public class CachedFilterBuilder : IFilterBuilder
+ {
+ private readonly QueryBuilderFactory queryFactory;
+ private readonly FilterBuilderFactory filterFactory;
+
+ private LurchTable<object, Filter> filterCache;
+
+ private readonly int cacheSize;
+
+ public CachedFilterBuilder(QueryBuilderFactory queryFactory,
+ FilterBuilderFactory filterFactory,
+ int cacheSize)
+ {
+ this.queryFactory = queryFactory;
+ this.filterFactory = filterFactory;
+ this.cacheSize = cacheSize;
+ }
+
+
+ public virtual Filter GetFilter(XmlElement e)
+ {
+ lock (this)
+ {
+ XmlElement childElement = DOMUtils.GetFirstChildOrFail(e);
+
+ if (filterCache == null)
+ {
+ filterCache = new LurchTable<object, Filter>(LurchTableOrder.Access, cacheSize);
+ }
+
+ // Test to see if child Element is a query or filter that needs to be
+ // cached
+ IQueryBuilder qb = queryFactory.GetQueryBuilder(childElement.Name);
+ object cacheKey = null;
+ Query q = null;
+ Filter f = null;
+ if (qb != null)
+ {
+ q = qb.GetQuery(childElement);
+ cacheKey = q;
+ }
+ else
+ {
+ f = filterFactory.GetFilter(childElement);
+ cacheKey = f;
+ }
+ Filter cachedFilter;
+ if (filterCache.TryGetValue(cacheKey, out cachedFilter) && cachedFilter != null)
+ {
+ return cachedFilter; // cache hit
+ }
+
+ //cache miss
+ if (qb != null)
+ {
+ cachedFilter = new QueryWrapperFilter(q);
+ }
+ else
+ {
+ cachedFilter = new CachingWrapperFilter(f);
+ }
+
+ filterCache[cacheKey] = cachedFilter;
+ return cachedFilter;
+ }
+ }
+
+ // LUCENENET NOTE: LRUCache replaced with LurchTable
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/ConstantScoreQueryBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/ConstantScoreQueryBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/ConstantScoreQueryBuilder.cs
new file mode 100644
index 0000000..7905b17
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/ConstantScoreQueryBuilder.cs
@@ -0,0 +1,44 @@
+\ufeffusing Lucene.Net.Search;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="ConstantScoreQuery"/>
+ /// </summary>
+ public class ConstantScoreQueryBuilder : IQueryBuilder
+ {
+ private readonly FilterBuilderFactory filterFactory;
+
+ public ConstantScoreQueryBuilder(FilterBuilderFactory filterFactory)
+ {
+ this.filterFactory = filterFactory;
+ }
+
+ public virtual Query GetQuery(XmlElement e)
+ {
+ XmlElement filterElem = DOMUtils.GetFirstChildOrFail(e);
+
+ Query q = new ConstantScoreQuery(filterFactory.GetFilter(filterElem));
+ q.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
+ return q;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/DisjunctionMaxQueryBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/DisjunctionMaxQueryBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/DisjunctionMaxQueryBuilder.cs
new file mode 100644
index 0000000..399a529
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/DisjunctionMaxQueryBuilder.cs
@@ -0,0 +1,60 @@
+\ufeffusing Lucene.Net.Search;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="DisjunctionMaxQuery"/>
+ /// </summary>
+ public class DisjunctionMaxQueryBuilder : IQueryBuilder
+ {
+ private readonly IQueryBuilder factory;
+
+ public DisjunctionMaxQueryBuilder(IQueryBuilder factory)
+ {
+ this.factory = factory;
+ }
+
+ /// <summary>
+ /// (non-Javadoc)
+ /// @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
+ /// </summary>
+ public virtual Query GetQuery(XmlElement e)
+ {
+ float tieBreaker = DOMUtils.GetAttribute(e, "tieBreaker", 0.0f);
+ DisjunctionMaxQuery dq = new DisjunctionMaxQuery(tieBreaker);
+ dq.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
+
+ XmlNodeList nl = e.ChildNodes;
+ for (int i = 0; i < nl.Count; i++)
+ {
+ XmlNode node = nl.Item(i);
+ if (node is XmlElement)
+ { // all elements are disjuncts.
+ XmlElement queryElem = (XmlElement)node;
+ Query q = factory.GetQuery(queryElem);
+ dq.Add(q);
+ }
+ }
+
+ return dq;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/DuplicateFilterBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/DuplicateFilterBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/DuplicateFilterBuilder.cs
new file mode 100644
index 0000000..8c9d845
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/DuplicateFilterBuilder.cs
@@ -0,0 +1,66 @@
+\ufeffusing Lucene.Net.Sandbox.Queries;
+using Lucene.Net.Search;
+using System;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="DuplicateFilter"/>
+ /// </summary>
+ public class DuplicateFilterBuilder : IFilterBuilder
+ {
+ public virtual Filter GetFilter(XmlElement e)
+ {
+ string fieldName = DOMUtils.GetAttributeWithInheritanceOrFail(e, "fieldName");
+ DuplicateFilter df = new DuplicateFilter(fieldName);
+
+ string keepMode = DOMUtils.GetAttribute(e, "keepMode", "first");
+ if (keepMode.Equals("first", StringComparison.OrdinalIgnoreCase))
+ {
+ df.KeepMode = KeepMode.KM_USE_FIRST_OCCURRENCE;
+ }
+ else if (keepMode.Equals("last", StringComparison.OrdinalIgnoreCase))
+ {
+ df.KeepMode = KeepMode.KM_USE_LAST_OCCURRENCE;
+ }
+ else
+ {
+ throw new ParserException("Illegal keepMode attribute in DuplicateFilter:" + keepMode);
+ }
+
+ string processingMode = DOMUtils.GetAttribute(e, "processingMode", "full");
+ if (processingMode.Equals("full", StringComparison.OrdinalIgnoreCase))
+ {
+ df.ProcessingMode = ProcessingMode.PM_FULL_VALIDATION;
+ }
+ else if (processingMode.Equals("fast", StringComparison.OrdinalIgnoreCase))
+ {
+ df.ProcessingMode = ProcessingMode.PM_FAST_INVALIDATION;
+ }
+ else
+ {
+ throw new ParserException("Illegal processingMode attribute in DuplicateFilter:" + processingMode);
+ }
+
+ return df;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/FilteredQueryBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/FilteredQueryBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/FilteredQueryBuilder.cs
new file mode 100644
index 0000000..3bbaad8
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/FilteredQueryBuilder.cs
@@ -0,0 +1,57 @@
+\ufeffusing Lucene.Net.Search;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="FilteredQuery"/>
+ /// </summary>
+ public class FilteredQueryBuilder : IQueryBuilder
+ {
+ private readonly IFilterBuilder filterFactory;
+ private readonly IQueryBuilder queryFactory;
+
+ public FilteredQueryBuilder(IFilterBuilder filterFactory, IQueryBuilder queryFactory)
+ {
+ this.filterFactory = filterFactory;
+ this.queryFactory = queryFactory;
+
+ }
+
+ /// <summary>
+ /// (non-Javadoc)
+ /// @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
+ /// </summary>
+ public virtual Query GetQuery(XmlElement e)
+ {
+ XmlElement filterElement = DOMUtils.GetChildByTagOrFail(e, "Filter");
+ filterElement = DOMUtils.GetFirstChildOrFail(filterElement);
+ Filter f = filterFactory.GetFilter(filterElement);
+
+ XmlElement queryElement = DOMUtils.GetChildByTagOrFail(e, "Query");
+ queryElement = DOMUtils.GetFirstChildOrFail(queryElement);
+ Query q = queryFactory.GetQuery(queryElement);
+
+ FilteredQuery fq = new FilteredQuery(q, f);
+ fq.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
+ return fq;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/FuzzyLikeThisQueryBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/FuzzyLikeThisQueryBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/FuzzyLikeThisQueryBuilder.cs
new file mode 100644
index 0000000..5c72356
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/FuzzyLikeThisQueryBuilder.cs
@@ -0,0 +1,71 @@
+\ufeffusing Lucene.Net.Analysis;
+using Lucene.Net.Sandbox.Queries;
+using Lucene.Net.Search;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="FuzzyLikeThisQuery"/>
+ /// </summary>
+ public class FuzzyLikeThisQueryBuilder : IQueryBuilder
+ {
+ private static readonly int DEFAULT_MAX_NUM_TERMS = 50;
+#pragma warning disable 612, 618
+ private static readonly float DEFAULT_MIN_SIMILARITY = SlowFuzzyQuery.defaultMinSimilarity;
+#pragma warning restore 612, 618
+ private static readonly int DEFAULT_PREFIX_LENGTH = 1;
+ private static readonly bool DEFAULT_IGNORE_TF = false;
+
+ private readonly Analyzer analyzer;
+
+ public FuzzyLikeThisQueryBuilder(Analyzer analyzer)
+ {
+ this.analyzer = analyzer;
+ }
+
+ public virtual Query GetQuery(XmlElement e)
+ {
+ XmlNodeList nl = e.GetElementsByTagName("Field");
+ int maxNumTerms = DOMUtils.GetAttribute(e, "maxNumTerms", DEFAULT_MAX_NUM_TERMS);
+ FuzzyLikeThisQuery fbq = new FuzzyLikeThisQuery(maxNumTerms, analyzer);
+ fbq.IgnoreTF = DOMUtils.GetAttribute(e, "ignoreTF", DEFAULT_IGNORE_TF);
+
+ for (int i = 0; i < nl.Count; i++)
+ {
+ XmlElement fieldElem = (XmlElement)nl.Item(i);
+ float minSimilarity = DOMUtils.GetAttribute(fieldElem, "minSimilarity", DEFAULT_MIN_SIMILARITY);
+ int prefixLength = DOMUtils.GetAttribute(fieldElem, "prefixLength", DEFAULT_PREFIX_LENGTH);
+ string fieldName = DOMUtils.GetAttributeWithInheritance(fieldElem, "fieldName");
+
+ string value = DOMUtils.GetText(fieldElem);
+ fbq.AddTerms(value, fieldName, minSimilarity, prefixLength);
+ }
+
+ fbq.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
+ return fbq;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/LikeThisQueryBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/LikeThisQueryBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/LikeThisQueryBuilder.cs
new file mode 100644
index 0000000..e10c9b1
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/LikeThisQueryBuilder.cs
@@ -0,0 +1,116 @@
+\ufeffusing Lucene.Net.Analysis;
+using Lucene.Net.Analysis.Tokenattributes;
+using Lucene.Net.Queries.Mlt;
+using Lucene.Net.Search;
+using Lucene.Net.Util;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="MoreLikeThisQuery"/>
+ /// </summary>
+ public class LikeThisQueryBuilder : IQueryBuilder
+ {
+ private static readonly int DEFAULT_MAX_QUERY_TERMS = 20;
+ private static readonly int DEFAULT_MIN_TERM_FREQUENCY = 1;
+ private static readonly float DEFAULT_PERCENT_TERMS_TO_MATCH = 30; //default is a 3rd of selected terms must match
+
+ private readonly Analyzer analyzer;
+ private readonly string[] defaultFieldNames;
+
+ public LikeThisQueryBuilder(Analyzer analyzer, string[] defaultFieldNames)
+ {
+ this.analyzer = analyzer;
+ this.defaultFieldNames = defaultFieldNames;
+ }
+
+ /// <summary>
+ /// (non-Javadoc)
+ /// @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
+ /// </summary>
+ public virtual Query GetQuery(XmlElement e)
+ {
+ string fieldsList = e.GetAttribute("fieldNames"); //a comma-delimited list of fields
+ string[] fields = defaultFieldNames;
+ if ((fieldsList != null) && (fieldsList.Trim().Length > 0))
+ {
+ fields = fieldsList.Trim().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
+ //trim the fieldnames
+ for (int i = 0; i < fields.Length; i++)
+ {
+ fields[i] = fields[i].Trim();
+ }
+ }
+
+ //Parse any "stopWords" attribute
+ //TODO MoreLikeThis needs to ideally have per-field stopWords lists - until then
+ //I use all analyzers/fields to generate multi-field compatible stop list
+ string stopWords = e.GetAttribute("stopWords");
+ HashSet<string> stopWordsSet = null;
+ if ((stopWords != null) && (fields != null))
+ {
+ stopWordsSet = new HashSet<string>();
+ foreach (string field in fields)
+ {
+ TokenStream ts = null;
+ try
+ {
+ ts = analyzer.TokenStream(field, stopWords);
+ ICharTermAttribute termAtt = ts.AddAttribute<ICharTermAttribute>();
+ ts.Reset();
+ while (ts.IncrementToken())
+ {
+ stopWordsSet.Add(termAtt.ToString());
+ }
+ ts.End();
+ }
+ catch (IOException ioe)
+ {
+ throw new ParserException("IoException parsing stop words list in "
+ + GetType().Name + ":" + ioe.Message);
+ }
+ finally
+ {
+ IOUtils.CloseWhileHandlingException(ts);
+ }
+ }
+ }
+
+ MoreLikeThisQuery mlt = new MoreLikeThisQuery(DOMUtils.GetText(e), fields, analyzer, fields[0]);
+ mlt.MaxQueryTerms = DOMUtils.GetAttribute(e, "maxQueryTerms", DEFAULT_MAX_QUERY_TERMS);
+ mlt.MinTermFrequency = DOMUtils.GetAttribute(e, "minTermFrequency", DEFAULT_MIN_TERM_FREQUENCY);
+ mlt.PercentTermsToMatch = DOMUtils.GetAttribute(e, "percentTermsToMatch", DEFAULT_PERCENT_TERMS_TO_MATCH) / 100;
+ mlt.StopWords = stopWordsSet;
+ int minDocFreq = DOMUtils.GetAttribute(e, "minDocFreq", -1);
+ if (minDocFreq >= 0)
+ {
+ mlt.MinDocFreq = minDocFreq;
+ }
+
+ mlt.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
+
+ return mlt;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/MatchAllDocsQueryBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/MatchAllDocsQueryBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/MatchAllDocsQueryBuilder.cs
new file mode 100644
index 0000000..3734cfd
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/MatchAllDocsQueryBuilder.cs
@@ -0,0 +1,33 @@
+\ufeffusing Lucene.Net.Search;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="MatchAllDocsQuery"/>
+ /// </summary>
+ public class MatchAllDocsQueryBuilder : IQueryBuilder
+ {
+ public virtual Query GetQuery(XmlElement e)
+ {
+ return new MatchAllDocsQuery();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeFilterBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeFilterBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeFilterBuilder.cs
new file mode 100644
index 0000000..11b65d8
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeFilterBuilder.cs
@@ -0,0 +1,169 @@
+\ufeffusing Lucene.Net.Index;
+using Lucene.Net.Search;
+using Lucene.Net.Util;
+using System;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Creates a <see cref="NumericRangeFilter"/>. The table below specifies the required
+ /// attributes and the defaults if optional attributes are omitted. For more
+ /// detail on what each of the attributes actually do, consult the documentation
+ /// for <see cref="NumericRangeFilter"/>:
+ /// <list type="table">
+ /// <listheader>
+ /// <term>Attribute name</term>
+ /// <term>Values</term>
+ /// <term>Required</term>
+ /// <term>Default</term>
+ /// </listheader>
+ /// <item>
+ /// <term>fieldName</term>
+ /// <term>String</term>
+ /// <term>Yes</term>
+ /// <term>N/A</term>
+ /// </item>
+ /// <item>
+ /// <term>lowerTerm</term>
+ /// <term>Specified by <c>type</c></term>
+ /// <term>Yes</term>
+ /// <term>N/A</term>
+ /// </item>
+ /// <item>
+ /// <term>upperTerm</term>
+ /// <term>Specified by <c>type</c></term>
+ /// <term>Yes</term>
+ /// <term>N/A</term>
+ /// </item>
+ /// <item>
+ /// <term>type</term>
+ /// <term>int, long, float, double</term>
+ /// <term>No</term>
+ /// <term>int</term>
+ /// </item>
+ /// <item>
+ /// <term>includeLower</term>
+ /// <term>true, false</term>
+ /// <term>No</term>
+ /// <term>true</term>
+ /// </item>
+ /// <item>
+ /// <term>includeUpper</term>
+ /// <term>true, false</term>
+ /// <term>No</term>
+ /// <term>true</term>
+ /// </item>
+ /// <item>
+ /// <term>precisionStep</term>
+ /// <term>int</term>
+ /// <term>No</term>
+ /// <term>4</term>
+ /// </item>
+ /// </list>
+ /// <para/>
+ /// If an error occurs parsing the supplied <c>lowerTerm</c> or
+ /// <c>upperTerm</c> into the numeric type specified by <c>type</c>, then the
+ /// error will be silently ignored and the resulting filter will not match any
+ /// documents.
+ /// </summary>
+ public class NumericRangeFilterBuilder : IFilterBuilder
+ {
+ private static readonly NoMatchFilter NO_MATCH_FILTER = new NoMatchFilter();
+
+ private bool strictMode = false;
+
+ /// <summary>
+ /// Specifies how this <see cref="NumericRangeFilterBuilder"/> will handle errors.
+ /// <para/>
+ /// If this is set to true, <see cref="GetFilter(XmlElement)"/> will throw a
+ /// <see cref="ParserException"/> if it is unable to parse the lowerTerm or upperTerm
+ /// into the appropriate numeric type. If this is set to false, then this
+ /// exception will be silently ignored and the resulting filter will not match
+ /// any documents.
+ /// <para/>
+ /// Defaults to false.
+ /// </summary>
+ public void SetStrictMode(bool strictMode)
+ {
+ this.strictMode = strictMode;
+ }
+
+ public virtual Filter GetFilter(XmlElement e)
+ {
+ string field = DOMUtils.GetAttributeWithInheritanceOrFail(e, "fieldName");
+ string lowerTerm = DOMUtils.GetAttributeOrFail(e, "lowerTerm");
+ string upperTerm = DOMUtils.GetAttributeOrFail(e, "upperTerm");
+ bool lowerInclusive = DOMUtils.GetAttribute(e, "includeLower", true);
+ bool upperInclusive = DOMUtils.GetAttribute(e, "includeUpper", true);
+ int precisionStep = DOMUtils.GetAttribute(e, "precisionStep", NumericUtils.PRECISION_STEP_DEFAULT);
+
+ string type = DOMUtils.GetAttribute(e, "type", "int");
+ try
+ {
+ Filter filter;
+ if (type.Equals("int", StringComparison.OrdinalIgnoreCase))
+ {
+ filter = NumericRangeFilter.NewIntRange(field, precisionStep, Convert.ToInt32(lowerTerm), Convert.ToInt32(upperTerm), lowerInclusive,
+ upperInclusive);
+ }
+ else if (type.Equals("long", StringComparison.OrdinalIgnoreCase))
+ {
+ filter = NumericRangeFilter.NewLongRange(field, precisionStep, Convert
+ .ToInt64(lowerTerm), Convert.ToInt64(upperTerm), lowerInclusive,
+ upperInclusive);
+ }
+ else if (type.Equals("double", StringComparison.OrdinalIgnoreCase))
+ {
+ filter = NumericRangeFilter.NewDoubleRange(field, precisionStep, Convert
+ .ToDouble(lowerTerm), Convert.ToDouble(upperTerm), lowerInclusive,
+ upperInclusive);
+ }
+ else if (type.Equals("float", StringComparison.OrdinalIgnoreCase))
+ {
+ filter = NumericRangeFilter.NewFloatRange(field, precisionStep, Convert
+ .ToSingle(lowerTerm), Convert.ToSingle(upperTerm), lowerInclusive,
+ upperInclusive);
+ }
+ else
+ {
+ throw new ParserException("type attribute must be one of: [long, int, double, float]");
+ }
+ return filter;
+ }
+ catch (FormatException nfe)
+ {
+ if (strictMode)
+ {
+ throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe);
+ }
+ return NO_MATCH_FILTER;
+ }
+ }
+
+ internal class NoMatchFilter : Filter
+ {
+ public override DocIdSet GetDocIdSet(AtomicReaderContext context, Bits acceptDocs)
+ {
+ return null;
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeQueryBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeQueryBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeQueryBuilder.cs
new file mode 100644
index 0000000..4b4b128
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/NumericRangeQueryBuilder.cs
@@ -0,0 +1,136 @@
+\ufeffusing Lucene.Net.Search;
+using Lucene.Net.Util;
+using System;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Creates a <see cref="NumericRangeQuery"/>. The table below specifies the required
+ /// attributes and the defaults if optional attributes are omitted. For more
+ /// detail on what each of the attributes actually do, consult the documentation
+ /// for <see cref="NumericRangeQuery"/>:
+ /// <list type="table">
+ /// <listheader>
+ /// <term>Attribute name</term>
+ /// <term>Values</term>
+ /// <term>Required</term>
+ /// <term>Default</term>
+ /// </listheader>
+ /// <item>
+ /// <term>fieldName</term>
+ /// <term>String</term>
+ /// <term>Yes</term>
+ /// <term>N/A</term>
+ /// </item>
+ /// <item>
+ /// <term>lowerTerm</term>
+ /// <term>Specified by <c>type</c></term>
+ /// <term>Yes</term>
+ /// <term>N/A</term>
+ /// </item>
+ /// <item>
+ /// <term>upperTerm</term>
+ /// <term>Specified by <c>type</c></term>
+ /// <term>Yes</term>
+ /// <term>N/A</term>
+ /// </item>
+ /// <item>
+ /// <term>type</term>
+ /// <term>int, long, float, double</term>
+ /// <term>No</term>
+ /// <term>int</term>
+ /// </item>
+ /// <item>
+ /// <term>includeLower</term>
+ /// <term>true, false</term>
+ /// <term>No</term>
+ /// <term>true</term>
+ /// </item>
+ /// <item>
+ /// <term>includeUpper</term>
+ /// <term>true, false</term>
+ /// <term>No</term>
+ /// <term>true</term>
+ /// </item>
+ /// <item>
+ /// <term>precisionStep</term>
+ /// <term>int</term>
+ /// <term>No</term>
+ /// <term>4</term>
+ /// </item>
+ /// </list>
+ /// <para/>
+ /// A <see cref="ParserException"/> will be thrown if an error occurs parsing the
+ /// supplied <c>lowerTerm</c> or <c>upperTerm</c> into the numeric type
+ /// specified by <c>type</c>.
+ /// </summary>
+ public class NumericRangeQueryBuilder : IQueryBuilder
+ {
+ public virtual Query GetQuery(XmlElement e)
+ {
+ string field = DOMUtils.GetAttributeWithInheritanceOrFail(e, "fieldName");
+ string lowerTerm = DOMUtils.GetAttributeOrFail(e, "lowerTerm");
+ string upperTerm = DOMUtils.GetAttributeOrFail(e, "upperTerm");
+ bool lowerInclusive = DOMUtils.GetAttribute(e, "includeLower", true);
+ bool upperInclusive = DOMUtils.GetAttribute(e, "includeUpper", true);
+ int precisionStep = DOMUtils.GetAttribute(e, "precisionStep", NumericUtils.PRECISION_STEP_DEFAULT);
+
+ string type = DOMUtils.GetAttribute(e, "type", "int");
+ try
+ {
+ Query filter;
+ if (type.Equals("int", StringComparison.OrdinalIgnoreCase))
+ {
+ filter = NumericRangeQuery.NewIntRange(field, precisionStep, Convert
+ .ToInt32(lowerTerm), Convert.ToInt32(upperTerm), lowerInclusive,
+ upperInclusive);
+ }
+ else if (type.Equals("long", StringComparison.OrdinalIgnoreCase))
+ {
+ filter = NumericRangeQuery.NewLongRange(field, precisionStep, Convert
+ .ToInt64(lowerTerm), Convert.ToInt64(upperTerm), lowerInclusive,
+ upperInclusive);
+ }
+ else if (type.Equals("double", StringComparison.OrdinalIgnoreCase))
+ {
+ filter = NumericRangeQuery.NewDoubleRange(field, precisionStep, Convert
+ .ToDouble(lowerTerm), Convert.ToDouble(upperTerm), lowerInclusive,
+ upperInclusive);
+ }
+ else if (type.Equals("float", StringComparison.OrdinalIgnoreCase))
+ {
+ filter = NumericRangeQuery.NewFloatRange(field, precisionStep, Convert
+ .ToSingle(lowerTerm), Convert.ToSingle(upperTerm), lowerInclusive,
+ upperInclusive);
+ }
+ else
+ {
+ throw new ParserException("type attribute must be one of: [long, int, double, float]");
+ }
+ return filter;
+ }
+ catch (FormatException nfe)
+ {
+ throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe);
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/RangeFilterBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/RangeFilterBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/RangeFilterBuilder.cs
new file mode 100644
index 0000000..e789cff
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/RangeFilterBuilder.cs
@@ -0,0 +1,39 @@
+\ufeffusing Lucene.Net.Search;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="TermRangeFilter"/>
+ /// </summary>
+ public class RangeFilterBuilder : IFilterBuilder
+ {
+ public Filter GetFilter(XmlElement e)
+ {
+ string fieldName = DOMUtils.GetAttributeWithInheritance(e, "fieldName");
+
+ string lowerTerm = e.GetAttribute("lowerTerm");
+ string upperTerm = e.GetAttribute("upperTerm");
+ bool includeLower = DOMUtils.GetAttribute(e, "includeLower", true);
+ bool includeUpper = DOMUtils.GetAttribute(e, "includeUpper", true);
+ return TermRangeFilter.NewStringRange(fieldName, lowerTerm, upperTerm, includeLower, includeUpper);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/SpanBuilderBase.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/SpanBuilderBase.cs b/src/Lucene.Net.QueryParser/Xml/Builders/SpanBuilderBase.cs
new file mode 100644
index 0000000..8ca22ed
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/SpanBuilderBase.cs
@@ -0,0 +1,40 @@
+\ufeffusing Lucene.Net.Search;
+using Lucene.Net.Search.Spans;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Base class for building <see cref="Search.Spans.SpanQuery"/>s
+ /// </summary>
+ public abstract class SpanBuilderBase : ISpanQueryBuilder
+ {
+ public virtual Query GetQuery(XmlElement e)
+ {
+ return GetSpanQuery(e);
+ }
+
+ /// <summary>
+ /// LUCENENET specific - must provide declaration of GetSpanQuery to satisfy
+ /// contract of <see cref="ISpanQueryBuilder"/>
+ /// </summary>
+ public abstract SpanQuery GetSpanQuery(XmlElement e);
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/SpanFirstBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/SpanFirstBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/SpanFirstBuilder.cs
new file mode 100644
index 0000000..cccfdb0
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/SpanFirstBuilder.cs
@@ -0,0 +1,47 @@
+\ufeffusing Lucene.Net.Search.Spans;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="SpanFirstQuery"/>
+ /// </summary>
+ public class SpanFirstBuilder : SpanBuilderBase
+ {
+ private readonly ISpanQueryBuilder factory;
+
+ public SpanFirstBuilder(ISpanQueryBuilder factory)
+ {
+ this.factory = factory;
+ }
+
+ public override SpanQuery GetSpanQuery(XmlElement e)
+ {
+ int end = DOMUtils.GetAttribute(e, "end", 1);
+ XmlElement child = DOMUtils.GetFirstChildElement(e);
+ SpanQuery q = factory.GetSpanQuery(child);
+
+ SpanFirstQuery sfq = new SpanFirstQuery(q, end);
+
+ sfq.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
+ return sfq;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/SpanNearBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/SpanNearBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/SpanNearBuilder.cs
new file mode 100644
index 0000000..7d84d46
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/SpanNearBuilder.cs
@@ -0,0 +1,54 @@
+\ufeffusing Lucene.Net.Search.Spans;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="SpanNearQuery"/>
+ /// </summary>
+ public class SpanNearBuilder : SpanBuilderBase
+ {
+ private readonly ISpanQueryBuilder factory;
+
+ public SpanNearBuilder(ISpanQueryBuilder factory)
+ {
+ this.factory = factory;
+ }
+
+ public override SpanQuery GetSpanQuery(XmlElement e)
+ {
+ string slopString = DOMUtils.GetAttributeOrFail(e, "slop");
+ int slop = int.Parse(slopString, CultureInfo.InvariantCulture);
+ bool inOrder = DOMUtils.GetAttribute(e, "inOrder", false);
+ List<SpanQuery> spans = new List<SpanQuery>();
+ for (XmlNode kid = e.FirstChild; kid != null; kid = kid.NextSibling)
+ {
+ if (kid.NodeType == XmlNodeType.Element)
+ {
+ spans.Add(factory.GetSpanQuery((XmlElement)kid));
+ }
+ }
+ SpanQuery[] spanQueries = spans.ToArray(/*new SpanQuery[spans.size()]*/);
+ return new SpanNearQuery(spanQueries, slop, inOrder);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/SpanNotBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/SpanNotBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/SpanNotBuilder.cs
new file mode 100644
index 0000000..039db38
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/SpanNotBuilder.cs
@@ -0,0 +1,52 @@
+\ufeffusing Lucene.Net.Search.Spans;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="SpanNotQuery"/>
+ /// </summary>
+ public class SpanNotBuilder : SpanBuilderBase
+ {
+ private readonly ISpanQueryBuilder factory;
+
+ public SpanNotBuilder(ISpanQueryBuilder factory)
+ {
+ this.factory = factory;
+ }
+
+ public override SpanQuery GetSpanQuery(XmlElement e)
+ {
+ XmlElement includeElem = DOMUtils.GetChildByTagOrFail(e, "Include");
+ includeElem = DOMUtils.GetFirstChildOrFail(includeElem);
+
+ XmlElement excludeElem = DOMUtils.GetChildByTagOrFail(e, "Exclude");
+ excludeElem = DOMUtils.GetFirstChildOrFail(excludeElem);
+
+ SpanQuery include = factory.GetSpanQuery(includeElem);
+ SpanQuery exclude = factory.GetSpanQuery(excludeElem);
+
+ SpanNotQuery snq = new SpanNotQuery(include, exclude);
+
+ snq.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
+ return snq;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/SpanOrBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/SpanOrBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/SpanOrBuilder.cs
new file mode 100644
index 0000000..bfb2986
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/SpanOrBuilder.cs
@@ -0,0 +1,53 @@
+\ufeffusing Lucene.Net.Search.Spans;
+using System.Collections.Generic;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="SpanOrQuery"/>
+ /// </summary>
+ public class SpanOrBuilder : SpanBuilderBase
+ {
+ private readonly ISpanQueryBuilder factory;
+
+ public SpanOrBuilder(ISpanQueryBuilder factory)
+ {
+ this.factory = factory;
+ }
+
+ public override SpanQuery GetSpanQuery(XmlElement e)
+ {
+ List<SpanQuery> clausesList = new List<SpanQuery>();
+ for (XmlNode kid = e.FirstChild; kid != null; kid = kid.NextSibling)
+ {
+ if (kid.NodeType == XmlNodeType.Element)
+ {
+ SpanQuery clause = factory.GetSpanQuery((XmlElement)kid);
+ clausesList.Add(clause);
+ }
+ }
+ SpanQuery[] clauses = clausesList.ToArray(/*new SpanQuery[clausesList.size()]*/);
+ SpanOrQuery soq = new SpanOrQuery(clauses);
+ soq.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
+ return soq;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/SpanOrTermsBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/SpanOrTermsBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/SpanOrTermsBuilder.cs
new file mode 100644
index 0000000..afef5e7
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/SpanOrTermsBuilder.cs
@@ -0,0 +1,78 @@
+\ufeffusing Lucene.Net.Analysis;
+using Lucene.Net.Analysis.Tokenattributes;
+using Lucene.Net.Index;
+using Lucene.Net.Search.Spans;
+using Lucene.Net.Util;
+using System.Collections.Generic;
+using System.IO;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder that analyzes the text into a <see cref="SpanOrQuery"/>
+ /// </summary>
+ public class SpanOrTermsBuilder : SpanBuilderBase
+ {
+ private readonly Analyzer analyzer;
+
+ public SpanOrTermsBuilder(Analyzer analyzer)
+ {
+ this.analyzer = analyzer;
+ }
+
+ public override SpanQuery GetSpanQuery(XmlElement e)
+ {
+ string fieldName = DOMUtils.GetAttributeWithInheritanceOrFail(e, "fieldName");
+ string value = DOMUtils.GetNonBlankTextOrFail(e);
+
+ List<SpanQuery> clausesList = new List<SpanQuery>();
+
+ TokenStream ts = null;
+ try
+ {
+ ts = analyzer.TokenStream(fieldName, value);
+ ITermToBytesRefAttribute termAtt = ts.AddAttribute<ITermToBytesRefAttribute>();
+ BytesRef bytes = termAtt.BytesRef;
+ ts.Reset();
+ while (ts.IncrementToken())
+ {
+ termAtt.FillBytesRef();
+ SpanTermQuery stq = new SpanTermQuery(new Term(fieldName, BytesRef.DeepCopyOf(bytes)));
+ clausesList.Add(stq);
+ }
+ ts.End();
+ SpanOrQuery soq = new SpanOrQuery(clausesList.ToArray(/*new SpanQuery[clausesList.size()]*/));
+ soq.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
+ return soq;
+ }
+#pragma warning disable 168
+ catch (IOException ioe)
+#pragma warning restore 168
+ {
+ throw new ParserException("IOException parsing value:" + value);
+ }
+ finally
+ {
+ IOUtils.CloseWhileHandlingException(ts);
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/SpanQueryBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/SpanQueryBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/SpanQueryBuilder.cs
new file mode 100644
index 0000000..d268b5d
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/SpanQueryBuilder.cs
@@ -0,0 +1,30 @@
+\ufeffusing Lucene.Net.Search.Spans;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Interface for retrieving a <see cref="SpanQuery"/>
+ /// </summary>
+ public interface ISpanQueryBuilder : IQueryBuilder
+ {
+ SpanQuery GetSpanQuery(XmlElement e);
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/SpanQueryBuilderFactory.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/SpanQueryBuilderFactory.cs b/src/Lucene.Net.QueryParser/Xml/Builders/SpanQueryBuilderFactory.cs
new file mode 100644
index 0000000..f5de614
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/SpanQueryBuilderFactory.cs
@@ -0,0 +1,52 @@
+\ufeffusing Lucene.Net.Search;
+using Lucene.Net.Search.Spans;
+using System.Collections.Generic;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Factory for <see cref="ISpanQueryBuilder"/>s
+ /// </summary>
+ public class SpanQueryBuilderFactory : ISpanQueryBuilder
+ {
+ private readonly IDictionary<string, ISpanQueryBuilder> builders = new Dictionary<string, ISpanQueryBuilder>();
+
+ public virtual Query GetQuery(XmlElement e)
+ {
+ return GetSpanQuery(e);
+ }
+
+ public void AddBuilder(string nodeName, ISpanQueryBuilder builder)
+ {
+ builders[nodeName] = builder;
+ }
+
+ public virtual SpanQuery GetSpanQuery(XmlElement e)
+ {
+ ISpanQueryBuilder builder;
+ if (!builders.TryGetValue(e.Name, out builder) || builder == null)
+ {
+ throw new ParserException("No SpanQueryObjectBuilder defined for node " + e.Name);
+ }
+ return builder.GetSpanQuery(e);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/SpanTermBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/SpanTermBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/SpanTermBuilder.cs
new file mode 100644
index 0000000..48fc08b
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/SpanTermBuilder.cs
@@ -0,0 +1,39 @@
+\ufeffusing Lucene.Net.Index;
+using Lucene.Net.Search.Spans;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="SpanTermQuery"/>
+ /// </summary>
+ public class SpanTermBuilder : SpanBuilderBase
+ {
+ public override SpanQuery GetSpanQuery(XmlElement e)
+ {
+ string fieldName = DOMUtils.GetAttributeWithInheritanceOrFail(e, "fieldName");
+ string value = DOMUtils.GetNonBlankTextOrFail(e);
+ SpanTermQuery stq = new SpanTermQuery(new Term(fieldName, value));
+
+ stq.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
+ return stq;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/TermQueryBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/TermQueryBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/TermQueryBuilder.cs
new file mode 100644
index 0000000..2212ada
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/TermQueryBuilder.cs
@@ -0,0 +1,38 @@
+\ufeffusing Lucene.Net.Index;
+using Lucene.Net.Search;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="TermQuery"/>
+ /// </summary>
+ public class TermQueryBuilder : IQueryBuilder
+ {
+ public virtual Query GetQuery(XmlElement e)
+ {
+ string field = DOMUtils.GetAttributeWithInheritanceOrFail(e, "fieldName");
+ string value = DOMUtils.GetNonBlankTextOrFail(e);
+ TermQuery tq = new TermQuery(new Term(field, value));
+ tq.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f);
+ return tq;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/2ae0b1bb/src/Lucene.Net.QueryParser/Xml/Builders/TermsFilterBuilder.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.QueryParser/Xml/Builders/TermsFilterBuilder.cs b/src/Lucene.Net.QueryParser/Xml/Builders/TermsFilterBuilder.cs
new file mode 100644
index 0000000..0e492bb
--- /dev/null
+++ b/src/Lucene.Net.QueryParser/Xml/Builders/TermsFilterBuilder.cs
@@ -0,0 +1,77 @@
+\ufeffusing Lucene.Net.Analysis;
+using Lucene.Net.Analysis.Tokenattributes;
+using Lucene.Net.Queries;
+using Lucene.Net.Search;
+using Lucene.Net.Util;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Xml;
+
+namespace Lucene.Net.QueryParsers.Xml.Builders
+{
+ /*
+ * 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>
+ /// Builder for <see cref="TermsFilter"/>
+ /// </summary>
+ public class TermsFilterBuilder : IFilterBuilder
+ {
+ private readonly Analyzer analyzer;
+
+ public TermsFilterBuilder(Analyzer analyzer)
+ {
+ this.analyzer = analyzer;
+ }
+
+ /// <summary>
+ /// (non-Javadoc)
+ /// @see org.apache.lucene.xmlparser.FilterBuilder#process(org.w3c.dom.Element)
+ /// </summary>
+ public virtual Filter GetFilter(XmlElement e)
+ {
+ List<BytesRef> terms = new List<BytesRef>();
+ string text = DOMUtils.GetNonBlankTextOrFail(e);
+ string fieldName = DOMUtils.GetAttributeWithInheritanceOrFail(e, "fieldName");
+
+ TokenStream ts = null;
+ try
+ {
+ ts = analyzer.TokenStream(fieldName, text);
+ ITermToBytesRefAttribute termAtt = ts.AddAttribute<ITermToBytesRefAttribute>();
+ BytesRef bytes = termAtt.BytesRef;
+ ts.Reset();
+ while (ts.IncrementToken())
+ {
+ termAtt.FillBytesRef();
+ terms.Add(BytesRef.DeepCopyOf(bytes));
+ }
+ ts.End();
+ }
+ catch (IOException ioe)
+ {
+ throw new Exception("Error constructing terms from index:" + ioe);
+ }
+ finally
+ {
+ IOUtils.CloseWhileHandlingException(ts);
+ }
+ return new TermsFilter(fieldName, terms);
+ }
+ }
+}