【问题标题】:Elasticsearch multiple search conditionsElasticsearch 多种搜索条件
【发布时间】:2014-07-24 21:29:20
【问题描述】:

我正在尝试通过以下方式进行弹性搜索查询:

  • 搜索查询
  • 功能分数查询 - 其中一个位于嵌套位置,另一个用于提高与特定类型匹配的文档的分数
  • 文档必须匹配{ verified: true }

我有一个名为users 的索引,在该索引中有两种类型——type1 和type2,每一种都有完全相同的映射。 users 索引中的示例文档如下:

{
  name: 'Brian Fantana',
  location: {
    coordinates: {
      lat: 22.266858, 
      lon: 114.152069
    },
    country: 'United States',
    city: 'New York'
  },
  verified: true
}

和映射:

{
  name: { type: 'string' },
  location: {
    properties: {
      coordinates: {
        type: 'geo_point',
        lat_lon: true
      }
    }
  },
  verified: { type: 'boolean' }
}

我尝试了以下方法:

{
  query: {
    bool: {
      must: { match: { verified: true } }
    },
    query_string: {
      query: <query_string>
    },
    function_score: {
      functions: [{
        script_score: "doc['_type'].value == 'type1' ? _score * 100 : _score"
      }]
    },
    nested: {
      path: 'location',
      query: {
        function_score: {
          functions: [{
            gauss: {
              'location.coordinates': <location>,
              scale: '50km'
            }
          }]
        }
      }
    }
    }]
  }
}

