duanbei7035 2014-04-27 20:34
浏览 82
已采纳

动态弹性搜索查询

I've just started using elasticsearch and I'm unsure how you go about using dynamic search queries. Currently I am using something like this:

$params['body'] = '
    {
        "query": {
            "multi_match": {
                "query": "' . $terms . '",
                    "fields": [
                    "title^1",
                    "description"
                    ],
                    "minimum_should_match": "70%"
            }
        },
        "highlight": {
            "pre_tags" : ["<b>"],
            "post_tags" : ["</b>"],
            "fields": {
                "description": {
                    "fragment_size": 400
                    },
                    "title": {
                    "number_of_fragments": 0
                    }
                    }
                }
            }
        }';

This works fine when $terms is just a string like ball. What I'm trying to do is be able for a user to search by string ball but maybe they want a ball from a particular location also and would have an optional location check box,if clicked a $state could be part of the query now. I looked into Bool Query
but I just wasn't sure if this was the right approach. Any guidance would be great.

Updated code

        {
        "query": {
            "bool": {
                "must": [
                {
                    "term": {
                        "description": {
                            "value": "' . $terms .'"
                        }
                    }
                },
                {
                    "term": {
                        "state_id": {
                        "value": "' . $location . '"
                        }
                    }
                }
                ]
            } 
        }
    }';

I'm just using a simple if(isset() in PHP to see if the terms are there and one for the location. If there is no location $_GET variable i'm just setting it as null.

  • 写回答

1条回答 默认 最新

  • dongyied24121 2014-04-28 05:33
    关注

    The bool query is one of the best approach for querying more than one query.

    curl -XPOST "http://192.168.31.16:9200/appviewx/ro/_search" -d'
    {
    "query": {
        "bool": {
            "must": [
               {
                   "term": {
                      "object": {
                         "value": "ball"
                      }
                   }
               },
               {
                   "term": {
                      "location": {
                         "value": "state"
                      }
                   }
               }
            ]
        }
    }
    }'
    

    Put these in if block

    curl -XPOST.      "http://192.168.31.16:9200/appviewx/ro/_search"     -d'
    {
     "query": {
    "bool": {
        "must": [
           {
               "term": {
                  "object": {
                     "value": "ball"
                  }
               }
           }
        ]
    }
    }
    

    }'

    There are three types in bool.Must,Should,Must_not.

    1)Should act as like OR condition

    2)Must act as like AND condition

    3)Must_not act as like NOT condition

    Use filter as much as u can to increase performance. refer

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