问题描述
我正在为两个指定的字段做一个简单的查询,手册和谷歌被证明是没有什么帮助的。下面的例子应该很清楚我想做什么。
I am trying to do a simple query for two specified fields, and the manual and google is proving to be of little help. Example below should make it pretty clear what I want to do.
{
"query": {
"and": {
"term": {
"name.family_name": "daniel",
"name.given_name": "tyrone"
}
}
}
}
作为一个奖金问题,为什么找到丹尼尔Tyrone与丹尼尔,但不是如果我搜索丹尼尔。它的行为就像一个真实的奇怪的反区分大小写搜索。
As a bonus question, why does it find "Daniel Tyrone" with "daniel", but NOT if I search for "Daniel". It behaves like a realy weird anti case sensitive search.
推荐答案
编辑:更新,对不起。您需要在Bool查询内的每个字段单独的术语
对象:
Updated, sorry. You need a separate Term
object for each field, inside of a Bool query:
{
"query": {
"bool": {
"must" : [
{
"term": {
"name.family_name": "daniel"
}
},
{
"term": {
"name.given_name": "tyrone"
}
}
]
}
}
}
ElasticSearch不会对术语查询进行分析,这使得它们区分大小写。一个术语查询表示ES在您的索引中查找这个确切的令牌,包括案例和标点符号。
Term queries are not analyzed by ElasticSearch, which makes them case sensitive. A Term query says to ES "look for this exact token inside your index, including case and punctuation".
如果你想要不区分大小写,您可以在分析器中添加关键字+小写过滤器。或者,您可以使用在查询时分析文本的查询(例如 Match
查询)
If you want case insensitivity, you could add a keyword + lowercase filter to your analyzer. Alternatively, you could use a query that analyzes your text at query time (like a Match
query)
Edit2:您也可以使用And或Bool过滤器。
You could also use And or Bool filters too.
这篇关于一个简单的AND查询与弹性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!