将弹性搜索查询转换为python中的Elasticsearch-dsl

时间:2015-12-09 12:43:59

标签: python elasticsearch elasticsearch-dsl

这是一个简单的弹性搜索查询。我必须使用弹性搜索dsl模块将其转换为python代码。

 GET indexforproject/project/_search
  {
    "query": {
      "filtered": {
        "query": {"match_all": {}},
        "filter": {
          "term": {
            "project_language.languageName.raw": "nodejs"
          }
        }
      }
    }
  }

这就是我用的

from elasticsearch import Elasticsearch
    from elasticsearch_dsl import Search,Q,query,F
    client = Elasticsearch([{'host':'blrkec248770d','port':'9200'}])
    temp="Internal"
    s=Search(using=client, index="indexforproject").filter("term","project_language.languageName.raw"="Internal")
    body={
    'query':"PHP and node.js",  
    'filters':[{'name':"languages",'values':"[python,PHP,angular]"}   ]
    }


    response=s.execute()
    for hit in response:
        print hit.title

1 个答案:

答案 0 :(得分:2)

我终于明白了。以下是代码:

from elasticsearch import Elasticsearch
    from elasticsearch_dsl import Search,Q,query,F
    client = Elasticsearch([{'host':'blrkec248770d','port':'9200'}])
    d={'project_language.languageName.raw':'nodejs'}
    s=Search(using=client, index="indexforproject").filter('term',**d)
    body={
    'query':"PHP and node.js",  
    'filters':[{'name':"languages",'values':"[python,PHP,angular]"}   ]
    }

    #print s.to_dict()
    response=s.execute()
    for hit in response:
        print hit.title