如何在URI搜索中的多个字段上进行搜索



我想使用URI搜索(q =)在Elasticsearch中执行和操作。我该怎么做?

如果我有这样的文件:[{" name":" test 1"," pub":" 2"},{" name":" test 2"," pub":" 1"},{" name":" a",":" 1"}]

,我想查询包含包含"测试"和酒吧等于" 1"的名称的文档。我该怎么做?

谢谢!

假设您的文档看起来像这样:

{
    "my_field": [
        { "name":"Test 1", "pub":"2"}, 
        { "name":"Test 2", "pub":"1"}, 
        { "name":"A", "pub":"1"}
    ]
}

my_field的映射类型nested类似于此:

{
  "mappings": {
    "doc_type": {
      "properties": {
        "my_field": {
          "type": "nested",
          "properties": {
            "name": { "type": "string" },
            "pub": {"type": "integer" }
          }
        }
      }
    }
  }
}

然后,您可以查询索引并使用以下nested查询获取预期文档:

POST /_search
{
  "query": {
    "nested": {
      "path": "my_field",
      "query": {
        "bool": {
          "filter": [
            {
              "match": {
                "name": "Test"
              }
            },
            {
              "match": {
                "pub": 1
              }
            }
          ]
        }
      }
    }
  }
}

实际上您需要嵌套字段。以下是一个很好的资源。

https://www.elastic.co/guide/en/elasticsearch/guide/guide/current/nested-objects.html

最新更新