douyueqing1530 2014-03-19 15:55
浏览 106
已采纳

如何使用elasticsearch一次搜索2个字符串?

I'm trying to use elasticsearch to find multiple terms at once.

More specifically, when a user enters a search term like "quake 2", I want elasticsearch to match "quake 2" and "quake ii". So that it matches the result if the games is stored in the system with roman numerals.

I will be using PHP to detect when integers exist in the search term, and then generate the it's counterpart with the roman numerals included, which should be straight forward, so I wasn't looking for help with that.

It's when it comes down to doing the lookup of two strings at once using elasticsearch where I hit a brick wall.

Here's an example of what I've tried:

$json = '{
            "query" : {
                    "terms" : {
                        "title" : [ "quake", "crysis" ],
                        "minimum_should_match" : 1
                    }
                }
            }
        }';
$searchParams['index'] = 'thegamesdb';
$searchParams['type']  = 'game';
$searchParams['body'] = $json;
$elasticResults = $client->search($searchParams);

The above behaves as expected and returns a list of results that include quake and halo, with fairly sane seaach scores for each.

But when I attempt to use the above query to search for "quake 2" and "quake ii" at the same time, I get absolutely no hits? In fact I've determined that it seems to be the inclusion of the spaces between the title and number that is throwing elastic search off.

$json = '{
            "query" : {
                    "terms" : {
                        "title" : [ "quake 2", "quake ii" ],
                        "minimum_should_match" : 1
                    }
                }
            }
        }';

If spaces aren't allowed in a "terms" query, then how am I supposed to be performing this type of search?

  • 写回答

2条回答 默认 最新

  • dongshetao1814 2014-03-19 16:52
    关注

    Split the query into two.use bool query should condition and match query to get appropriate result. match query works because the match query are analyzed and constructed again by ES.

         {
        "query": {
            "bool": {
                "must": [
                   {
                       "match": {
                          "title": "quake 2"
                       }
                   },
                   {
                   "match": {
                      "title": "quake ii"
                   }
                   }
                ]
            }
            }
        }
    

    i ve tried it.. it worked.. HOpe it helps..!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题