Ejemplo n.º 1
0
        public SearchServiceResults GetActors(CommonSearchRequest searchRequest)
        {
            var results = _client.Search <ActorSearchItem>(s => s
                                                           .From(searchRequest.Skip)
                                                           .Size(searchRequest.PageSize)
                                                           .Index("actor")
                                                           .Source(src => src.Includes(i => i.Field("Title")))
                                                           .Type("actorsearchitem")
                                                           .Query(q => q
                                                                  .Bool(b => b
                                                                        .Name(searchRequest.Query)))
                                                           );

            var searchResult = new SearchServiceResults()
            {
                TotalResults     = (int)results.Total,
                DebugInformation = results.DebugInformation,
                OriginalQuery    = searchRequest.Query
            };

            foreach (var hit in results.Hits)
            {
                var relatedDocument = results.Documents.FirstOrDefault(p => p.Id == hit.Id);
                relatedDocument.PostProcess(hit.Highlights);
                searchResult.Items.Add(relatedDocument);
            }

            return(searchResult);
        }
Ejemplo n.º 2
0
        public SearchServiceResults Search <T>(CommonSearchRequest searchRequest) where T : SearchItemDocumentBase
        {
            var filteringQuery = CreateCommonFilter(searchRequest);

            const int titleBoost   = 15;
            const int keywordBoost = 45;
            const int castBoost    = 20;

            var results = _client.Search <SearchItemDocumentBase>(s => s
                                                                  .From(searchRequest.Skip)
                                                                  .Size(searchRequest.PageSize)
                                                                  //.ConcreteTypeSelector((a, b) => FromStringType(b.Type))
                                                                  .Index(Indices.Index(IndexDefinition.All.Select(p => p.Name).ToArray()))
                                                                  .Type(Types.Type(IndexDefinition.All.Select(p => p.EsType).ToArray()))
                                                                  .Query(q => q
                                                                         .FunctionScore(fsc => fsc
                                                                                        .BoostMode(FunctionBoostMode.Multiply)
                                                                                        .ScoreMode(FunctionScoreMode.Sum)
                                                                                        .Functions(f => f
                                                                                                   .FieldValueFactor(b => b
                                                                                                                     .Field(nameof(SearchItemDocumentBase.Rating))
                                                                                                                     .Missing(0.7)
                                                                                                                     .Modifier(FieldValueFactorModifier.None)
                                                                                                                     )
                                                                                                   )
                                                                                        .Query(qx => qx.MultiMatch(m => m
                                                                                                                   .Query(searchRequest.Query.ToLower())
                                                                                                                   .Fields(ff => ff
                                                                                                                           .Field(f => f.Title, boost: titleBoost)
                                                                                                                           .Field(f => f.Summary)
                                                                                                                           .Field(f => f.Keywords, boost: keywordBoost)
                                                                                                                           .Field($"{nameof(MovieSearchItem.Cast)}.{nameof(ActorNestedType.Name)}", boost: castBoost)
                                                                                                                           )
                                                                                                                   .Type(TextQueryType.BestFields)
                                                                                                                   ) && filteringQuery)
                                                                                        )
                                                                         )
                                                                  .Highlight(h => h
                                                                             .Fields(ff => ff
                                                                                     .Field(f => f.Title)
                                                                                     .Field(f => f.Summary)
                                                                                     .NumberOfFragments(2)
                                                                                     .FragmentSize(250)
                                                                                     .NoMatchSize(200)
                                                                                     )
                                                                             )
                                                                  );

            var searchResult = new SearchServiceResults()
            {
                TotalResults     = (int)results.Total,
                DebugInformation = results.DebugInformation,
                OriginalQuery    = searchRequest.Query
            };

            foreach (var hit in results.Hits)
            {
                if (typeof(T).Name.ToLowerInvariant() == "SearchItemDocumentBase".ToLowerInvariant() || (typeof(T).Name.ToLowerInvariant() != "SearchItemDocumentBase".ToLowerInvariant() && hit.Type == typeof(T).Name.ToLowerInvariant()))
                {
                    var relatedDocument = results.Documents.FirstOrDefault(p => p.Id == hit.Id);
                    relatedDocument.PostProcess(hit.Highlights);
                    searchResult.Items.Add(relatedDocument);
                }
            }

            return(searchResult);
        }