在python中将弹性搜索查询转换为Elasticsearchdsl

2024-05-15 09:31:56 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

这就是我用过的

^{pr2}$

Tags: 模块代码projectsearchgetmatchallfilter
1条回答
网友
1楼 · 发布于 2024-05-15 09:31:56

我终于明白了。代码如下:

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

相关问题 更多 >