ElasticSearch / Nest MatchPhrasePrefix перестал работать после обновления версии - PullRequest
0 голосов
/ 08 марта 2019

Я обновился с:

  • ElasticSearch 2.0 до 6.6.1
  • ElasticSearch.Net Nuget пакет 2.4.6 до 6.5.1
  • NEST NuGet пакет 2.4.6 - 6.5.1

... и мой запрос Nest для выполнения MatchPhrasePrefix перестал возвращать результаты.

Программное обеспечение представляет собой поисковую систему для веб-страниц, и предполагается, что одна из функций позволяет ограничивать результаты URL-адресами, начинающимися с определенного пути, например http://example.com/blog, чтобы видеть только посты блога в результатах поиска.

У меня есть mainQuery, который отлично работает. Если пользователь вводит значение urlStartstWith, mainQuery объединяется с запросом bool / MatchPhrasePrefix.

Индексы содержат от 100 до 1000 документов.

Вещи, которые я пробовал, не работали:

  • Полностью перестроен новый индекс
  • Удалено .Operator(Operator.And), так как он не существует в этой версии NEST (вызвало ошибку компиляции)
  • Увеличение MaxExpansions до различных значений вплоть до 5000
  • URL-кодировка urlStartstWith
  • Удаление строки .MinimumShouldMatch(1)

Если я выполню этот запрос, построенный с новой библиотекой NEST, на старом сервере ElasticSearch версии 2.0, он будет работать. Так что это то, что изменилось под капотом в самом ElasticSearch, я думаю.

Запрос

var urlStartWithFilter = esQuery.Bool(b =>
    b.Filter(m =>
        m.MatchPhrasePrefix(pre =>
            pre
                //.MaxExpansions(5000) //did nothing
                //.Operator(Operator.And) //does not exist in new version of NEST
                .Query(urlStartsWith)
                .Field(f => f.Url))
        )
        .MinimumShouldMatch(1)
    );

mainQuery = mainQuery && urlStartWithFilter;

По запросу - пример с начала до конца

Это пример, который показывает проблему и очень близок к тому, как я запрашиваю индекс веб-страниц моего фактического проекта.

Запустите экземпляр ElasticSearch 6.6.1. Вы можете сделать это в Docker через:

docker pull docker.elastic.co/elasticsearch/elasticsearch:6.6.1
docker network create esnetwork --driver=bridge
docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" --name elasticsearch -d --network esnetwork docker.elastic.co/elasticsearch/elasticsearch:6.6.1

Создайте новое консольное приложение .Net Framework 4.6.1. Вставьте следующее в Program.cs

using Nest;
using System;
using System.Collections.Generic;

namespace Loader
{
    class Program
    {

        const string ELASTIC_SERVER = "http://localhost:9200";
        const string DEFAULT_INDEX = "stack_overflow_api";

        private static Uri es_node = new Uri(ELASTIC_SERVER);
        private static ConnectionSettings settings = new ConnectionSettings(es_node).DefaultIndex(DEFAULT_INDEX);
        private static ElasticClient client = new ElasticClient(settings);

        private static bool include_starts_with = true;

        static void Main(string[] args)
        {
            WriteMainMenu();
        }


        static void WriteMainMenu()
        {
            //Console.Clear();
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("What to do?");
            Console.WriteLine("1 - Load Sample Data into ES");
            Console.WriteLine("2 - Run a query WITHOUT StartsWith");
            Console.WriteLine("3 - Run a query WITH StartsWith");
            Console.WriteLine("[Enter] to exit.");
            Console.WriteLine("");
            Console.WriteLine("");

            var option = Console.ReadLine();

            if (option == "1")
            {
                LoadSampleData();
            }
            else if (option == "2")
            {
                include_starts_with = false;
                RunStartsWithQuery();
            }
            else if (option == "3")
            {
                include_starts_with = true;
                RunStartsWithQuery();
            }

            //- exit
        }

        private static void LoadSampleData()
        {
            var existsResponse = client.IndexExists(DEFAULT_INDEX);
            if (existsResponse.Exists) //delete existing mapping (and data)
            {
                client.DeleteIndex(DEFAULT_INDEX);
            }

            var rebuildResponse = client.CreateIndex(DEFAULT_INDEX, c => c.Settings(s => s.NumberOfReplicas(1).NumberOfShards(5)));
            var response2 = client.Map<Item>(m => m.AutoMap());

            var data = GetSearchResultData();

            Console.WriteLine($"Indexing {data.Count} items...");
            var response = client.IndexMany<Item>(data);
            client.Refresh(DEFAULT_INDEX);

            WriteMainMenu();
        }

