【问题标题】:Query and exclude in ElasticSearch在 ElasticSearch 中查询和排除
【发布时间】:2020-08-24 11:23:15
【问题描述】:

我正在尝试将match_phrase_prefix 查询与排除查询一起使用,以便它匹配除要排除的术语之外的所有术语。我在基本的 URI 查询中找到了它,但不是常规的 JSON 查询。如何将此 URI 转换为 JSON 类型的查询?

"http://127.0.0.1:9200/topics/_search?q=name:"
                        + QUERY + "* AND !name=" + CURRENT_TAGS

其中CURRENT_TAGS 是不匹配的标签列表。

这是我目前所拥有的:

{
  "query": {
    "bool": {
      "must": {
        "match_phrase_prefix": {
          "name": "a"
        }
      },
      "filter": {
        "terms": {
          "name": [
            "apple"
          ]
        }
      }
    }
  }
}

但是,当我这样做时,apple 仍然包含在结果中。如何排除apple

【问题讨论】:

  • @user2896120您已经很长时间没有接受并支持我的答案了,如果您的问题得到解决,如果您可以接受并支持答案,那就太好了

标签: elasticsearch


【解决方案1】:

你快到了,你可以使用must_not,它是boolean query 的一部分来排除你不想要的文档,下面是你的示例的工作示例。

索引映射

{
 
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      }
  }
}
}

将示例文档索引为appleamazon 符合您搜索条件的世界最大公司:)

搜索查询以排除 apple

{
  "query": {
    "bool": {
      "must": {
        "match_phrase_prefix": {
          "name": "a"
        }
      },
      "must_not": {
        "match": {
          "name": "apple"
        }
      }
    }
  }
}

搜索结果

 "hits": [
      {
        "_index": "matchprase",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.6931471,
        "_source": {
          "name": "amazon"
        }
      }
    ]

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 1970-01-01
相关资源
最近更新 更多