Elasticsearch结合查询和过滤器不能提供正确的结果

时间:2015-03-07 11:19:58

标签: json elasticsearch dsl

我尝试使用额外的过滤条件制作搜索页面,但我无法按照自己的意愿调查查询。

这是查询示例:

{
    "size": 25,
    "from": 0,
    "sort": {
        "_score": {
            "order": "asc"
        }
    },
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "year": "2015"
                            }
                        }
                    ]
                }
            },
            "query": {
                "match": {
                    "title": "Sense"
                }
            }
        }
    }
}

我只想要从2015年开始的结果。搜索标题' Sense'什么都没有,即使有一排标题' Sense8'。如果我搜索Sense8,它会返回正确的数据,但不会返回#Sense'。

我做错了什么?

由于

1 个答案:

答案 0 :(得分:0)

您可能需要在映射中使用ngramedge ngram分析器。我写了一篇关于在Qbox blog上使用ngrams进行自动完成的博客文章,详细介绍了它,但是这里有一些代码可能会给你你想要的东西:

PUT /test_index
{
   "settings": {
      "analysis": {
         "filter": {
            "ngram_filter": {
               "type": "edgeNGram",
               "min_gram": 2,
               "max_gram": 20,
               "token_chars": [
                  "letter",
                  "digit",
                  "punctuation",
                  "symbol"
               ]
            }
         },
         "analyzer": {
            "ngram_analyzer": {
               "type": "custom",
               "tokenizer": "whitespace",
               "filter": [
                  "lowercase",
                  "asciifolding",
                  "ngram_filter"
               ]
            },
            "whitespace_analyzer": {
               "type": "custom",
               "tokenizer": "whitespace",
               "filter": [
                  "lowercase",
                  "asciifolding"
               ]
            }
         }
      }
   },
   "mappings": {
      "doc": {
          "properties": {
               "year":{
                   "type": "string"
               },
               "title":{
                   "type": "string",
                   "index_analyzer": "ngram_analyzer", 
                   "search_analyzer": "whitespace_analyzer"
               }
           }
      }
   }
}

POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"doc","_id":1}}
{"year": "2015","title":"Sense8"}
{"index":{"_index":"test_index","_type":"doc","_id":2}}
{"year": "2014","title":"Something else"}

POST /test_index/_search
{
    "size": 25,
    "from": 0,
    "sort": {
        "_score": {
            "order": "asc"
        }
    },
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "year": "2015"
                            }
                        }
                    ]
                }
            },
            "query": {
                "match": {
                    "title": "Sense"
                }
            }
        }
    }
}
...
{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": null,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 0.30685282,
            "_source": {
               "year": "2015",
               "title": "Sense8"
            },
            "sort": [
               0.30685282
            ]
         }
      ]
   }
}

您可以在浏览器中运行代码:

http://sense.qbox.io/gist/4f72c182db2017ac7d32077af16cbc3528cb79f0