        private static List<Item> GetSearchResultData()
        {
            var jsonPath = System.IO.Path.Combine(Environment.CurrentDirectory, "StackOverflowSampleJson.json");
            var jsondata = System.IO.File.ReadAllText(jsonPath);
            var searchResult = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Item>>(jsondata);
            return searchResult;
        }

        private static void RunStartsWithQuery()
        {
            Console.WriteLine("Enter a search query and press enter, or just press enter to search for the default of 'Perl'.");
            var search = Console.ReadLine().ToLower();

            if (string.IsNullOrWhiteSpace(search))
            {
                search = "Perl";
            }   

            Console.WriteLine($"Searching for {search}...");

            var result = client.Search<Item>(s => s
                .Query(esQuery => {


                    var titleQuery = esQuery.Match(m => m
                            .Field(p => p.title)
                            .Boost(1)
                            .Query(search)
                        );

                    var closedReasonQuery = esQuery.Match(m => m
                        .Field(p => p.closed_reason)
                        .Boost(1)
                        .Query(search)
                    );

                    // search across a couple fields
                    var mainQuery = titleQuery || closedReasonQuery;

                    if (include_starts_with)
                    {
                        var urlStartsWith = "https://stackoverflow.com/questions/";

                        var urlStartWithFilter = esQuery.Bool(b =>
                            b.Filter(m =>
                                m.MatchPhrasePrefix(pre =>
                                    pre
                                        //.MaxExpansions(5000) //did nothing
                                        //.Operator(Operator.And) //does not exist in new version of NEST
                                        .Query(urlStartsWith)
                                        .Field(f => f.link))
                                )
                                .MinimumShouldMatch(1)
                            );

                        mainQuery = mainQuery && urlStartWithFilter;
                    }

                    return mainQuery;
                })
            );

            if (result.IsValid == false)
            {
                Console.WriteLine("ES Query had an error");
            }
            else if (result.Hits.Count > 0)
            {
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.WriteLine($"Found {result.Hits.Count} results:");

                foreach (var item in result.Hits)
                {
                    Console.WriteLine($"    {item.Source.title}");
                }
                Console.ForegroundColor = ConsoleColor.White;
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine($"Found 0 results");
                Console.ForegroundColor = ConsoleColor.White;
            }

            WriteMainMenu();

        }
    }


    public class Item
    {
        public List<string> tags { get; set; }
        //public Owner owner { get; set; }
        public bool is_answered { get; set; }
        public int view_count { get; set; }
        public int answer_count { get; set; }
        public int score { get; set; }
        public int last_activity_date { get; set; }
        public int creation_date { get; set; }
        public int last_edit_date { get; set; }
        public int question_id { get; set; }
        public string link { get; set; }
        public string title { get; set; }
        public int? accepted_answer_id { get; set; }
        public int? closed_date { get; set; }
        public string closed_reason { get; set; }
        public int? community_owned_date { get; set; }
    }
}
  • Создайте новый файл с именем StackOverflowSampleJson.json и вставьте содержимое этого образца JSON: https://pastebin.com/s5rcHysp
  • Установите StackOverflowSampleJson.json для вывода в каталог сборки, щелкнув его правой кнопкой мыши, выбрав свойства и изменив Copy to Output Directory на Always
  • Запустите приложение.
  • Выберите 1 - Load Sample Data into ES, чтобы заполнить индекс
  • Выберите 2 - Run a query WITHOUT StartsWith, чтобы выполнить запрос без StartsWith / MatchPhrasePrefix, чтобы убедиться, что обычный запрос работает
  • Выберите 3 - Run a query WITH StartsWith, чтобы увидеть, что включение этого дополнительного запроса обнуляет счетчик результатов.

1 Ответ

0 голосов
/ 12 марта 2019

Хорошо, я не очень понимаю, почему старый запрос работал сasticsearch 2.0, а не сasticsearch 6.6, но изменение внутреннего запроса привело к тому, что он работал как с ES2, так и с ES6:

if (include_starts_with)
{
    var urlStartsWith = "https://stackoverflow.com/questions/";

    var urlStartWithFilter = esQuery.MatchPhrasePrefix(pre => pre
        .Query(urlStartsWith)
        .Field(f => f.link)
    );

    mainQuery = mainQuery && urlStartWithFilter;
}
...