Mukul Chakravarty
Mukul Chakravarty

Reputation: 111

Convert elastic search query into Elasticsearch-dsl in python

This is a simple working elastic search query. I have to convert this into a python code using elastic search dsl module.

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

This is what i used

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

Upvotes: 4

Views: 2666

Answers (1)

Mukul Chakravarty
Mukul Chakravarty

Reputation: 111

I finally got it. Below is the code :

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

Upvotes: 3

Related Questions