ElasticSearch匹配值列表中的值

时间:2018-02-22 16:27:37

标签: symfony elasticsearch foselasticabundle

我正在使用ElasticSearch(在使用FosElasticaBundle的Symfony 4项目中),我不知道如何构建如下查询:

Match if value in list of values

我有一个字段"code",如果此字段"code"的值为:"first or "second" or "third"

,我只需要检索一个元素

我尝试了条款:

  • 必须查询:没有检索到任何内容,也许必须是"first AND "second" AND "third"
  • 应该查询:即使我有另一个值而不是"first or "second" or "third",一切都被检索到了,所以查询是没用的吗?

我尝试使用Match(应该,必须)或逐个使用......没有...

那么,我怎样才能在elasticsearch中做到这一点?谢谢!

1 个答案:

答案 0 :(得分:0)

PUT some-test

PUT /some-test/doc/1
{
  "foo": "first"
}

PUT /some-test/doc/2
{
  "foo": "second"
}

PUT /some-test/doc/3
{
  "foo": "fourth"
}

POST some-test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "foo": "first second third"
          }
        }
      ]
    }
  }
}

给了我

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "some-test",
        "_type": "doc",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "foo": "second"
        }
      },
      {
        "_index": "some-test",
        "_type": "doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "foo": "first"
        }
      }
    ]
  }
}