示例#1
0
        public async Task <TermVectorsModel> TermVectors(string queryString, double lat, double lng)
        {
            var client           = GetClient();
            var searchDescriptor = new SearchDescriptor <Place>();
            var dumper           = new NestDescriptorDumper(client.RequestResponseSerializer);

            #region Search
            QueryContainerDescriptor <Place> queryContainer = new QueryContainerDescriptor <Place>();
            var query = queryContainer.Bool(b =>
                                            b.Should(
                                                q => q.Match(m => m.Field(f => f.Name).Query(queryString)),
                                                q => q.Match(m => m.Field(f => f.Vicinity).Query(queryString))
                                                )
                                            );

            Func <SortDescriptor <Place>, SortDescriptor <Place> > SortByGeo =
                (SortDescriptor <Place> s) => s.GeoDistance(g =>
                                                            g.Field(f => f.Geometry.Location)
                                                            .DistanceType(GeoDistanceType.Arc)
                                                            .Unit(DistanceUnit.Kilometers)
                                                            .Order(SortOrder.Ascending)
                                                            .Points(new GeoLocation(lat, lng)));

            Func <SortDescriptor <Place>, IPromise <IList <ISort> > > sort = s => SortByGeo(s.Descending(SortSpecialField.Score).Descending(d => d.Rating));

            searchDescriptor
            .Index <Place>()
            .Query(x => query)
            .Sort(sort)
            .ScriptFields(x =>
                          x.ScriptField("distance", s => s.Source($"doc['geometry.location'].arcDistance({lat},{lng})")))
            .Take(10)
            .Source(true)
            ;
            #endregion

            var results = await client.SearchAsync <Place>(searchDescriptor);

            var model = ToSearchPlaces(results.Hits).First();

            var term = new TermVectorsDescriptor <Place>(IndexName.From <Place>(), TypeName.From <Place>());
            term.Index <Place>()
            .Id(Id.From(model))
            .Fields(tf => tf.Vicinity, tf => tf.Name)
            ;

            var sss = dumper.Dump <TermVectorsDescriptor <Place> >(term);

            var termVectorsResult = await client.TermVectorsAsync <Place>(term);

            return(ToTermVectorsModel(termVectorsResult, model));
        }
示例#2
0
        public Dictionary <string, IReadOnlyDictionary <string, TermVectorTerm> > GetTermVectorsOfDoc(TermRequestParametersModel requestParameters)
        {
            TermVectorsDescriptor <Question> termVectorDescriptor = new TermVectorsDescriptor <Question>();

            termVectorDescriptor.Index(requestParameters.Index);
            termVectorDescriptor.Id(requestParameters.Id);
            termVectorDescriptor.Fields(requestParameters.Fields);
            termVectorDescriptor.TermStatistics(true);
            termVectorDescriptor.Offsets(false);
            termVectorDescriptor.Positions(false);
            termVectorDescriptor.Payloads(false);
            termVectorDescriptor.Filter(f => f.MaximimumNumberOfTerms(requestParameters.MaxNumberOfTerms));

            var termVectorResponse = TermRequestToDoc(termVectorDescriptor);

            return(termVectorResponse.TermVectors
                   .Select(tvItem => new KeyValuePair <string, IReadOnlyDictionary <string, TermVectorTerm> >(tvItem.Key.Name,
                                                                                                              tvItem.Value.Terms))
                   .ToDictionary(tv => tv.Key, tv => tv.Value));
        }