简体   繁体   English

ElasticSearch / Searchkick - LIKE查询

[英]ElasticSearch / Searchkick - LIKE query

I'm using ElasticSearch gem Searchkick with Rails and I want to perform a search based on 2 values: 我正在使用ElasticSearch gem Searchkick和Rails,我想基于2个值执行搜索:

- params[:full_text_query]
- params[:only_from_this_location]

One should search full-text on all fields and another should restrict the search only to records that partially match one field. 应该在所有字段上搜索全文,而另一个应该仅将搜索限制为部分匹配一个字段的记录。

I want something like this: 我想要这样的东西:

Post.search(params[:full_text_query], where: { location: params[:only_from_this_location] })

However I need location filter to match not only exact values, but also partial ones. 但是我需要位置过滤器不仅要匹配精确值,还要匹配部分值。 So not only "New York", but also "New York, US". 所以不仅“纽约”,还有“纽约,美国”。 Like %something% you would do in SQL. 就像在%SQL中你会做的那样。

I also need support for pagination so it needs to be in one query. 我还需要支持分页,因此需要在一个查询中。

What are my options? 我有什么选择?

Regular expressions are allowed to use inside Searchkick where method. 正则表达式允许在Searchkick里面使用where方法。 Try using something like this in your code. 尝试在代码中使用这样的东西。

Post.search(params[:full_text_query], where: { location: /.*#{params[:only_from_this_location]}.*/ })

I'm also struggling with this kind of issue in my current project, if you found anything to solve the query LIKE problem in searchckick please share. 我也在我当前的项目中遇到这种问题,如果你发现任何东西来解决搜索中的问题LIKE问题请分享。

However concerning the pagination you can add the following parameter to your line 但是,关于分页,您可以将以下参数添加到您的行

Post.search(params[:full_text_query], where: { location: params[:only_from_this_location] }, per_page: 10)

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

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