.Suggest не тянет никаких результатов - PullRequest
0 голосов
/ 14 февраля 2020

Мой ElasticSearch хорошо работает для поиска, но я думаю, что мой контроллер Suggest настроен неправильно. Я пытаюсь взять поле Title и использовать его для предложения.

Я использую Nest v7.5.1 и Elasticsearch. Net v7.5.1.

Модель пакета :

public class Package
{
    public string Title { get; set; }
    public string Summary { get; set; }
    public string ResultDisplaySummary { get; set; }
    public string IconUrl { get; set; }
    public string URL { get; set; }
    public string[] Tags { get; set; }
    public string ResultType { get; set; }
    public string Keywords { get; set; }
    public CompletionField Suggest { get; set; }
    public string PestControlledBy { get; set; }
    public string PesticideControlsThesePests { get; set; }
    public string PesticideInstructions { get; set; }
    public string PesticideActiveIngredients { get; set; }
    public string PesticidesContainingThisActiveIngredient { get; set; }
    public string PesticideSafeOn { get; set; }
    public string PesticideNotSafeOn { get; set; }
}

Индексатор:

var clientIndexResponse = Client.Indices.Create(CurrentIndexName, c => c
.Settings(st => st
    .NumberOfShards(2)
    .NumberOfReplicas(0)
    .Setting("index.mapping.nested_objects.limit", 12000)
    .Analysis(an => an // https://stackoverflow.com/questions/38065966/token-chars-mapping-to-ngram-filter-elasticsearch-nest
        .Analyzers(anz => anz
            .Custom("ngram_analyzer", cc => cc
                .Tokenizer("ngram_tokenizer")
            //.Filters(nGramFilters)
            )
        )
        .Tokenizers(tz => tz
                .NGram("ngram_tokenizer", td => td
                    .MinGram(3)
                    .MaxGram(3)
                /*.TokenChars(
                TokenChar.Letter,
                TokenChar.Digit,
                TokenChar.Punctuation,
                TokenChar.Whitespace,
                TokenChar.Symbol
            )*/
                )
            )
        )
    )
    .Map<Package>(map => map
        .AutoMap()
        .Properties(p => p
        .Text(t => t
            .Name(n => n.Title)
            .Fields(f => f
                .Keyword(n => n
                    .Name("keyword")
                        .IgnoreAbove(256)
                )
                .Text(n => n
                    .Name("ngram")
                    .Analyzer("ngram_analyzer")
                )
            )
        )
        .Completion(pr => pr // ************* SUGGEST PART ******************//
            .Name(n => n.Suggest)
        )
        .Text(t => t
            .Name(n => n.Summary)
            .Fields(f => f
                .Keyword(k => k
                    .Name("keyword")
                        .IgnoreAbove(256)
                )
                .Text(tt => tt
                    .Name("ngram")
                    .Analyzer("ngram_analyzer")
                )
            )
        )
        .Text(t => t
            .Name(n => n.PestControlledBy)
            .Fields(f => f
                .Keyword(k => k
                    .Name("keyword")
                        .IgnoreAbove(256)
                )
                .Text(tt => tt
                    .Name("ngram")
                    .Analyzer("ngram_analyzer")
                )
            )
        )
        .Text(t => t
            .Name(n => n.PesticideControlsThesePests)
            .Fields(f => f
                .Keyword(k => k
                    .Name("keyword")
                        .IgnoreAbove(256)
                )
                .Text(tt => tt
                    .Name("ngram")
                    .Analyzer("ngram_analyzer")
                )
            )
        )
        .Text(t => t
            .Name(n => n.PesticideInstructions)
            .Fields(f => f
                .Keyword(k => k
                    .Name("keyword")
                        .IgnoreAbove(256)
                )
                .Text(tt => tt
                    .Name("ngram")
                    .Analyzer("ngram_analyzer")
                )
            )
        )
        .Text(t => t
            .Name(n => n.PesticideActiveIngredients)
            .Fields(f => f
                .Keyword(k => k
                    .Name("keyword")
                        .IgnoreAbove(256)
                )
                .Text(tt => tt
                    .Name("ngram")
                    .Analyzer("ngram_analyzer")
                )
            )
        )
        .Text(t => t
            .Name(n => n.PesticidesContainingThisActiveIngredient)
            .Fields(f => f
                .Keyword(k => k
                    .Name("keyword")
                        .IgnoreAbove(256)
                )
                .Text(tt => tt
                    .Name("ngram")
                    .Analyzer("ngram_analyzer")
                )
            )
        )
        .Text(t => t
            .Name(n => n.PesticideSafeOn)
            .Fields(f => f
                .Keyword(k => k
                    .Name("keyword")
                        .IgnoreAbove(256)
                )
                .Text(tt => tt
                    .Name("ngram")
                    .Analyzer("ngram_analyzer")
                )
            )
        )
        .Text(t => t
            .Name(n => n.PesticideNotSafeOn)
            .Fields(f => f
                .Keyword(k => k
                    .Name("keyword")
                        .IgnoreAbove(256)
                )
                .Text(tt => tt
                    .Name("ngram")
                    .Analyzer("ngram_analyzer")
                )
            )
        )
    )
)
);

