Elasticsearch:Multi-match (multi_match) 及 Disjunction max 查询
Posted Elastic 中国社区官方博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch:Multi-match (multi_match) 及 Disjunction max 查询相关的知识,希望对你有一定的参考价值。
多重匹配(multi_match)查询,顾名思义就是跨多个字段搜索查询。 例如,如果我们想在 title、synopsis 和 tags 三个字段中搜索 Java 一词,那么 multi_match query 就是答案。另外,很多开发者还不是很清楚 multi-match 及 disjunction max query 的区别和联系。在今天的文章中,我将详述这两个查询。
准备数据
我们使用 bulk 指令来创建如下的 books 索引:
POST _bulk
"index":"_index":"books","_id":"1"
"title": "Core Java Volume I – Fundamentals","author": "Cay S. Horstmann","edition": 11, "synopsis": "Java reference book that offers a detailed explanation of various features of Core Java, including exception handling, interfaces, and lambda expressions. Significant highlights of the book include simple language, conciseness, and detailed examples.","amazon_rating": 4.6,"release_date": "2018-08-27","tags": ["Programming Languages, Java Programming"]
"index":"_index":"books","_id":"2"
"title": "Effective Java","author": "Joshua Bloch", "edition": 3,"synopsis": "A must-have book for every Java programmer and Java aspirant, Effective Java makes up for an excellent complementary read with other Java books or learning material. The book offers 78 best practices to follow for making the code better.", "amazon_rating": 4.7, "release_date": "2017-12-27", "tags": ["Object Oriented Software Design"]
"index":"_index":"books","_id":"3"
"title": "Java: A Beginner’s Guide", "author": "Herbert Schildt","edition": 8,"synopsis": "One of the most comprehensive books for learning Java. The book offers several hands-on exercises as well as a quiz section at the end of every chapter to let the readers self-evaluate their learning.","amazon_rating": 4.2,"release_date": "2018-11-20","tags": ["Software Design & Engineering", "Internet & Web"]
"index":"_index":"books","_id":"4"
"title": "Java - The Complete Reference","author": "Herbert Schildt","edition": 11,"synopsis": "Convenient Java reference book examining essential portions of the Java API library, Java. The book is full of discussions and apt examples to better Java learning.","amazon_rating": 4.4,"release_date": "2019-03-19","tags": ["Software Design & Engineering", "Internet & Web", "Computer Programming Language & Tool"]
"index":"_index":"books","_id":"5"
"title": "Head First Java","author": "Kathy Sierra and Bert Bates","edition":2, "synopsis": "The most important selling points of Head First Java is its simplicity and super-effective real-life analogies that pertain to the Java programming concepts.","amazon_rating": 4.3,"release_date": "2005-02-18","tags": ["IT Certification Exams", "Object-Oriented Software Design","Design Pattern Programming"]
"index":"_index":"books","_id":"6"
"title": "Java Concurrency in Practice","author": "Brian Goetz with Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea","edition": 1,"synopsis": "Java Concurrency in Practice is one of the best Java programming books to develop a rich understanding of concurrency and multithreading.","amazon_rating": 4.3,"release_date": "2006-05-09","tags": ["Computer Science Books", "Programming Languages", "Java Programming"]
"index":"_index":"books","_id":"7"
"title": "Test-Driven: TDD and Acceptance TDD for Java Developers","author": "Lasse Koskela","edition": 1,"synopsis": "Test-Driven is an excellent book for learning how to write unique automation testing programs. It is a must-have book for those Java developers that prioritize code quality as well as have a knack for writing unit, integration, and automation tests.","amazon_rating": 4.1,"release_date": "2007-10-22","tags": ["Software Architecture", "Software Design & Engineering", "Java Programming"]
"index":"_index":"books","_id":"8"
"title": "Head First Object-Oriented Analysis Design","author": "Brett D. McLaughlin, Gary Pollice & David West","edition": 1,"synopsis": "Head First is one of the most beautiful finest book series ever written on Java programming language. Another gem in the series is the Head First Object-Oriented Analysis Design.","amazon_rating": 3.9,"release_date": "2014-04-29","tags": ["Introductory & Beginning Programming", "Object-Oriented Software Design", "Java Programming"]
"index":"_index":"books","_id":"9"
"title": "Java Performance: The Definite Guide","author": "Scott Oaks","edition": 1,"synopsis": "Garbage collection, JVM, and performance tuning are some of the most favorable aspects of the Java programming language. It educates readers about maximizing Java threading and synchronization performance features, improve Java-driven database application performance, tackle performance issues","amazon_rating": 4.1,"release_date": "2014-03-04","tags": ["Design Pattern Programming", "Object-Oriented Software Design", "Computer Programming Language & Tool"]
"index":"_index":"books","_id":"10"
"title": "Head First Design Patterns", "author": "Eric Freeman & Elisabeth Robson with Kathy Sierra & Bert Bates","edition": 10,"synopsis": "Head First Design Patterns is one of the leading books to build that particular understanding of the Java programming language." ,"amazon_rating": 4.5,"release_date": "2014-03-04","tags": ["Design Pattern Programming", "Object-Oriented Software Design eTextbooks", "Web Development & Design eTextbooks"]
"index":"_index":"books","_id":"11"
"title": "javascript - The Definitive Guide", "author": "David Flanagan","edition": 1,"synopsis": "JavaScript is the programming language of the web and is used by more software developers today than any other programming language. For nearly 25 years this best seller has been the go-to guide for JavaScript programmers." ,"amazon_rating": 4.7,"release_date": "2020-05-29","tags": ["Design Pattern Programming", "Object-Oriented Software Design eTextbooks", "Programming Languages, Javascript Programming"]
"index":"_index":"books","_id":"12"
"title": "Eloquent Javascript", "author": "Marijn Haverbeke","edition": 3,"synopsis": "JavaScript lies at the heart of almost every modern web application, from social apps like Twitter to browser-based game frameworks like Phaser and Babylon. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications." ,"amazon_rating": 4.6,"release_date": "2018-12-14","tags": ["DOM", "Node.js","object-oriented and functional programming techniques", "Programming Languages, Javascript Programming"]
"index":"_index":"books","_id":"13"
"title": "JavaScript and JQuery: Interactive Front–End Web", "author": "Jon Duckett","edition": 1,"synopsis": "This full-color book adopts a visual approach to teaching JavaScript & jQuery, showing you how to make web pages more interactive and interfaces more intuitive through the use of inspiring code examples, infographics, and photography." ,"amazon_rating": 4.7,"release_date": "2014-07-18","tags": ["DJavaScript and jQuery", "JavaScript APIs, and jQuery plugins", "Programming Languages, Javascript Programming"]
"index":"_index":"books","_id":"14"
"title": "A Smarter Way to Learn JavaScript", "author": "Mark Myers","edition": 1,"synopsis": "JThe first problem is retention. You remember only ten or twenty percent of what you read. That spells failure. To become fluent in a computer language, you have to retain pretty much everything." ,"amazon_rating": 4.6,"release_date": "2014-03-20","tags": ["Programming Languages, Javascript Programming"]
"index":"_index":"books","_id":"15"
"title": "Head First JavaScript Programming", "author": "Eric T. Freeman and Elisabeth Robson","edition": 1,"synopsis": "This brain-friendly guide teaches you everything from JavaScript language fundamentals to advanced topics, including objects, functions, and the browser’s document object model." ,"amazon_rating": 4.5,"release_date": "2014-04-10","tags": ["The secrets of JavaScript types", "The inner details of JavaScript", "Programming Languages, Javascript Programming"]
"index":"_index":"books","_id":"16"
"title": "Modern JavaScript for the Impatient", "author": "Cay Horstmann","edition": 1,"synopsis": "Modern JavaScript for the Impatient is a complete yet concise guide to JavaScript E6 and beyond. Rather than first requiring you to learn and transition from older versions, it helps you quickly get productive with today’s far more powerful versions and rapidly move from languages such as Java, C#, C, or C++." ,"amazon_rating": 4.9,"release_date": "2020-08-18","tags": ["modern JavaScript", "Object-Oriented Programming", "JavaScript libraries, frameworks, and platforms"]
"index":"_index":"books","_id":"17"
"title": "JavaScript in easy steps", "author": "Mike McGrath","edition": 6,"synopsis": "JavaScript in easy steps, 6th edition instructs the user how to create exciting web pages that employ the power of JavaScript to provide functionality. You need have no previous knowledge of any scripting language so it's ideal for the newcomer to JavaScript. By the end of this book you will have gained a sound understanding of JavaScript and be able to add exciting dynamic scripts to your own web pages." ,"amazon_rating": 4.4,"release_date": "2020-02-28","tags": ["Get Started in JavaScript", "Interact with the Document", "Create Web Applications"]
"index":"_index":"books","_id":"18"
"title": "JavaScript: The Good Parts", "author": "Douglas Crockford","edition": 1,"synopsis": "Most programming languages contain good and bad parts, but JavaScript has more than its share of the bad, having been developed and released in a hurry before it could be refined. This authoritative book scrapes away these bad features to reveal a subset of JavaScript that's more reliable, readable, and maintainable than the language as a whole—a subset you can use to create truly extensible and efficient code." ,"amazon_rating": 4.5,"release_date": "2008-05-18","tags": ["Design Pattern Programming", "Object-Oriented Programming", "Programming Languages, Javascript Programming"]
"index":"_index":"books","_id":"19"
"title": "JavaScript Everywhere", "author": "Adam Scott","edition": 1,"synopsis": "JavaScript is the little scripting language that could. Once used chiefly to add interactivity to web browser windows, JavaScript is now a primary building block of powerful and robust applications. In this practical book, new and experienced JavaScript developers will learn how to use this language to create APIs as well as web, mobile, and desktop applications." ,"amazon_rating": 4.5,"release_date": "2020-02-21","tags": ["Building Cross-platform Applications with GraphQL, React, React Native, and Electron", "Programming Languages, Javascript Programming"]
"index":"_index":"books","_id":"20"
"title": "JavaScript The Complete Reference", "author": "Thomas A. Powell and Fritz Schneider","edition": 3,"synopsis": "Design, debug, and publish high-performance web pages and applications using tested techniques and best practices from expert developers. The all-new edition of this comprehensive guide has been thoroughly revised and expanded to cover the latest JavaScript features, tools, and programming methods." ,"amazon_rating": 4.3,"release_date": "2012-09-16","tags": ["XMLHttpRequest object to create Ajax applications", "DOM", "Programming Languages, Javascript Programming"]
"index":"_index":"books","_id":"21"
"title": "The C# Player's Guide", "author": "RB Whitaker","edition": 5,"synopsis": "The book in your hands is a different kind of programming book. Like an entertaining video game, programming is an often challenging but always rewarding experience. This book shakes off the dusty, dull, dryness of the typical programming book, replacing it with something more exciting and flavorful: a bit of humor, a casual tone, and examples involving dragons and asteroids instead of bank accounts and employees." ,"amazon_rating": 5.0,"release_date": "2022-01-14","tags": ["basic mechanics of C#", "object-oriented programming", "advanced C# features"]
"index":"_index":"books","_id":"22"
"title": "C# 10 and .NET 6", "author": "Mark J. Price","edition": 1,"synopsis": "You will learn object-oriented programming, writing, testing, and debugging functions, implementing interfaces, and inheriting classes. The book covers the .NET APIs for performing tasks like managing and querying data, monitoring and improving performance, and working with the filesystem, async streams, serialization, and encryption. It provides examples of cross-platform apps you can build and deploy, such as websites and services using ASP.NET Core." ,"amazon_rating": 4.6,"release_date": "2021-11-09","tags": ["real-world applications", "latest features of C# 10 and .NET 6", "Visual Studio 2022 and Visual Studio Code"]
"index":"_index":"books","_id":"23"
"title": "C# Programming in easy steps", "author": "Mike McGrath","edition": 2,"synopsis": "C# Programming in easy steps, 2nd edition will teach you to code applications, and demonstrates every aspect of the C# language you will need to produce professional programming results. Its examples provide clear syntax-highlighted code showing C# language basics including variables, arrays, logic, looping, methods, and classes." ,"amazon_rating": 4.6,"release_date": "2020-05-29","tags": ["programming in C#", "creating apps", "fundamental understanding of C#"]
"index":"_index":"books","_id":"24"
"title": "Professional C# and .NET", "author": "Christian Nagel","edition": 1,"synopsis": "Experienced programmers making the transition to C# will benefit from the author’s in-depth explorations to create Web- and Windows applications using ASP.NET Core, Blazor, and WinUI using modern application patterns and new features offered by .NET including Microservices deployed to Docker images, GRPC, localization, asynchronous streaming, and much more." ,"amazon_rating": 4.5,"release_date": "2022-03-04","tags": ["extension of .NET to non-Microsoft platforms like OSX and Linux", "Microsoft Azure services such as Azure App", "C# 10 and .NET 6"]
"index":"_index":"books","_id":"25"
"title": "Head First C#", "author": "Stellman, Andrew, Greene and Jennifer","edition": 4,"synopsis": "What will you learn from this book? For beginning programmers looking to learn C#, this practical guide provides a bright alternative to the legions of dull tutorials on this popular object-oriented language." ,"amazon_rating": 4.6,"release_date": "2021-01-29","tags": ["Real-World Programming with C# and .Net Core", "Object-oriented language", "multi-sensory learning"]
"index":"_index":"books","_id":"26"
"title": "C# 9.0 in a Nutshell", "author": "Albahari and Joseph","edition": 1,"synopsis": "When you have questions about C# 9.0 or .NET 5, this bestselling guide has the answers. C# is a language of unusual flexibility and breadth, but with its continual growth, there's so much more to learn. In the tradition of O'Reilly's Nutshell guides, this thoroughly updated edition is simply the best one-volume reference to the C# language available today." ,"amazon_rating": 4.5,"release_date": "2021-03-31","tags": ["C# and .NET", "pointers, closures, and patterns Dig deep into LINQ", "Programming Languages"]
"index":"_index":"books","_id":"27"
"title": "C# in Depth", "author": "Jon Skeet","edition": 4,"synopsis": "C# is an amazing language that's up to any challenge you can throw at it. As a C# developer, you also need to be up to the task." ,"amazon_rating": 4.6,"release_date": "2019-05-20","tags": ["C# 5, 6, and 7", "better code with tuples, string interpolation, pattern matching"]
"index":"_index":"books","_id":"28"
"title": "C# Data Structures and Algorithms", "author": "Marcin Jamro","edition": 1,"synopsis": "Data structures allow organizing data efficiently. They are critical to various problems and their suitable implementation can provide a complete solution that acts like reusable code. In this book, you will learn how to use various data structures while developing in the C# language as well as how to implement some of the most common algorithms used with such data structures." ,"amazon_rating": 4.6,"release_date": "2018-04-19","tags": ["Implement algorithms", "Build enhanced applications by using hashtables, dictionaries and sets", "Programming Languages, C#"]
"index":"_index":"books","_id":"29"
"title": "Learning C# by Developing Games with Unity 3D", "author": "Terry Norton","edition": 1,"synopsis": "For the absolute beginner to any concept of programming, writing a script can appear to be an impossible hurdle to overcome. The truth is, there are only three simple concepts to understand: 1) having some type of information; 2) using the information; and 3) communicating the information. Each of these concepts is very simple and extremely important. These three concepts are combined to access the feature set provided by Unity." ,"amazon_rating": 3.9,"release_date": "2013-09-25","tags": ["Unity C# scripts", "GameObjects and Component objects", "Unity's Scripting"]
"index":"_index":"books","_id":"30"
"title": "Pro C# 9 with .NET 5", "author": "Andrew Troelsen and Phillip Japikse","edition": 1,"synopsis": "This essential classic provides a comprehensive foundation in the C# programming language and the framework it lives in. Now in its 10th edition, you will find the latest C# 9 and .NET 5 features served up with plenty of "behind the curtain" discussion designed to expand developers’ critical thinking skills when it comes to their craft. Coverage of ASP.NET Core, Entity Framework Core, and more, sits alongside the latest updates to the new unified .NET platform, from performance improvements to Windows Desktop apps on .NET 5, updates in XAML tooling, and expanded coverage of data files and data handling. Going beyond the latest features in C# 9, all code samples are rewritten for this latest release." ,"amazon_rating": 4.7,"release_date": "2021-06-08","tags": ["C# 9 features", "ASP.NET Core web applications and web services", "C# and modern frameworks for services"]
"index":"_index":"books","_id":"31"
"title": "Python Crash Course", "author": "Eric Matthes","edition": 2,"synopsis": "Reading books is a kind of enjoyment. Reading books is a good habit. We bring you a different kinds of books. You can carry this book where ever you want. It is easy to carry. It can be an ideal gift to yourself and to your loved ones. Care instruction keep away from fire." ,"amazon_rating": 4.7,"release_date": "2019-05-09","tags": ["Software Architecture", "Functional Programming", "General Introduction to Programming"]
"index":"_index":"books","_id":"32"
"title": "Automate The Boring Stuff With Python", "author": "Al Sweigart","edition": 2,"synopsis": "Reading books is a kind of enjoyment. Reading books is a good habit. We bring you a different kinds of books. You can carry this book where ever you want. It is easy to carry. It can be an ideal gift to yourself and to your loved ones. Care instruction keep away from fire." ,"amazon_rating": 4.7,"release_date": "2019-10-17","tags": ["Software Architecture", "Functional Programming", "General Introduction to Programming"]
"index":"_index":"books","_id":"33"
"title": "Python Projects for Beginners", "author": "Connor P. Milliken","edition": 1,"synopsis": "Immerse yourself in learning Python and introductory data analytics with this book’s project-based approach. Through the structure of a ten-week coding bootcamp course, you’ll learn key concepts and gain hands-on experience through weekly projects." ,"amazon_rating": 4.7,"release_date": "2019-11-16","tags": ["Python language", "Python Data Analysis library", "Anaconda, Jupyter Notebooks, and the Python Shell"]
"index":"_index":"books","_id":"34"
"title": "Python All-in-One For Dummies", "author": "John C. Shovic and Alan Simpson","edition": 2,"synopsis": "Powerful and flexible, Python is one of the most popular programming languages in the world. It's got all the right stuff for the software driving the cutting-edge of the development world—machine learning, robotics, artificial intelligence, data science, etc. The good news is that it’s also pretty straightforward to learn, with a simplified syntax, natural-language flow, and an amazingly supportive user community." ,"amazon_rating": 4.3,"release_date": "2021-04-09","tags": ["Python Building Blocks", "Artificial Intelligence and Python", "Data Science and Python"]
"index":"_index":"books","_id":"35"
"title": "Learning Python", "author": "Mark Lutz ","edition": 1,"synopsis": "Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutz’s popular training course, this updated fifth edition will help you quickly write efficient, high-quality code with Python. It’s an ideal way to begin, whether you’re new to programming or a professional developer versed in other languages." ,"amazon_rating": 4.5,"release_date": "2013-07-06","tags": ["Software quality", "Enjoyment", "Powerful Object-Oriented Programming"]
"index":"_index":"books","_id":"36"
"title": "Data Structure and Algorithmic Thinking with Python", "author": "Narasimha Karumanchi","edition": 1,"synopsis": ""Data Structure and Algorithmic Thinking with Python" is designed to give a jump-start to programmers, job hunters and those who are appearing for exams. All the code in this book are written in Python. It contains many programming puzzles that not only encourage analytical thinking, but also prepares readers for interviews. This book, with its focused and practical approach, can help readers quickly pick up the concepts and techniques for developing efficient and effective solutions to problems." ,"amazon_rating": 4.2,"release_date": "2015-01-29","tags": ["Algorithmic Programming", "Introduction to Programming", "Python Programming"]
"index":"_index":"books","_id":"37"
"title": "Python for Data Analysis", "author": "Wes Mckinney","edition": 2,"synopsis": "Get complete instructions for manipulating, processing, cleaning, and crunching datasets in Python. Updated for Python 3.6, the second edition of this hands-on guide is packed with practical case studies that show you how to solve a broad set of data analysis problems effectively. You’ll learn the latest versions of pandas, NumPy, IPython, and Jupyter in the process. " ,"amazon_rating": 4.6,"release_date": "2017-11-03","tags": ["pandas, NumPy, IPython, and Jupyter", "Python Programming", "Data Analytics"]
"index":"_index":"books","_id":"38"
"title": "Effective Pandas", "author": "Matt Harrison", "edition": 1, "synopsis": "Best practices for manipulating data with Pandas. This book will arm you with years of knowledge and experience that are condensed into an easy to follow format. Rather than taking months reading blogs and websites and searching mailing lists and groups, this book will teach you how to write good Pandas code." ,"amazon_rating": 4.9,"release_date": "2021-12-08","tags": ["Data Manipulation", "Visualization", "Programming Languages"]
"index":"_index":"books","_id":"39"
"title": "Python by Example", "author": "Terry Norton","edition": 1,"synopsis": "Python is today's fastest growing programming language. This engaging and refreshingly different guide breaks down the skills into clear step-by-step chunks and explains the theory using brief easy-to-understand language. Rather than bamboozling readers with pages of mind-numbing technical jargon, this book includes 150 practical challenges, putting the power in the reader's hands." ,"amazon_rating": 4.5,"release_date": "2019-06-06","tags": ["Introduction To Programming", "Python Programming"]
"index":"_index":"books","_id":"40"
"title": "Python Distilled", "author": "David Beazley","edition": 1,"synopsis": "The richness of modern Python challenges developers at all levels. How can programmers who are new to Python know where to begin without being overwhelmed? How can experienced Python developers know they're coding in a manner that is clear and effective? How does one make the jump from learning about individual features to thinking in Python at a deeper level? Dave Beazley's new Python Distilled addresses these and many other real-world issues." ,"amazon_rating": 4.7,"release_date": "2021-11-09","tags": ["Introduction To Programming", "Python Programming"]
"index":"_index":"books","_id":"41"
"title": "Kotlin Programming", "author": "Josh Skeen, Andrew Bailey and David Greenhalgh","edition": 1,"synopsis": "Kotlin is a statically typed programming language designed to interoperate with Java and fully supported by Google on the android operating system. It is also a multiplatform language that can be used to write code that can be shared across platforms including macOS, ios, Windows, and JavaScript." ,"amazon_rating": 5.0,"release_date": "2022-01-13","tags": ["Kotlin Essentials", "Kotlin concepts and foundational APIs"]
v "index":"_index":"books","_id":"42"
"title": "Java to Kotlin", "author": "Duncan McGregor and Nat Pryce","edition": 1,"synopsis": "It takes a week to travel the 8,000 miles overland from Java to Kotlin. If you're an experienced Java developer who has tried the Kotlin language, you were probably productive in about the same time. " ,"amazon_rating": 5.0,"release_date": "2021-08-24","tags": ["Kotlin from scratch", "mixed language codebase"]
"index":"_index":"books","_id":"43"
"title": "Kotlin in Action", "author": "DDmitry Jemerov","edition": 1,"synopsis": "Kotlin is a new programming language targeting the Java platform. It offers on expressiveness and safety without compromising simplicity,seamless interoperability with existing Java code, and great tooling support. Because Kotlin generates regular Java bytecode and works together with existing Java libraries and frameworks, it can be used almost everywhere where Java is used today - for server-side development, Android apps, and much more." ,"amazon_rating": 4.7,"release_date": "2017-03-27","tags": ["IMobile Phone Programming", "Introduction to Programming
"]
"index":"_index":"books","_id":"44"
"title": "Head First Kotlin", "author": "Dawn Griffiths and David Griffiths","edition": 1,"synopsis": "This hands-on book helps you learn the Kotlin language with a unique method that goes beyond syntax and how-to manuals, and teaches you how to think like a great Kotlin developer. You ll learn everything from language fundamentals to collections, generics, lambdas, and higher-order functions." ,"amazon_rating": 4.4,"release_date": "2019-02-28","tags": ["Introduction To Programming", "Kotlin language"]
"index":"_index":"books","_id":"45"
"title": "Kotlin Cookbook", "author": "Ken Kousen","edition": 1,"synopsis": "Use Kotlin to build Android apps, web applications, and more—while you learn the nuances of this popular language. With this unique cookbook, developers will learn how to apply thisJava-based language to their own projects. Both experienced programmers and those new to Kotlin will benefit from the practical recipes in this book." ,"amazon_rating": 4.4,"release_date": "2019-11-22","tags": ["Introduction To Programming", "Mobile Phone Programming", "Android and Spring"]
"index":"_index":"books","_id":"46"
"title": "The Joy of Kotlin", "author": "Pierre-Yves Saumont Saumont","edition": 1,"synopsis": "The Joy of Kotlin teaches readers the right way to code in Kotlin. This insight-rich book covers everything needed to master the Kotlin language while exploring coding techniques that will make readers better developers no matter what language they use." ,"amazon_rating": 4.3,"release_date": "2019-05-20","tags": ["Introduction To Programming", "Safe handling of errors and exceptions", "Dealing with optional data"]
"index":"_index":"books","_id":"47"
"title": "Discovering Statistics Using R", "author": "Andy Field, Jeremy Miles and Zoe Field","edition": 1,"synopsis": "Discovering Statistics Using R takes students on a journey of statistical discovery using R, a free, flexible and dynamically changing software tool for data analysis that is becoming increasingly popular across the social and behavioural sciences throughout the world." ,"amazon_rating": 4.5,"release_date": "2012-03-22","tags": ["Programming Languages & Tools", "Psychological Methodology", "R Programming"]
"index":"_index":"books","_id":"48"
"title": "R for Data Science", "author": "Garrett Grolemund and Hadley Wickham","edition": 1,"synopsis": "What exactly is data science? With this book, you’ll gain a clear understanding of this discipline for discovering natural laws in the structure of data. Along the way, you’ll learn how to use the versatile R programming language for data analysis." ,"amazon_rating": 4.7,"release_date": "2016-07-25","tags": ["Data Wrangling", "Data Visualization", "Exploratory Data Analysis"]
"index":"_index":"books","_id":"49"
"title": "Geocomputation with R", "author": "Robin Lovelace, Jakub Nowosad and Jannes Muenchow","edition": 1,"synopsis": "Geocomputation with R is for people who want to analyze, visualize and model geographic data with open source software. It is based on R, a statistical programming language that has powerful data processing, visualization, and geospatial capabilities." ,"amazon_rating": 5.0,"release_date": "2019-03-21","tags": ["Geocomputation", "R Programming"]
"index":"_index":"books","_id":"50"
"title": "R Cookbook", "author": "Jd Long and Paul Teetor","edition": 1,"synopsis": "Perform data analysis with R quickly and efficiently with more than 275 practical recipes in this expanded second edition. The R language provides everything you need to do statistical work, but its structure can be difficult to master. These task-oriented recipes make you productive with R immediately. Solutions range from basic tasks to input and output, general statistics, graphics, and linear regression." ,"amazon_rating": 4.6,"release_date": "2019-07-12","tags": ["Data Analysis, Statistics, and Graphics", "R Programming"]
Mulit-match query
以下清单显示了跨这三个字段( title,synopsis 和 tags)搜索 Java 的查询:
GET books/_search?filter_path=**.hits
"_source": false,
"query":
"multi_match":
"query": "Java",
"fields": [
"title",
"synopsis",
"tags"
]
,
"highlight":
"fields":
"title": ,
"tags":
上面搜索针对 title, synopsis 及 tags 进行查询。任何一个字段含义 Java 的,将被搜索到。它返回的结果为:
"hits":
"hits": [
"_index": "books",
"_id": "1",
"_score": 2.895521,
"highlight":
"title": [
"Core <em>Java</em> Volume I – Fundamentals"
],
"tags": [
"Programming Languages, <em>Java</em> Programming"
]
,
"_index": "books",
"_id": "6",
"_score": 2.490448,
"highlight":
"title": [
"<em>Java</em> Concurrency in Practice"
],
"tags": [
"<em>Java</em> Programming"
]
,
"_index": "books",
"_id": "7",
"_score": 2.490448,
"highlight":
"title": [
"Test-Driven: TDD and Acceptance TDD for <em>Java</em> Developers"
],
"tags": [
"<em>Java</em> Programming"
]
,
"_index": "books",
"_id": "8",
"_score": 2.2779927,
"highlight":
"tags": [
"<em>Java</em> Programming"
]
,
"_index": "books",
"_id": "4",
"_score": 2.1310725,
"highlight":
"title": [
"<em>Java</em> - The Complete Reference"
]
,
"_index": "books",
"_id": "2",
"_score": 2.0172975,
"highlight":
"title": [
"Effective <em>Java</em>"
]
,
"_index": "books",
"_id": "9",
"_score": 1.902419,
"_ignored": [
"synopsis.keyword"
],
"highlight":
"title": [
"<em>Java</em> Performance: The Definite Guide"
]
,
"_index": "books",
"_id": "5",
"_score": 1.8384864,
"highlight":
"title": [
"Head First <em>Java</em>"
]
,
"_index": "books",
"_id": "10",
"_score": 1.5168624
,
"_index": "books",
"_id": "3",
"_score": 1.3304513,
"highlight":
"title": [
"<em>Java</em>: A Beginner’s Guide"
]
]
我们认为它和如下的搜索近似,但是计分的方法不同:
GET books/_search?filter_path=**.hits
"_source": false,
"query":
"bool":
"should": [
"match":
"title": "Java"
,
"match":
"synopsis": "Java"
,
"match":
"tags": "Java"
]
,
"highlight":
"fields":
"title": ,
"tags":
上述查询的结果和前面的结果是一样的,但是返回的文档的排序是不同的。这源于两种搜索方法的计分规则不同而导致的。上述查询返回的结果是:
"hits":
"hits": [
"_index": "books",
"_id": "6",
"_score": 5.9732165,
"highlight":
"title": [
"<em>Java</em> Concurrency in Practice"
],
"tags": [
"<em>Java</em> Programming"
]
,
"_index": "books",
"_id": "1",
"_score": 5.967611,
"highlight":
"title": [
"Core <em>Java</em> Volume I – Fundamentals"
],
"tags": [
"Programming Languages, <em>Java</em> Programming"
]
,
"_index": "books",
"_id": "7",
"_score": 4.76928,
"highlight":
"title": [
"Test-Driven: TDD and Acceptance TDD for <em>Java</em> Developers"
],
"tags": [
"<em>Java</em> Programming"
]
,
"_index": "books",
"_id": "2",
"_score": 3.9979239,
"highlight":
"title": [
"Effective <em>Java</em>"
]
,
"_index": "books",
"_id": "4",
"_score": 3.722776,
"highlight":
"title": [
"<em>Java</em> - The Complete Reference"
]
,
"_index": "books",
"_id": "8",
"_score": 3.6387753,
"highlight":
"tags": [
"<em>Java</em> Programming"
]
,
"_index": "books",
"_id": "5",
"_score": 3.6034803,
"highlight":
"title": [
"Head First <em>Java</em>"
]
,
"_index": "books",
"_id": "9",
"_score": 3.3518178,
"_ignored": [
"synopsis.keyword"
],
"highlight":
"title": [
"<em>Java</em> Performance: The Definite Guide"
]
,
"_index": "books",
"_id": "3",
"_score": 2.6121073,
"highlight":
"title": [
"<em>Java</em>: A Beginner’s Guide"
]
,
"_index": "books",
"_id": "10",
"_score": 1.5168624
]
上述结果文档 ID 的排序为:
6 1 7 2 4 8 5 9 3 10
这个和之前的文档 ID 排序:
1 6 7 8 4 2 9 5 10 3
是不同的。究其原因,在默认的情况下,multi-match 所使用的 type 是 best fields。
假如我们在查询中定义 multi-match 的 type 为 most_fields:
GET books/_search?filter_path=**.hits
"_source": false,
"query":
"multi_match":
"query": "Java",
"fields": [
"title",
"synopsis",
"tags"
],
"type": "most_fields"
,
"highlight":
"fields":
"title": ,
"tags":
我们将看到和上面使用 bool 查询一样的查询结果:6 1 7 2 4 8 5 9 3 10。
multi_match 查询需要一组字段以及搜索条件。 我们通过合并各个字段的所有结果来获得汇总结果。
Best field
你可能想知道当我们搜索多个字段时,文档的相关性是什么? 匹配更多单词的字段得分更高。 也就是说,如果我们在多个字段中搜索 Java Collections,则两个单词匹配的字段(比如 synopsis)比一个(或没有)匹配的字段更相关。 在这种情况下,具有此 synopsis 字段的文档被设置为具有较高的相关性分数。
匹配所有搜索条件的字段称为最佳字段(best field)。 在前面的示例中,假设 synopsis 包含 Java 和 Collections 这两个词,我们可以简单地说 synopsis 是最好的字段。 运行查询时,multi-match 在后台使用 best_fields 类型。 此类型是 multi_match queries 的默认类型。 当然,还有其他类型的字段,我们很快就会看到。
让我们重写我们在之前清单中编写的查询,但这次我们没有让 Elasticsearch 使用 best_fields type 的默认设置,而是通过覆盖 type 字段来特别提到它。 以下清单显示了生成的查询。
GET books/_search
"_source": false,
"query":
"multi_match":
"query": "Design Patterns",
"type": "best_fields",
"fields": [
"title",
"synopsis"
]
,
"highlight":
"fields":
"tags": ,
"title":
在清单中,我们在 title 和 synopsis 字段中查询 Design Patterns。 这一次,我们明确指定查询使用 multi_match query 的 best_fields 类型。
注意:multi_match query 的默认类型设置为 best_fields。 best_fields 算法将单词最多的字段排在单词最少的字段之前。
如果你查看响应和分数(请参阅以下代码片段),你会发现 “Head First Design Patterns” 一书的得分为 6.7926607,而 “Head First Object-Oriented Analysis Design book” 一书的得分为 2.8500392 :
"hits": [
"_index": "books",
"_id": "10",
"_score": 6.7926607,
"highlight":
"title": [
"Head First <em>Design</em> <em>Patterns</em>"
]
,
"_index": "books",
"_id": "8",
"_score": 2.8500392,
"highlight":
"title": [
"Head First Object-Oriented Analysis <em>Design</em>"
]
...
还有其他类型的多重匹配查询,例如 cross_fields、most_fields、phrase、phrase_prefix 等。 我们可以使用 type 参数来设置查询的类型,以在多个字段中搜索最佳匹配。 但是,我们不会深入研究所有这些类型,因此请参阅 Elasticsearch 的文档以获取更多信息。
如果您想知道 Elasticsearch 如何执行 multi_match 查询,在幕后,它是使用 disjunction max (dis_max) 查询重写的。 我们将在下一节详细讨论这种查询类型。
Disjunction max (dis_max) queries
在上一节中,我们查看了 multi_match query,其中针对多个字段搜索条件。 这种查询类型如何在幕后执行? Elasticsearch 使用 disjunction max query (dis_max) 重写了 multi_match query。 dis_maxquery 将每个字段拆分为单独的匹配查询,如下面的清单所示。
GET books/_search
"_source": false,
"query":
"dis_max":
"queries": [
"match": "title": "Design Patterns",
"match": "synopsis": "Design Patterns"]
如果返回的文档与多个查询子句匹配,则 dis_max 查询会为该文档分配任何匹配子句中的最高相关性分数,以及任何其他匹配子查询的打破平局增量。
从这个清单中可以看出,多个字段在 dis_maxquery 下被拆分为两个匹配查询。 查询返回单个字段具有高相关性 _score 的文档。
注意:dis_max 查询被归类为复合查询:一种包含其他查询的查询。
在某些情况下,multi_match query 期间多个字段的相关分数可能相同。 在这种情况下,比分最终打成平手。 为了打破平局,我们使用 tie breaker,将在下一节中讨论。
Tie breakers
相关分数是根据单字段的分数计算的,但是如果分数打平,我们可以指定 tie_breaker 来解除打分。 如果我们使用 tie_breaker,Elasticsearch 计算的总分略有不同,我们很快就会看到,但首先,让我们检查一个例子。
以下清单针对几个字段查询几个词:title 和 tags。 但是,该清单还显示了一个附加参数 tie_breaker。
GET books/_search
"query":
"multi_match":
"query": "Design Patterns",
"type": "best_fields",
"fields": ["title","tags"],
"tie_breaker": 0.9
当我们使用 best_fields 类型搜索设计模式并指定多个字段(上面列表中的字段 title 和 synopsis)时,我们可以提供一个 tie_breaker 来克服任何分数相同的情况。 当我们提供 tie breaker 时,总分计算如下:
总分 = 最佳匹配字段 _score + 其他匹配字段 _score * tie_breaker
在前面,我们使用了 dis_max query。 事实上,Elasticsearch 将所有 multi_match 查询转换为 dis_max query。 例如,上面清单中的 multi_match query 可以重写为 dis_maxquery,如下一个清单所示:
GET books/_search
"_source": false,
"query":
"dis_max":
"queries": [
"match": "title": "Design Patterns",
"match": "synopsis": "Design Patterns"],
"tie_breaker": 0.5
,
"highlight":
"fields":
"title": ,
"synopsis": ,
"tags":
如你所见,以前写为 multi_match query 的同一查询现在被重写为 dis_max query。 事实上,这正是 Elasticsearch 在幕后所做的。
因为我们搜索多个字段,有时我们可能希望为特定字段赋予额外权重(例如,在 title 中找到我们的搜索词比出现在冗长的 synopsis 或 tags 字段中的相同搜索词更相关)。 我们如何让 Elasticsearch 知道我们的意图是给 title 字段额外的权重? 这正是我们在提升单个查询时所做的,将在下一节中讨论。
个别字段提升
通常在网站或应用程序上为用户提供搜索栏,以便他们可以搜索产品、书籍或评论等内容。 当用户输入几个词时,他们不会说他们只对搜索特定领域中的那些词感兴趣。 例如,当我们在亚马逊上搜索 C# 书籍时,我们不会要求亚马逊仅在特定类别(如 title 或 synopsis)中进行搜索。 我们只需在文本框中输入字符串,然后让亚马逊来计算结果。 这正是我们可以使用个别字段提升来做的!
在 multi_match query 中,我们可以提高(提升)特定字段的条件。 比如说,在搜索 C# 指南时,我们认为在 title 中查找单词比在 tags 中查找更重要。 在这种情况下,我们只是通过使用插入符号和数字来提高 title 字段的重要性:例如 title²。 以下清单显示了此场景的完整查询。
GET books/_search
"query":
"multi_match":
"query": "C# Guide",
"fields": ["title^2", "tags"]
在此清单中,我们将 title 字段的重要性加倍,因此如果在 title 字段中找到文本 C# Guide,则该文档的得分将高于在 tags 字段中找到的文档。
以上是关于Elasticsearch:Multi-match (multi_match) 及 Disjunction max 查询的主要内容,如果未能解决你的问题,请参考以下文章