简体   繁体   English

Elasticsearch过滤器

[英]Elasticsearch Filters

I will receive the filter inputs from the front-end via node js some may be null or empty but in the elasticsearch query filter all the field should be present . 我将通过节点js从前端接收过滤器输入,其中某些输入可能为null或为空,但在elasticsearch查询过滤器中,所有字段均应存在。 How to get response even the inputs are empty for some filters ? 即使某些过滤器的输入为空,如何获得响应?

 elasticClient.search({
                 index: 'pubmed-index',
                 type: 'pubmed-paper',
                 body: {
                  query: {
                      bool: {
                          must: {
                              match: {
                                  Abstract: `${searchKeyword}`
                              }
                          },
                          filter:
                          {"or":[{
                                  term: 
                                  {
                                  "Publication Type": `${searchPublicationType}`
                                  }},
                              {

                              "range": {
                                "Date Completed": {
                                    "gte" :`${searchGreaterYear}||/y`,
                                    "lte" : `${searchLesserYear}||/y`,
                                    "format" :"yyyy"
                                }
                            }
                          }
                           ]}



                      }
                  }
              }
               }).then(function (resp) {
                   console.log(resp.hits.hits);
                   return res.json(resp.hits.hits)
               }, function (err) {
                   console.log(err.message);
                   return res.json(err.message)
               });

I need the output either for publication type or date completed if any of the fields are missing 如果缺少任何字段,则需要发布类型或完成日期的输出

I can suggest you take a look on the search template functionality provided by Elasticsearch. 我建议您看一下Elasticsearch提供的搜索模板功能。

It uses mustache as a template language and allows to have for example conditional clauses: 它使用胡子作为模板语言,并允许使用例如条件子句:

{
  "query": {
    "bool": {
      "must": {
        "match": {
          "line": "{{text}}" 
        }
      },
      "filter": {
        {{#line_no}} 
          "range": {
            "line_no": {
              {{#start}} 
                "gte": "{{start}}" 
                {{#end}},{{/end}} 
              {{/start}} 
              {{#end}} 
                "lte": "{{end}}" 
              {{/end}} 
            }
          }
        {{/line_no}} 
      }
    }
  }
}

and few more options. 和更多的选择。

I was using it on production for a similar case - to omit some filters when a user does not put any value for them. 在类似情况下,我在生产中使用了它-当用户没有为它们添加任何值时,就省略一些过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM