ElasticSearch查询搜索消息并限制为节点ID

时间:2013-04-14 00:26:30

标签: elasticsearch dsl

我在ElasticSearch中有以下映射:

{
  "xenforo" : {
    "post" : {
      "_source" : {
        "enabled" : false
      },
      "properties" : {
        "date" : {
          "type" : "long",
          "store" : "yes"
        },
        "discussion_id" : {
          "type" : "long",
          "store" : "yes"
        },
        "message" : {
          "type" : "string"
        },
        "node" : {
          "type" : "long"
        },
        "thread" : {
          "type" : "long"
        },
        "title" : {
          "type" : "string"
        },
        "user" : {
          "type" : "long",
          "store" : "yes"
        }
      }
    },

我想在消息字段中搜索单词test,但将其限制为节点ID 5.如何修改此DSL搜索?

$data_string = '{
"from" : 0, "size" : 100,
"sort" : [
    { "date" : {"order" : "desc"} }
],
"query": {
        "match" : {
            "message" : {
                "query" : "test"
            }
        }
    }
}';

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用filtered查询:

{
  "from": 0,
  "size": 100,
  "sort": [{
    "date": {
      "order": "desc"
    }
  }],
  "query": {
    "filtered": {
      "query": {
        "match": {
          "message": {
            "query": "test"
          }
        }
      },
      "filter": {
        "term": {
          "node": 5
        }
      }
    }
  }
}