elasticsearch 匹配搜索查询中文档中的所有单词 [英] elasticsearch match all words from document in the search query
问题描述
我们可以像这样搜索特定文档字段中的所有单词:
We can search for ALL words in a specific document.field like this:
{ "query" : { "match" : { "title": { "query" : "Black Nike Mens", "operator" : "and" } } } }
这将在 title
字段中搜索词 Black
、Nike
和 Mens
,这样只有那些返回的文档将在 title
字段中包含所有这些单词.
This will search for the words Black
, Nike
and Mens
in the field title
such that only those documents are returned that will have ALL these words in the title
field.
但我想做的有点不同.
我想这样查找,如果文档的 title
字段的所有单词都出现在我的搜索查询中,那么它将返回该文档.
I want to lookup such that if all the words of the title
field of the document are present in my search query then it will return that document.
例如
假设在elasticsearch数据库中有一个title:Nike Free Sparq Mens White"
的文档
suppose there is a document with title : "Nike Free Sparq Mens White"
in the elasticsearch database
现在,如果我使用 query 进行搜索:Nike Free Sparq 09 - Mens - White/Black/Varsity Red"
那么它应该返回这个文档,因为 document.title 中的所有单词都可以存在于我的查询中
now if I search with a query : "Nike Free Sparq 09 - Mens - White/Black/Varsity Red"
then it should return this document, because all the words in the document.title do exist in my query
但是如果我使用 query 进行搜索:Nike Free Lebron - Mens - White/Black"
那么它不应该返回文档,因为我的查询包含 Sparq
失踪
but if I search with a query : "Nike Free Lebron - Mens - White/Black"
then it should NOT return the document because my query has the word Sparq
missing
这是一种反向运算符搜索
this is a sort of reverse-and-operator search
这可能吗?如果是,那么如何?
Is this possible? If yes, then how?
推荐答案
我终于让它工作了,但不是用直接的方法!
I finally got it to work but not with a direct method!
这就是我所做的:
- 从源查询中创建一个干净的单词列表,方法是:
- 改为小写
- 用空格替换任何特殊字符和标点
- 删除重复的单词
这对我来说效果很好!
除非有人有来自elasticsearch查询语言的直接方法.
Unless someone has a direct method from elasticsearch query language.
这篇关于elasticsearch 匹配搜索查询中文档中的所有单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!