如何在Rails中运行查询后检查elasticsearch令牌?

时间:2016-05-26 13:42:33

标签: ruby-on-rails elasticsearch

我的问题如下:

我在rails应用程序中使用特定设置在我的索引和搜索分析器中运行弹性搜索查询,问题是它不会在应用程序中返回任何结果,另一方面当我尝试直接从我的elasticsearch运行它时码头工人,我有令牌返回。如果我在我的应用查询中使用这些令牌,我会得到结果......

所以这是我的弹性搜索查询:

curl -XGET 'localhost:9200/development-stoot-services/_analyze?analyzer=search_francais' -d 'cours de guitare'
{"tokens":[{"token":"cour","start_offset":0,"end_offset":5,"type":"<ALPHANUM>","position":1},{"token":"guitar","start_offset":9,"end_offset":16,"type":"<ALPHANUM>","position":3}]}

这是从我的rails应用程序到elasticsearch的查询:

query = {
   "query" : {
     "bool" : {
       "must" : [
         {
           "range" : {
             "deadline" : {
               "gte" : "2016-05-26T10:27:19+02:00"
             }
           }
         },
         {
           "terms" : {
             "state" : [
               "open"
             ]
           }
         },
         {
           "query_string" : {
             "query" : "cours de guitare",
             "default_operator" : "AND",
             "fields" : [
               "title",
               "description",
               "brand",
               "category_name"
             ]
           }
         }
       ]
     }
   },
   "filter" : {
     "and" : [
       {
         "geo_distance" : {
           "distance" : "40km",
           "location" : {
             "lat" : 48.855736,
             "lon" : 2.32927300000006
           }
         }
       }
     ]
   },
   "sort" : [
     {
       "created_at" : "desc"
     }
   ]
 } 

最后一个查询不会返回任何结果,但是如果我尝试使用elasticsearch返回的标记(&#39; cour&#39;,&#39;吉他&#39;),我会得到预期的结果。所以我想rails和elasticsearch之间有一个问题,我找不到... 任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:1)

尝试像这样修改您的查询,即您需要在search_francais中指定query_string分析器,以便像对cours de guitare一样分析_analyze } endpoint:

     ...
     {
       "query_string" : {
         "query" : "cours de guitare",
         "default_operator" : "AND",
         "analyzer": "search_francais",       <--- add this line
         "fields" : [
           "title",
           "description",
           "brand",
           "category_name"
         ]
       }
     },
     ...