[英]exact match query in elasticsearch
I'm trying to run an exact match query in ES 我正在尝试在ES中运行完全匹配查询
in MYSQL my query would be: 在MYSQL我的查询将是:
SELECT * WHERE `content_state`='active' AND `author`='bob' AND `title` != 'Beer';
I looked at the ES docs here: 我在这里查看了ES文档:
https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_exact_values.html https://www.elastic.co/guide/zh-CN/elasticsearch/guide/current/_finding_exact_values.html
and came up with this: 并提出了这个:
{
"from" : '.$offset.', "size" : '.$limit.',
"filter": {
"and": [
{
"and": [
{
"term": {
"content_state": "active"
}
},
{
"term": {
"author": "bob"
}
},
{
"not": {
"filter": {
"term": {
"title": "Beer"
}
}
}
}
]
}
]
}
}
but my results are still coming back with the title = Beer, it doesn't seem to be excluding the titles that = Beer. 但我的搜索结果仍然以标题= Beer为准,似乎并没有排除= Beer的标题。
did I do something wrong? 我做错什么了吗?
I'm pretty new to ES 我对ES很陌生
I figured it out, I used this instead... 我想通了,我改用这个...
{
"from" : '.$offset.', "size" : '.$limit.',
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "content_state",
"query": "active"
}
},
{
"query_string": {
"default_field": "author",
"query": "bob"
}
}
],
"must_not": [
{
"query_string": {
"default_field": "title",
"query": "Beer"
}
}
]
}
}
}
Query String Query is a pretty good concept to handle various relationship between search criteria. 查询字符串查询是一个很好的概念,可以处理搜索条件之间的各种关系。 Have a quick look into Query string query syntax to understand in detail about this concept
快速浏览查询字符串查询语法 ,以详细了解此概念
{
"query": {
"query_string": {
"query": "(content_state:active AND author:bob) AND NOT (title:Beer)"
}
}
}
Filters are supposed to work on exact values , if you had defined your mapping in a manner where title was a non-analyzed field, your previous attempt ( with filters) would have worked as well. 过滤器应该在精确值上工作 ,如果您以title是未分析字段的方式定义了映射,则先前的尝试(使用过滤器)也可以工作。
{
"mappings": {
"test": {
"_all": {
"enabled": false
},
"properties": {
"content_state": {
"type": "string"
},
"author": {
"type": "string"
},
"title": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.