2014-03-28 180 views
0

我在弹性搜索过滤时遇到问题。我想筛选订单行的索引。像这样的SQL查询:弹性搜索过滤问题

SELECT * FROM orderrow WHERE item_code = '7X-BogusItem'

这里是我的elasticsearch查询:

GET /myindex/orderrow/_search 
{ 
    "query": { 
     "constant_score": { 
      "filter": { 
       "term": { 
        "item_code": "7X-BogusItem" 
       } 
      } 
     } 
    } 
} 

我越来越没有结果返回。然而,当我运行此查询时:

GET /myindex/orderrow/_search 
{ 
    "query": { 
     "query_string": { 
      "query": "7X-BogusItem" 
     } 
    } 
} 

我得到正确的结果。我究竟做错了什么?

+1

答案是描述你的问题。如果你想保持搜索行为,添加一个你不分析的multi_field。使用过滤器而不是查询可能会更好。你不是在寻找得分,而是寻找完全匹配。在做订单行时,你也应该看看嵌套的对象。这应该提供足够的信息进一步调查。 –

回答

3

你可以尝试用:

GET /myindex/orderrow/_search 
{ 
    "query": { 
     "constant_score": { 
      "filter": { 
       "query": { 
        "query_string": { 
         "query": "7X-BogusItem" 
        } 
       } 
      } 
     } 
    } 
} 

的事情是,QUERY_STRING查询,而搜索项查询不被分析。可能您的数据7X-BogusItem在索引期间由默认分析器转换为像7xbogusitem这样的术语。当您尝试使用术语7X-BogusItem进行查询时,它不起作用,因为您没有术语7X-BogusItem - 您只有术语7xbogusitem。但是执行query_string会将您的查询7X-BogusItem转换为底层条目7xbogusitem,并且它会找到您想要的。

如果您不希望您的文字7X-BogusItem通过分析转化做,你可以现场item_code改变映射选项"index" : "not_analyzed"

您可以检查你的数据将是什么样子经过分析:

curl -XGET "localhost:9200/_analyze?analyzer=standard&pretty" -d '7X-BogusItem' 
{ 
    "tokens" : [ { 
    "token" : "7x", 
    "start_offset" : 0, 
    "end_offset" : 2, 
    "type" : "<ALPHANUM>", 
    "position" : 1 
    }, { 
    "token" : "bogusitem", 
    "start_offset" : 3, 
    "end_offset" : 12, 
    "type" : "<ALPHANUM>", 
    "position" : 2 
    } ] 
} 

所以对于文本7X-BogusItem我们在指数方面7xbogusitem