查询ElasticSearch哈希中是否存在密钥

时间:2015-10-20 04:45:24

标签: hash elasticsearch

如何检查我的查询字词是否属于我的某个字段中的键?例如,这是一个存储的文档:

{
  field1: "some value",
  field2: "some other value",
  field3: {
    something: [1,2],
    else: [2,3]
  }
}

查询“something”应返回该文档。查询“some value”也应该返回该文档。这是我到目前为止所做的:

{
  query: {
    filtered: {
      query: {
        multi_match: {
          query: query,
          fields: ['field1', 'field2'],
          operator: 'and'
        }
      },
      filter: {
        or: [
          {
            exists: { field: "field3"}
          }
        ]
      }
    } 
  }
}

2 个答案:

答案 0 :(得分:0)

假设你想要“某种价值”,以adjesent方式,以下应该可以正常工作 -

{
  "query": {
    "filtered": {
      "filter": {
        "exists": {
          "field": "field3"
        }
      },
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "match_phrase": {
                      "field1": "some value"
                    }
                  },
                  {
                    "match_phrase": {
                      "field2": "some value"
                    }
                  }
                ]
              }
            },
            {
              "multi_match": {
                "query": "something",
                "fields": [
                  "field1",
                  "field2"
                ],
                "operator": "and"
              }
            }
          ]
        }
      }
    }
  }
}

答案 1 :(得分:0)

{
  query: {
    filtered: {
      filter: {
        or: [
          {
            query: {
              multi_match: {
                query: query,
                fields: ['field1', 'field2'],
                operator: "and"
              }
            }
          },
          {
            exists: { field: "field3.query" }
          }
        ]
      }
    }
  }
}

唯一需要注意的是,如果query是包含多个字词(或数组)的字符串,则您必须为每个字词创建一个exists过滤器。