Контроллер (это правильно получает удар и передает string term):

public JsonResult Suggester(string term = "")
    {
        IElasticClient _client = DorkSearchConfiguration.GetClient();

        var result = _client.Search<Package>(s => s
                .Index<Package>()
                .Source(sf => sf
                    .Includes(f => f
                        .Field(ff => ff.Summary)
                        .Field(ff => ff.URL)
                    )
                )
                .Suggest(su => su
                    .Completion("package-suggestions", c => c
                        .Prefix(term)
                        .Field(p => p.Suggest)
                    )
                )
            );

        var suggestions = result.Suggest["package-suggestions"]
        .FirstOrDefault()
        .Options
        .Select(suggest => new
        {
            Name = suggest.Source.Title,
            Slug = suggest.Source.URL,
            Title = !string.IsNullOrEmpty(suggest.Source.Summary)
                ? string.Concat(suggest.Source.Summary.Take(200))
                : string.Empty
        });

        bool hi = true;

        return Json(suggestions, JsonRequestBehavior.AllowGet);
    }

Набор данных:

var packages = new List<Package>();

packages.Add(new Package { Title = "Annual Buttercup (Ranunculus sceleratus)", Summary = "Annual buttercup, also known as Cursed Crowfoot or Celery-leaved Buttercup, is an annual yellow flower that grows from 1/2 to 2 feet tall. All buttercups are poisonous, but Annual buttercup is proven to have a higher concentration of toxicity. The flowers usually have 3 to 5 yellow petals with round tips.", IconUrl = "/lawn-pests/card1x1/buttercup-annual.jpg", ResultType = "pest", PestControlledBy = "Post-Emergent Control from Crossbow. " });
packages.Add(new Package { Title = "Horseweed (Marestail) (Erigeron canadensis)", Summary = "Horseweed, also known as marestail, is a winter and summer annual. It grows as a rosette of leaves and goes dormant in the wintertime. It primarily emerges in the fall. A single plant can produce 200,000 seeds that float on parachutes similar to that of a dandelion, making it very capable of spreading quickly.", IconUrl = "/lawn-pests/card1x1/horseweed-marestail.jpg", ResultType = "pest", PestControlledBy = "Post-Emergent Control from Crossbow, Tenacity, Milestone, Primesource Meso 4 SC Select, Speedzone, and Celsius WG. " });
packages.Add(new Package { Title = "Common Lambsquarters (Chenopodium album)", Summary = "Common lambsquarters is a summer annual which can grow from between 2 to 6 feet in height. When flowering, they exhibit very small grayish-green or green flowers on green-to-reddish stems. The seeds are shiny and smooth. It's found all throughout the United States, except for extreme deserts.", IconUrl = "/lawn-pests/card1x1/lambsquarters-common.jpg", ResultType = "pest", PestControlledBy = "Pre-Emergent Control from Sedgehammer 5% Halosulfuron-Methyl, Sedgehammer 75% Halosulfurion-Methyl, Tenacity, Pro Turf Barricade, Primesource Meso 4 SC Select, and Prodiamine 65 WDG. Post-Emergent Control from Crossbow, Ortho Nutsedge Killer Ready-To-Spray, Tenacity, Milestone, Primesource Meso 4 SC Select, 2,4-D Amine, Speedzone, Triplet SF, and Celsius WG. " });
packages.Add(new Package { Title = "Common Ragweed (Ambrosia artemisiifolia)", Summary = "Common ragweed is a summer annual plant that grows up to 3 feet tall and branches often. The stems are green to pinkish-red and hairy. The leaves are up to 6 inches in length and 4 inches across, and alternate along the stem. The seeds can remaindormant in the soil for 5 years or more.", IconUrl = "/lawn-pests/card1x1/ragweed-common.jpg", ResultType = "pest", PestControlledBy = "Pre-Emergent Control from Sedgehammer 5% Halosulfuron-Methyl and Sedgehammer 75% Halosulfurion-Methyl. Post-Emergent Control from Crossbow, Sedgehammer 5% Halosulfuron-Methyl, Sedgehammer 75% Halosulfurion-Methyl, Triple Threat, Milestone, 2,4-D Amine, Speedzone, Triplet SF, and Celsius WG. " });
packages.Add(new Package { Title = "Thyme-Leaf Spurge (Euphorbia serpyllifolia)", Summary = "Thyme-leaf spurge has small white flowers that luster along the branches. It is also sometime referred to as Thymeleaf sandmat, due to it forming a prostrate mat sometimes in the sand.", IconUrl = "/lawn-pests/card1x1/spurge-thyme-leaf.jpg", ResultType = "pest", PestControlledBy = "Post-Emergent Control from Crossbow. " });
packages.Add(new Package { Title = "Annual Bedstraw (Galium aparine)", Summary = "4-40 inches tall with weak four sided stems that are sparsely branched. Stems have small clinging hooks. Leaves form in whorls; typically with 6 narrow leaflets. Flowers are white to greenish, tiny, and have four petals. Flowers form in clusters of 3-5 in leaf axils.", IconUrl = "/lawn-pests/card1x1/bedstraw-annual.jpg", ResultType = "pest", PestControlledBy = "Post-Emergent Control from Crossbow, Milestone, Certainty, Speedzone, and Triplet SF. " });
packages.Add(new Package { Title = "Bluebur (Lappula squarrosa)", Summary = "The seedlings are covered with short hairs and have a distinctive crease down the center of the leaf. When Bluebur has reached maturity, it produces tiny, blue flowers, and each flower has four bur-like seeds with two rows of sharp, hooked prickles. ", IconUrl = "/lawn-pests/card1x1/bluebur.jpg", ResultType = "pest", PestControlledBy = "Post-Emergent Control from Crossbow. " });
packages.Add(new Package { Title = "Burdock (Arctium)", Summary = "Burdock grows relatively tall, 1-2 meters, therefore having deep roots which are brownish green, or nearly black on the outside. The basal rosette of leaves stays close to the ground the first year and the beginning of the second. Burdock has purple flowers on tips of prickly ball of bracts that blooms between June and October. Large, wavy, heart-shaped leaves that are green on the top and whitish on the bottom makes identifying burdock easy. ", IconUrl = "/lawn-pests/card1x1/burdock-1.jpg", ResultType = "pest", PestControlledBy = "Post-Emergent Control from Crossbow, Milestone, 2,4-D Amine, Speedzone, and Triplet SF. " });
packages.Add(new Package { Title = "White Sweet Clover (Melilotus albus)", Summary = "White sweet clover appears very different from the typical white clover found in the lawn. It's not small, like normal clover, and grows a whopping 3 to 8 feet in height. The upper stems have long, white flowers that are from about 2 to 6 inches long. The weight of the flowers tends to give the plant a floppy appearance.", IconUrl = "/lawn-pests/card1x1/clover-white-sweet.jpg", ResultType = "pest", PestControlledBy = "Post-Emergent Control from Crossbow, Milestone, 2,4-D Amine, and Celsius WG. " });
packages.Add(new Package { Title = "Bur Clover (Medicago polymorpha)", Summary = "Bur clover can grow from between 16 and 22 inches and bloom in early spring with 3 to 5 flowers per plant. These can be rather annoying to humans and animals due to the seed burs that attach to clothing and fur. Prevention with a pre-emergent herbicide is key, but a herbicide that contains 2,4-d can take them out in the mean time.", IconUrl = "/lawn-pests/card1x1/clover-bur.png", ResultType = "pest", PestControlledBy = "Post-Emergent Control from Crossbow, Speedzone, and Celsius WG. " });

Любое направление будет полезно. ElasticSearch потрясающий, когда я могу заставить его работать ...

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...