protected virtual Lucene.Net.Search.Query VisitWithinRadius(WithinRadiusNode node, LuceneQueryMapperState mappingState) { SpatialContext ctx = SpatialContext.GEO; var strategy = new PointVectorStrategy(ctx, Sitecore.ContentSearch.Spatial.Common.Constants.LocationFieldName); if (node.Latitude is double && node.Longitude is double && node.Radius is double) { var distance = DistanceUtils.Dist2Degrees((double)node.Radius, DistanceUtils.EARTH_MEAN_RADIUS_MI); Circle circle = ctx.MakeCircle((double)node.Longitude, (double)node.Latitude, distance); var spatialArgs = new SpatialArgs(SpatialOperation.IsWithin, circle); var dq = strategy.MakeQuery(spatialArgs); DistanceReverseValueSource valueSource = new DistanceReverseValueSource(strategy, circle.GetCenter(), distance); ValueSourceFilter vsf = new ValueSourceFilter(new QueryWrapperFilter(dq), valueSource, 0, distance); var filteredSpatial = new FilteredQuery(new MatchAllDocsQuery(), vsf); mappingState.FilterQuery = filteredSpatial; Lucene.Net.Search.Query spatialRankingQuery = new FunctionQuery(valueSource); Random r = new Random(DateTime.Now.Millisecond); var randomNumber = r.Next(10000101, 11000101); Lucene.Net.Search.Query dummyQuery = Lucene.Net.Search.NumericRangeQuery.NewIntRange("__smallcreateddate", randomNumber, Int32.Parse(DateTime.Now.ToString("yyyyMMdd")), true, true); BooleanQuery bq = new BooleanQuery(); bq.Add(filteredSpatial, Occur.MUST); bq.Add(spatialRankingQuery, Occur.MUST); bq.Add(dummyQuery, Occur.SHOULD); return(bq); } throw new NotSupportedException("Wrong parameters type, Radius, latitude and longitude must be of type double"); }
/// <summary> /// Test that queries based on reverse/ordFieldScore returns docs with expected score. /// </summary> /// <param name="field"></param> /// <param name="inOrder"></param> private void DoTestExactScore(string field, bool inOrder) { IndexReader r = DirectoryReader.Open(dir); IndexSearcher s = NewSearcher(r); ValueSource vs; if (inOrder) { vs = new OrdFieldSource(field); } else { vs = new ReverseOrdFieldSource(field); } Query q = new FunctionQuery(vs); TopDocs td = s.Search(q, null, 1000); assertEquals("All docs should be matched!", N_DOCS, td.TotalHits); ScoreDoc[] sd = td.ScoreDocs; for (int i = 0; i < sd.Length; i++) { float score = sd[i].Score; string id = s.IndexReader.Document(sd[i].Doc).Get(ID_FIELD); Log("-------- " + i + ". Explain doc " + id); Log(s.Explain(q, sd[i].Doc)); float expectedScore = N_DOCS - i - 1; assertEquals("score of result " + i + " shuould be " + expectedScore + " != " + score, expectedScore, score, TEST_SCORE_TOLERANCE_DELTA); string expectedId = inOrder ? Id2String(N_DOCS - i) : Id2String(i + 1); // reverse ==> smaller values first - in-order ==> larger values first assertTrue("id of result " + i + " shuould be " + expectedId + " != " + score, expectedId.Equals(id, StringComparison.Ordinal)); } r.Dispose(); }
internal Context() { mImplementPtr = ConversationAPI.CreateContext(); IntPtr global = ConversationAPI.GetContextGlobalSave(ImplementPtr); if (global != IntPtr.Zero) { mGlobalSave = new Save(global); } IntPtr player = ConversationAPI.GetContextPlayerSave(ImplementPtr); if (player != IntPtr.Zero) { mPlayerSave = new Save(player); } // 长期持有自己的delegate防止被gc mFunctionHandler = new FunctionHandler(OnFunctoinCall); mProcessHandler = new FunctionHandler(OnProcessCall); mFunctionQuery = new FunctionQuery(OnFunctoinQuery); mProcessQuery = new FunctionQuery(OnProcessQuery); ConversationAPI.SetFunctionHandler(ImplementPtr, mFunctionHandler); ConversationAPI.SetProcessHandler(ImplementPtr, mProcessHandler); ConversationAPI.SetFunctionQuery(ImplementPtr, mFunctionQuery); ConversationAPI.SetProcessQuery(ImplementPtr, mProcessQuery); }
protected virtual Lucene.Net.Search.Query VisitWithinRadius(WithinRadiusNode node, LuceneQueryMapperState mappingState) { SpatialContext ctx = SpatialContext.GEO; var strategy = new PointVectorStrategy(ctx, Sitecore.ContentSearch.Spatial.Common.Constants.LocationFieldName); if (node.Latitude is double && node.Longitude is double && node.Radius is double) { var distance = DistanceUtils.Dist2Degrees((double)node.Radius, DistanceUtils.EARTH_MEAN_RADIUS_MI); Circle circle = ctx.MakeCircle((double)node.Longitude, (double)node.Latitude, distance); var spatialArgs = new SpatialArgs(SpatialOperation.Intersects, circle); var dq = strategy.MakeQuery(spatialArgs); DistanceReverseValueSource valueSource = new DistanceReverseValueSource(strategy, circle.GetCenter(), distance); ValueSourceFilter vsf = new ValueSourceFilter(new QueryWrapperFilter(dq), valueSource, 0, distance); var filteredSpatial = new FilteredQuery(new MatchAllDocsQuery(), vsf); Lucene.Net.Search.Query spatialRankingQuery = new FunctionQuery(valueSource); BooleanQuery bq = new BooleanQuery(); bq.Add(filteredSpatial, Occur.MUST); bq.Add(spatialRankingQuery, Occur.MUST); return(bq); } throw new NotSupportedException("Wrong parameters type, Radius, latitude and longitude must be of type double"); }
/// <summary> /// Test that FieldScoreQuery returns docs in expected order. /// </summary> /// <param name="valueSource"></param> private void DoTestRank(ValueSource valueSource) { FunctionQuery functionQuery = new FunctionQuery(valueSource); IndexReader r = DirectoryReader.Open(dir); IndexSearcher s = NewSearcher(r); Log("test: " + functionQuery); QueryUtils.Check( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, functionQuery, s); ScoreDoc[] h = s.Search(functionQuery, null, 1000).ScoreDocs; assertEquals("All docs should be matched!", N_DOCS, h.Length); string prevID = "ID" + (N_DOCS + 1); // greater than all ids of docs in this test for (int i = 0; i < h.Length; i++) { string resID = s.Doc(h[i].Doc).Get(ID_FIELD); Log(i + ". score=" + h[i].Score + " - " + resID); Log(s.Explain(functionQuery, h[i].Doc)); assertTrue("res id " + resID + " should be < prev res id " + prevID, resID.CompareToOrdinal(prevID) < 0); prevID = resID; } r.Dispose(); }
public virtual void TestWithScore() { Store.Directory indexDir = NewDirectory(); Store.Directory taxoDir = NewDirectory(); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); IndexWriter iw = new IndexWriter(indexDir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()))); FacetsConfig config = new FacetsConfig(); for (int i = 0; i < 4; i++) { Document doc = new Document(); doc.Add(new NumericDocValuesField("price", (i + 1))); doc.Add(new FacetField("a", Convert.ToString(i % 2))); iw.AddDocument(config.Build(taxoWriter, doc)); } DirectoryReader r = DirectoryReader.Open(iw, true); DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter); ValueSource valueSource = new ValueSourceAnonymousInnerClassHelper(this); FacetsCollector fc = new FacetsCollector(true); // score documents by their 'price' field - makes asserting the correct counts for the categories easier Query q = new FunctionQuery(new LongFieldSource("price")); FacetsCollector.Search(NewSearcher(r), q, 10, fc); Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, valueSource); Assert.AreEqual("dim=a path=[] value=10.0 childCount=2\n 1 (6.0)\n 0 (4.0)\n", facets.GetTopChildren(10, "a").ToString()); IOUtils.Close(taxoWriter, iw, taxoReader, taxoDir, r, indexDir); }
public IList <SearchItem> SearchByLocation(string queryString, double longitude, double latitude, double searchRadiusKm, int maxHits = 10) { IList <SearchItem> results; using (var searcher = new IndexSearcher(Directory, true)) using (var analyser = new StandardAnalyzer(LuceneVersion)) { var distance = DistanceUtils.Dist2Degrees(searchRadiusKm, DistanceUtils.EARTH_MEAN_RADIUS_KM); var searchArea = _spatialContext.MakeCircle(longitude, latitude, distance); var fields = new[] { Name }; var parser = new MultiFieldQueryParser(LuceneVersion, fields, analyser); parser.DefaultOperator = QueryParser.Operator.OR; // Allow multiple terms. var query = ParseQuery(queryString, parser); var spatialArgs = new SpatialArgs(SpatialOperation.Intersects, searchArea); var spatialQuery = _strategy.MakeQuery(spatialArgs); var valueSource = _strategy.MakeRecipDistanceValueSource(searchArea); var valueSourceFilter = new ValueSourceFilter(new QueryWrapperFilter(spatialQuery), valueSource, 0, 1); var filteredSpatial = new FilteredQuery(query, valueSourceFilter); var spatialRankingQuery = new FunctionQuery(valueSource); BooleanQuery bq = new BooleanQuery(); bq.Add(filteredSpatial, Occur.MUST); bq.Add(spatialRankingQuery, Occur.MUST); var hits = searcher.Search(bq, maxHits).ScoreDocs; results = MapResultsToSearchItems(hits, searcher); } return(results); }
public static TopDocs DistranceScore_PointVectorStrategy(Searcher searcher, PointVectorStrategy strategy, Point myLocation) { var vs = strategy.MakeDistanceValueSource(myLocation); var q = new FunctionQuery(vs); // Returns a score for each document based on a ValueSource TopDocs hits = searcher.Search(q, null, 100); return(hits); }
public Query MakeQueryWithValueSource(SpatialArgs args, ValueSource valueSource) { var bq = new BooleanQuery(); var spatial = MakeFilter(args); bq.Add(new ConstantScoreQuery(spatial), Occur.MUST); // This part does the scoring Query spatialRankingQuery = new FunctionQuery(valueSource); bq.Add(spatialRankingQuery, Occur.MUST); return(bq); }
/// <summary> /// 查询功能 /// </summary> /// <param name="functionQuery"></param> /// <returns></returns> public PagedResult <FunctionDTO> Find(FunctionQuery functionQuery) { Check.Argument.IsNotNull(functionQuery, "functionQuery"); //Apply filtering var query = _databaseContext.Functions.Where(functionQuery); var total = query.Count(); query = SortMemeberHelper.SortingAndPaging <Function>(query, functionQuery.sort, functionQuery.dir , functionQuery.page, functionQuery.limit); var resultSet = query.AsNoTracking().ToList().Select(r => _functionMapper.Map(r)).ToList(); return(new PagedResult <FunctionDTO>(resultSet, total)); }
/// <summary> /// Test that queries based on reverse/ordFieldScore scores correctly /// </summary> /// <param name="field"></param> /// <param name="inOrder"></param> private void DoTestRank(string field, bool inOrder) { IndexReader r = DirectoryReader.Open(dir); IndexSearcher s = NewSearcher(r); ValueSource vs; if (inOrder) { vs = new OrdFieldSource(field); } else { vs = new ReverseOrdFieldSource(field); } Query q = new FunctionQuery(vs); Log("test: " + q); QueryUtils.Check( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, q, s); ScoreDoc[] h = s.Search(q, null, 1000).ScoreDocs; assertEquals("All docs should be matched!", N_DOCS, h.Length); string prevID = inOrder ? "IE" // greater than all ids of docs in this test ("ID0001", etc.) : "IC"; // smaller than all ids of docs in this test ("ID0001", etc.) for (int i = 0; i < h.Length; i++) { string resID = s.Doc(h[i].Doc).Get(ID_FIELD); Log(i + ". score=" + h[i].Score + " - " + resID); Log(s.Explain(q, h[i].Doc)); if (inOrder) { assertTrue("res id " + resID + " should be < prev res id " + prevID, resID.CompareToOrdinal(prevID) < 0); } else { assertTrue("res id " + resID + " should be > prev res id " + prevID, resID.CompareToOrdinal(prevID) > 0); } prevID = resID; } r.Dispose(); }
// Test that FieldScoreQuery returns docs with expected score. //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private void doTestExactScore(ValueSource valueSource) throws Exception private void doTestExactScore(ValueSource valueSource) { FunctionQuery functionQuery = new FunctionQuery(valueSource); IndexReader r = DirectoryReader.Open(dir); IndexSearcher s = NewSearcher(r); TopDocs td = s.Search(functionQuery, null, 1000); assertEquals("All docs should be matched!", N_DOCS, td.TotalHits); ScoreDoc[] sd = td.ScoreDocs; foreach (ScoreDoc aSd in sd) { float score = aSd.Score; Log(s.Explain(functionQuery, aSd.Doc)); string id = s.IndexReader.Document(aSd.Doc).Get(ID_FIELD); float expectedScore = ExpectedFieldScore(id); // "ID7" --> 7.0 assertEquals("score of " + id + " shuould be " + expectedScore + " != " + score, expectedScore, score, TEST_SCORE_TOLERANCE_DELTA); } r.Dispose(); }
public void CreateSpatialFilterAndWeight(PointRadiusCriterion geoFilter, Filter currentFilter, Weight currentWeight) { var spatialContext = SpatialContext.GEO; var geohashTree = new GeohashPrefixTree(spatialContext, 10); var strategy = new RecursivePrefixTreeStrategy(geohashTree, geoFilter.FieldName); var point = spatialContext.MakePoint(geoFilter.Longitude, geoFilter.Latitude); var spatialArgs = new SpatialArgs(SpatialOperation.Intersects, spatialContext.MakeCircle(point, DistanceUtils.Dist2Degrees(geoFilter.RadiusKm, DistanceUtils.EARTH_MEAN_RADIUS_KM))); var circle = spatialContext.MakeCircle(point, DistanceUtils.Dist2Degrees(geoFilter.RadiusKm, DistanceUtils.EARTH_MEAN_RADIUS_KM)); var circleCells = strategy.GetGrid().GetWorldNode().GetSubCells(circle); var luceneFilters = new List <Filter>(); if (currentFilter != null) { luceneFilters.Add(currentFilter); } var tempSpatial = strategy.MakeFilter(spatialArgs); luceneFilters.Add(tempSpatial); if (geoFilter.Sort != PointRadiusCriterion.SortOption.None) { var valueSource = strategy.MakeDistanceValueSource(point); var funcQ = new FunctionQuery(valueSource); // this is a bit odd... but boosting the score negatively orders results if (geoFilter.Sort == PointRadiusCriterion.SortOption.Ascending) { funcQ.Boost = -1; } spatialWeight = funcQ.CreateWeight(this); spatialWeight.GetSumOfSquaredWeights(); luceneFilters.Add(new QueryWrapperFilter(currentWeight.Query)); } spatialFilter = new ChainedFilter(luceneFilters.ToArray(), 1); }
/** scores[] are in docId order */ protected virtual void CheckValueSource(ValueSource vs, float[] scores, float delta) { FunctionQuery q = new FunctionQuery(vs); // //TODO is there any point to this check? // int expectedDocs[] = new int[scores.length];//fill with ascending 0....length-1 // for (int i = 0; i < expectedDocs.length; i++) { // expectedDocs[i] = i; // } // CheckHits.checkHits(Random, q, "", indexSearcher, expectedDocs); TopDocs docs = indexSearcher.Search(q, 1000);//calculates the score for (int i = 0; i < docs.ScoreDocs.Length; i++) { ScoreDoc gotSD = docs.ScoreDocs[i]; float expectedScore = scores[gotSD.Doc]; assertEquals("Not equal for doc " + gotSD.Doc, expectedScore, gotSD.Score, delta); } CheckHits.CheckExplanations(q, "", indexSearcher); }
//TODO this is basically old code that hasn't been verified well and should probably be removed public virtual Query MakeQueryDistanceScore(SpatialArgs args) { // For starters, just limit the bbox var shape = args.Shape; if (!(shape is IRectangle || shape is ICircle)) { throw new NotSupportedException("Only Rectangles and Circles are currently supported, found [" + shape.GetType().Name + "]");//TODO } IRectangle bbox = shape.BoundingBox; if (bbox.CrossesDateLine) { throw new NotSupportedException("Crossing dateline not yet supported"); } ValueSource valueSource = null; Query spatial = null; SpatialOperation op = args.Operation; if (SpatialOperation.Is(op, SpatialOperation.BBoxWithin, SpatialOperation.BBoxIntersects)) { spatial = MakeWithin(bbox); } else if (SpatialOperation.Is(op, SpatialOperation.Intersects, SpatialOperation.IsWithin)) { spatial = MakeWithin(bbox); if (args.Shape is ICircle) { var circle = (ICircle)args.Shape; // Make the ValueSource valueSource = MakeDistanceValueSource(shape.Center); var vsf = new ValueSourceFilter( new QueryWrapperFilter(spatial), valueSource, 0, circle.Radius); spatial = new FilteredQuery(new MatchAllDocsQuery(), vsf); } } else if (op == SpatialOperation.IsDisjointTo) { spatial = MakeDisjoint(bbox); } if (spatial == null) { throw new UnsupportedSpatialOperation(args.Operation); } if (valueSource != null) { valueSource = new CachingDoubleValueSource(valueSource); } else { valueSource = MakeDistanceValueSource(shape.Center); } Query spatialRankingQuery = new FunctionQuery(valueSource); var bq = new BooleanQuery(); bq.Add(spatial, BooleanClause.Occur.MUST); bq.Add(spatialRankingQuery, BooleanClause.Occur.MUST); return(bq); }
/// <summary> /// Create a CustomScoreQuery over input subQuery and a <seealso cref="FunctionQuery"/>. </summary> /// <param name="subQuery"> the sub query whose score is being customized. Must not be null. </param> /// <param name="scoringQuery"> a value source query whose scores are used in the custom score /// computation. This parameter is optional - it can be null. </param> public CustomScoreQuery(Query subQuery, FunctionQuery scoringQuery) : this(subQuery, scoringQuery != null ? new FunctionQuery[] { scoringQuery } : new FunctionQuery[0]) // don't want an array that contains a single null.. { }
// constructor internal CustomAddQuery(Query q, FunctionQuery qValSrc) : base(q, qValSrc) { }
private void DoTestCustomScore(ValueSource valueSource, double dboost) { float boost = (float)dboost; FunctionQuery functionQuery = new FunctionQuery(valueSource); IndexReader r = DirectoryReader.Open(dir); IndexSearcher s = NewSearcher(r); // regular (boolean) query. BooleanQuery q1 = new BooleanQuery(); q1.Add(new TermQuery(new Term(TEXT_FIELD, "first")), BooleanClause.Occur.SHOULD); q1.Add(new TermQuery(new Term(TEXT_FIELD, "aid")), BooleanClause.Occur.SHOULD); q1.Add(new TermQuery(new Term(TEXT_FIELD, "text")), BooleanClause.Occur.SHOULD); Log(q1); // custom query, that should score the same as q1. BooleanQuery q2CustomNeutral = new BooleanQuery(true); Query q2CustomNeutralInner = new CustomScoreQuery(q1); q2CustomNeutral.Add(q2CustomNeutralInner, BooleanClause.Occur.SHOULD); // a little tricky: we split the boost across an outer BQ and CustomScoreQuery // this ensures boosting is correct across all these functions (see LUCENE-4935) q2CustomNeutral.Boost = (float)Math.Sqrt(dboost); q2CustomNeutralInner.Boost = (float)Math.Sqrt(dboost); Log(q2CustomNeutral); // custom query, that should (by default) multiply the scores of q1 by that of the field CustomScoreQuery q3CustomMul = new CustomScoreQuery(q1, functionQuery); q3CustomMul.Strict = true; q3CustomMul.Boost = boost; Log(q3CustomMul); // custom query, that should add the scores of q1 to that of the field CustomScoreQuery q4CustomAdd = new CustomAddQuery(q1, functionQuery); q4CustomAdd.Strict = true; q4CustomAdd.Boost = boost; Log(q4CustomAdd); // custom query, that multiplies and adds the field score to that of q1 CustomScoreQuery q5CustomMulAdd = new CustomMulAddQuery(q1, functionQuery, functionQuery); q5CustomMulAdd.Strict = true; q5CustomMulAdd.Boost = boost; Log(q5CustomMulAdd); // do al the searches TopDocs td1 = s.Search(q1, null, 1000); TopDocs td2CustomNeutral = s.Search(q2CustomNeutral, null, 1000); TopDocs td3CustomMul = s.Search(q3CustomMul, null, 1000); TopDocs td4CustomAdd = s.Search(q4CustomAdd, null, 1000); TopDocs td5CustomMulAdd = s.Search(q5CustomMulAdd, null, 1000); // put results in map so we can verify the scores although they have changed IDictionary <int, float> h1 = TopDocsToMap(td1); IDictionary <int, float> h2CustomNeutral = TopDocsToMap(td2CustomNeutral); IDictionary <int, float> h3CustomMul = TopDocsToMap(td3CustomMul); IDictionary <int, float> h4CustomAdd = TopDocsToMap(td4CustomAdd); IDictionary <int, float> h5CustomMulAdd = TopDocsToMap(td5CustomMulAdd); VerifyResults(boost, s, h1, h2CustomNeutral, h3CustomMul, h4CustomAdd, h5CustomMulAdd, q1, q2CustomNeutral, q3CustomMul, q4CustomAdd, q5CustomMulAdd); r.Dispose(); }
// constructor internal CustomMulAddQuery(Query q, FunctionQuery qValSrc1, FunctionQuery qValSrc2) : base(q, qValSrc1, qValSrc2) { }
public void Should_run_a_query_for_a_single_sql_object() { var sqlObject = new FunctionQuery(); }
public static extern void SetProcessQuery(IntPtr pContext, FunctionQuery pQuery);