[英]Search in Elastic Search with AND operator
我的ES中有以下文件
name type
Thomas Robert King xyz
Peter Jacob Junior xyz
Robert Einstein Jr abc
Thomas Jupyin DR xyz
我想獲取所有與Robert和Thomas匹配的文檔,它們都在句子中且類型為xyz。
我創建的當前ES查詢是:
http://localhost/index/type/_search/?size=100&pretty=1/
{
"query": {
"bool": {
"must": [
{
"match":
{ "name": "Thomas" }
},
{
"match":
{ "name": "Robert" }
}
],
"must_not": [
{
{
"match":
{ "type": "abc" }
}
]
}
}
}
是在彈性搜索中創建AND場景的正確方法,還是有更好的查詢來處理相同的輸出。 我正在使用ES 2.1
給定您當前的目標:在句子中獲取所有與Robert和Thomas匹配的文檔,並且它們的類型均為xyz,因此不應在abc上使用must_not查詢。 如果還有其他類型(例如ubv),您的查詢還將獲得與此類型匹配的文檔。
您可以執行以下查詢:
{
"query": {
"bool": {
"must": [
{
"match":
{
"name": {
"query": "Thomas Robert",
"operator": "and"
}
}
},
{
"match":
{
"type": "xyz"
}
}
]
}
}
}
順便說一句,這些子句必須在布爾查詢中為查詢計分。 如果只想過濾xyz類型,則應使用:
{
"query": {
"bool": {
"must": [
{
"match":
{
"name": {
"query": "Thomas Robert",
"operator": "and"
}
}
}],
"filter": [
{
"match":
{
"type": "xyz"
}
}
]
}
}
}
此外,請查看術語級查詢與全文查詢,以了解您的真正需求。 如果未分析“類型”字段,則應使用術語查詢而不是匹配項,以避免在匹配查詢中進行分析。
https://www.elastic.co/guide/zh-CN/elasticsearch/reference/2.3/term-level-queries.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.