显然,我不知道您将如何组合这样的查询。对不起,我是个白痴。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    这就是我想出的。这是针对 ES v1.2.2 运行的——这很重要,因为早期版本没有像这样为脚本公开 _type 字段。在 1.3.0 中,不推荐使用动态脚本。

    你可以看到我正在索引几个文档来演示评分功能的成功应用。

        #!/bin/sh
    echo "--- delete index"
    curl -X DELETE 'http://localhost:9200/so_multi_search/'
    echo "--- create index and put mapping into place"
    curl -XPUT "http://localhost:9200/so_multi_search/?pretty=true" -d '{
        "mappings": {
            "type1": {
                "properties": {
                    "group":  { "type": "integer" },
                    "verified": { "type": "boolean" },
                    "name": { "type": "string" },
                    "location": {
                        "properties": {
                            "coordinates": {
                                "type": "geo_point",
                                "lat_lon": true
                            }
                        }
                    }
                }
            },
            "type2": {
                "properties": {
                    "group":  { "type": "integer" },
                    "verified": { "type": "boolean" },
                    "name": { "type": "string" },
                    "location": {
                        "properties": {
                            "coordinates": {
                                "type": "geo_point",
                                "lat_lon": true
                            }
                        }
                    }
                }
            }
        },
        "settings" : {
            "number_of_shards" : 1,
            "number_of_replicas" : 0
        }
    }'
    echo "--- index users by POSTing"
    curl -XPOST "http://localhost:9200/so_multi_search/type1" -d '{
        "name": "Brian Fantana",
        "location": {
            "coordinates": {
                  "lat": 40.712784,
                  "lon": -74.005941
            },
            "country": "United States",
            "city": "New York"
        },
        "group": 1,
        "verified": true
    }'
    curl -XPOST "http://localhost:9200/so_multi_search/type2" -d '{
        "name": "Brian Fantana",
        "location": {
            "coordinates": {
                  "lat": 40.712784,
                  "lon": -74.005941
            },
            "country": "United States",
            "city": "New York"
        },
        "group": 2,
        "verified": true
    }'
    curl -XPOST "http://localhost:9200/so_multi_search/type2" -d '{
        "name": "Anna Fantana",
        "location": {
            "coordinates": {
                  "lat": 40.82,
                  "lon": -73.5
            },
            "country": "United States",
            "city": "New York"
        },
        "group": 2,
        "verified": true
    }'
    echo "--- flush index"
    curl -XPOST 'http://localhost:9200/_flush'
    echo "--- search the users"
    curl -XGET "http://localhost:9200/so_multi_search/_search?pretty=true" -d '{
        "query": {
            "filtered": {
                "query": {
                    "function_score": {
                        "functions": [
                            {
                                "filter": {
                                    "query": {
                                        "query_string": {
                                            "default_field": "name",
                                            "query": "Fantana"
                                        }
                                    }
                                },
                                "script_score": {
                                    "script": "doc[\"_type\"].value == \"type1\" ? _score * 100 : _score"
                                }
                            },
                            {
                                "gauss": {
                                    "location.coordinates": {
                                        "origin": "40.712784, -74.005941",
                                        "scale": "50km"
                                    }
                                }
                            }
                        ]
                    }
                },
                "filter": {
                    "query": {
                        "bool": {
                            "must": {
                                "match": {
                                    "verified": true
                                }
                            }
                        }
                    }
                }
            }
        }
    }'
    

    输出:

    --- delete index
    {"acknowledged":true}--- create index and put mapping into place
    {
      "acknowledged" : true
    }
    --- index users by POSTing
    {"_index":"so_multi_search","_type":"type1","_id":"bAPnG5KfQXu_QIgGvY1mmA","_version":1,"created":true}{"_index":"so_multi_search","_type":"type2","_id":"XtCC8L6QRKueCdPsncAOpg","_version":1,"created":true}{"_index":"so_multi_search","_type":"type2","_id":"qlXiyXpQTySCLdMcL-DILw","_version":1,"created":true}--- flush index
    {"_shards":{"total":8,"successful":8,"failed":0}}--- search the users
    {
      "took" : 4,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "failed" : 0
      },
      "hits" : {
        "total" : 3,
        "max_score" : 100.0,
        "hits" : [ {
          "_index" : "so_multi_search",
          "_type" : "type1",
          "_id" : "bAPnG5KfQXu_QIgGvY1mmA",
          "_score" : 100.0,
          "_source":{
        "name": "Brian Fantana",
        "location": {
            "coordinates": {
                  "lat": 40.712784,
                  "lon": -74.005941
            },
            "country": "United States",
            "city": "New York"
        },
        "group": 1,
        "verified": true
    }
        }, {
          "_index" : "so_multi_search",
          "_type" : "type2",
          "_id" : "XtCC8L6QRKueCdPsncAOpg",
          "_score" : 1.0,
          "_source":{
        "name": "Brian Fantana",
        "location": {
            "coordinates": {
                  "lat": 40.712784,
                  "lon": -74.005941
            },
            "country": "United States",
            "city": "New York"
        },
        "group": 2,
        "verified": true
    }
        }, {
          "_index" : "so_multi_search",
          "_type" : "type2",
          "_id" : "qlXiyXpQTySCLdMcL-DILw",
          "_score" : 0.5813293,
          "_source":{
        "name": "Anna Fantana",
        "location": {
            "coordinates": {
                  "lat": 40.82,
                  "lon": -73.5
            },
            "country": "United States",
            "city": "New York"
        },
        "group": 2,
        "verified": true
    }
        } ]
      }
    }
    

    【讨论】:

      【解决方案2】:

      好的,在阅读更多查询 DSL 之后,这是我的解决方案:

      query: {
          function_score: {
              query: {
                  query_string: {
                      query: query + '*'
                  },
              },
              filter: {
                  term: {
                      verified: true
                  }
              },
              functions: [{
                  script_score: {
                      script: "doc['_type'].value == 'type1' ? _score * 1000 : _score"
                  },
                  gauss: {
                      'location.coordinates': {
                          origin: [location.latitude, location.longitude],
                          scale: '20km'
                      } 
                  }
              }]
          }
      }
      

      【讨论】:

      • 你确定这个工作吗?我认为使用这种语法,“查询”被删除。文档讲述的是 "(query|filter)": {}, 而不是 "query": {}, "filter": {},
      猜你喜欢
      • 2011-09-28
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      相关资源
      最近更新 更多