ElasticSearch - 查找具有确切值

时间:2017-05-31 21:06:18

标签: database elasticsearch

我正在使用弹性搜索,我尝试进行简单的查询工作。我使用的是elasticseach 2.3。我无法使用我使用的其他技术限制的较新版本。

基本上我将新闻存储在数据库中,其中包含标题,来源和出版日期。 我试图制作的查询应该搜索包含某个关键字的所有新闻,来自某些来源A或B,并且发布日期在给定范围内。

到目前为止,我有这个:

    {  
        "query":{  
            "bool":{  
                "must":[  
                    {  
                        "bool":{  
                            "should":[  
                                {  
                                    "match":{  
                                        "source":"SOURCE_A"
                                    }
                                },
                                {
                                    "match":{
                                        "source":"SOURCE_B"
                                    }
                                },
                                {
                                   "match":{
                                       "title": "keyword"
                                   }
                                }
                            ]
                        }
                    }
                ],
                "filter":{  
                    "range":{  
                        "publication_date":{  
                            "gte":"DATE_FROM",
                            "lte":"DATE_TO"
                        }
                    }
                }
            }
        }
    }

问题在于,如果给定的源与另一个源完全相同(例如:" SOURCE"和" SOURCE ABC"),它们都包含在结果中。我想完全匹配相同的来源。 有人能指出我正确的方向吗?

谢谢!

该指数由Django Haystack创建,但由于其局限性,我需要自己查询数据库。索引映射如下:

    {
      "myindex": {
        "mappings": {
          "modelresult": {
            "properties": {
              "django_ct": {
                "type": "string",
                "index": "not_analyzed",
                "include_in_all": false
              },
              "django_id": {
                "type": "string",
                "index": "not_analyzed",
                "include_in_all": false
              },
              "id": {
                "type": "string"
              },
              "publication_date": {
                "type": "date",
                "format": "strict_date_optional_time||epoch_millis"
              },
              "source": {
                "type": "string",
                "analyzer": "ascii_analyser"
              },
              "summary": {
                "type": "string",
                "analyzer": "ascii_analyser"
              },
              "text": {
                "type": "string",
                "analyzer": "ascii_analyser"
              },
              "title": {
                "type": "string",
                "analyzer": "ascii_analyser"
              },
              "url": {
                "type": "string",
                "analyzer": "ascii_analyser"
              }
            }
          }
        }
      }
    }

0 个答案:

没有答案