Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
<img src="//i.stack.imgur.com/RUiNP.png" height="16" width="18" alt="" class="sponsor tag img">elasticsearch ElasticSearch查询以排除某些结果_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Filter - Fatal编程技术网 elasticsearch ElasticSearch查询以排除某些结果,elasticsearch,filter,elasticsearch,Filter" /> elasticsearch ElasticSearch查询以排除某些结果,elasticsearch,filter,elasticsearch,Filter" />

elasticsearch ElasticSearch查询以排除某些结果

elasticsearch ElasticSearch查询以排除某些结果,elasticsearch,filter,elasticsearch,Filter,我正在尝试编写一个允许过滤结果集的ElasticSearch查询。该应用程序为职务提供了一个筛选器,也为相同的职务提供了一个排除筛选器。例如,在下面的数据集中,我想筛选工程师,但也排除软件工程师。问题是,现在的查询还排除了首席软件工程师,而且不应该排除 以下是我使用的数据: { "data": [ { "email": "chris@example.com", "job_title"

我正在尝试编写一个允许过滤结果集的ElasticSearch查询。该应用程序为职务提供了一个筛选器,也为相同的职务提供了一个排除筛选器。例如,在下面的数据集中,我想筛选
工程师
,但也排除
软件工程师
。问题是,现在的查询还排除了首席软件工程师
,而且不应该排除

以下是我使用的数据:

{
  "data": [
    {
      "email": "chris@example.com",
      "job_title": "Industrial Electrical Engineer"
    },
    {
      "email": "esther@example.com",
      "job_title": "Chief Revenue Officer"
    },
    {
      "email": "george@example.com",
      "job_title": "Principal Software Engineer"
    },
    {
      "email": "helena@example.com",
      "job_title": "Software Engineer"
    },
    {
      "email": "john@example.com",
      "job_title": "Engineer"
    },
    {
      "email": "judith@example.com",
      "job_title": "Design Engineer"
    },
    {
      "email": "katy@example.com",
      "job_title": "Software Designer"
    },
    {
      "email": "mark@example.com",
      "job_title": "Engineer"
    },
    {
      "email": "mary@example.com",
      "job_title": "Mechanical Design Engineer"
    },
    {
      "email": "tom@example.com",
      "job_title": "Electrical Engineer"
    },
    {
      "email": "tony@example.com",
      "job_title": "Chief Executive Officer"
    }
  ]
}
以下是ElasticSearch查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "job_title": {
                    "query": "Engineer",
                    "operator": "and"
                  }
                }
              }
            ]
          }
        }
      ],
      "filter": [
        {
          "term": {
            "user_id": 1
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "match": {
                  "job_title": "Software Engineer"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

您可以在“不得”子句中使用,仅排除确切的短语“软件工程师”。

假设
职务
文本类型。如果未指定分析器,Elasticsearch将为
文本
类型字段使用。这将把“软件工程师”分为

更新1:

如果您正在使用elasticsearch 7.10或更高版本,并且希望使搜索不区分大小写并搜索确切的术语,则可以使用

否则,如果您使用的是低于7.10的版本,则需要修改索引映射,如下所示,然后重新为数据编制索引

{
  "settings": {
    "analysis": {
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "job_title": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "normalizer": "my_normalizer"
          }
        }
      }
    }
  }
}

我已经试过了。
match\u phrase
的问题是,如果我想排除
软件工程师
,它也排除了
首席软件工程师
。我认为您描述的行为应该只针对“phrase\u prefix”@CosminThank您的回答。我确实用
.keyword
尝试了这个版本,但没有收到任何结果。在你的帖子之后,我又试了一次,但这次写的是
软件工程师
,而不是像我以前那样写的
软件工程师
,它成功了。@Cosmin很高兴这对你有效:-)你能接受答案吗:-)@Cosmin它不适用于小写字母,因为关键字分析器寻找精确的匹配,而不是作为标准分析器的分析文本,是否可以调整为不区分大小写?我确信用户总是会键入小写。@Cosmin是的,您可以这样做,但为此,您需要更改索引映射。您能告诉我您使用的是哪个版本的elasticsearch吗?
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "job_title": {
                    "query": "Engineer",
                    "operator": "and"
                  }
                }
              }
            ]
          }
        }
      ],
      "filter": [
        {
          "bool": {
            "must_not": [
              {
                "term": {
                  "job_title.keyword": "Software Engineer"
                }
              }
            ]
          }
        }
      ]
    }
  }
}
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "job_title": {
                    "query": "Engineer",
                    "operator": "and"
                  }
                }
              }
            ]
          }
        }
      ],
      "filter": [
        {
          "bool": {
            "must_not": [
              {
                "term": {
                  "job_title.keyword": {
                    "value": "software engineer",
                    "case_insensitive": true
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
{
  "settings": {
    "analysis": {
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "job_title": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "normalizer": "my_normalizer"
          }
        }
      }
    }
  }
}