弹性搜索字符串类型的DSL查询python

时间:2016-12-03 13:29:21

标签: python elasticsearch dsl

我一直在努力尝试但努力做到这一点。 实际上在我的索引映射中,我的所有字段都是类型字符串。 e.g

p=r"(\W|^)(x|y|z|f|g|h)(?=\W|$)"
repl=r"\1<i>\2</i>"
re.sub(p,repl,line)

我在弹性搜索中使用python客户端生成了以下查询:

{"ind_ecue_fin_ult1":{"mappings":{"ind_ecue_fin_ult1":{"properties":{"age":{"type":"string"},"altaDiff":{"type":"string"},"antiguedad":{"type":"string"},"canal_entrada":{"type":"string"},"cod_prov":{"type":"string"},"conyuemp":{"type":"string"},"empIndex":{"type":"string"},"fecDiff":{"type":"string"},"gender":{"type":"string"},"ind_actividad_cliente":{"type":"string"},"indext":{"type":"string"},"indfall":{"type":"string"},"indrel":{"type":"string"},"indrel_1mes":{"type":"string"},"indresi":{"type":"string"},"nomprov":{"type":"string"},"nuevo":{"type":"string"},"others":{"type":"string"},"renta":{"type":"string"},"residence":{"type":"string"},"segmento":{"type":"string"},"tiprel_1mes":{"type":"string"},"wt":{"type":"double"}}}}}}

但是,即使我在“shoulds”中尝试任何一个数组术语,我也会收到此异常:

{'query': {'bool': {'minimum_should_match': 5,
   'should': [{'boost': 4, 'terms': {'age': '1'}},
    {'boost': 3, 'terms': {'antiguedad': '0.0'}},
    {'boost': 3.5, 'terms': {'indrel': '1'}},
    {'boost': 3, 'terms': {'indrel_1mes': '1'}},
    {'boost': 2, 'terms': {'tiprel_1mes': 'A'}},
    {'boost': 3, 'terms': {'indresi': 'S'}},
    {'boost': 2.5, 'terms': {'indext': 'N'}},
    {'boost': 2, 'terms': {'conyuemp': 'DEF'}},
    {'boost': 2, 'terms': {'canal_entrada': 'KHL'}},
    {'boost': 1.5, 'terms': {'indfall': 'N'}},
    {'boost': 1.5, 'terms': {'cod_prov': '28'}},
    {'boost': 2, 'terms': {'nomprov': 'MALAGA'}},
    {'boost': 1.5, 'terms': {'ind_actividad_cliente': '1'}},
    {'boost': 4, 'terms': {'renta': '1'}},
    {'boost': 2.5, 'terms': {'segmento': '02 - PARTICULARES'}},
    {'range': {'wt': {'boost': 5.0, 'gte': 0.4, 'lte': 0.525}}}]}}}

请帮忙。 TIA。

1 个答案:

答案 0 :(得分:0)

我终于弄明白了,正确的语法如下:

curl -XPOST "http://localhost:9200/ind_ahor_fin_ult1/_search" -d'
{
    "query": {
        "bool": {
            "should": [
                { "match": { "segmento":  {"query":"02 - PARTICULARES","boost":2}}},
                { "match": { "renta": {"query":"1","boost":3}}}
            ],
            "minimum_should_match": 2
        }
    }
}'

它的描述非常好:

https://www.elastic.co/guide/en/elasticsearch/guide/current/query-time-boosting.html

我错过了,不知何故。谢谢!