繁体   English   中英

elasticsearch中的完全匹配查询

[英]exact match query in elasticsearch

我正在尝试在ES中运行完全匹配查询

在MYSQL我的查询将是:

SELECT * WHERE `content_state`='active' AND `author`='bob' AND `title` != 'Beer';

我在这里查看了ES文档:

https://www.elastic.co/guide/zh-CN/elasticsearch/guide/current/_finding_exact_values.html

并提出了这个:

{
"from" : '.$offset.', "size" : '.$limit.',
"filter": {
"and": [
  {
    "and": [
      {
        "term": {
          "content_state": "active"
        }
      },
      {
        "term": {
          "author": "bob"
        }
      },
      {
        "not": {
          "filter": {
            "term": {
              "title": "Beer"
            }
          }
        }
      }
    ]
  }
    ]
  }
}

但我的搜索结果仍然以标题= Beer为准,似乎并没有排除= Beer的标题。

我做错什么了吗?

我对ES很陌生

我想通了,我改用这个...

{
 "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": {
    "query_string": {
      "query": "(content_state:active AND author:bob) AND NOT (title:Beer)"
    }
  }
}

过滤器应该在精确值上工作 ,如果您以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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM