elasticsearch 如何将Lucene查询字符串转换为Elasticsearch匹配/匹配前缀等等效项,elasticsearch,solr,lucene,elasticsearch,Solr,Lucene" /> elasticsearch 如何将Lucene查询字符串转换为Elasticsearch匹配/匹配前缀等等效项,elasticsearch,solr,lucene,elasticsearch,Solr,Lucene" />

elasticsearch 如何将Lucene查询字符串转换为Elasticsearch匹配/匹配前缀等等效项

elasticsearch 如何将Lucene查询字符串转换为Elasticsearch匹配/匹配前缀等等效项,elasticsearch,solr,lucene,elasticsearch,Solr,Lucene,我目前正在从SOLR v3迁移到Elasticsearch v5.11。我的问题是,如何将下面的查询字符串转换为Elasticsearch匹配/匹配短语等等效项。这可能吗 (entityName:(john AND lewis OR "john lewis") OR entityNameText:(john AND lewis OR "john lewis")) AND (status( "A" OR "I" status)) 我试着这么做,到目前为止只使用了第一组括号,但似乎不正确: {

我目前正在从SOLR v3迁移到Elasticsearch v5.11。我的问题是,如何将下面的查询字符串转换为Elasticsearch匹配/匹配短语等等效项。这可能吗

(entityName:(john AND lewis OR "john lewis") 
OR entityNameText:(john AND lewis OR "john lewis")) 
AND (status( "A" OR "I" status))
我试着这么做,到目前为止只使用了第一组括号,但似乎不正确:

{
"bool": {
    "should": [
        [{
            "bool": {
                "should": [
                    [{
                        "match_phrase": {
                            "entityName": "john lewis"
                        }
                    }]
                ],
                "must": [
                    [{
                        "match": {
                            "entityName": {
                                "query": "john lewis",
                                "operator": "and"
                            }
                        }
                    }]
                ]
            }
        }, {
            "bool": {
                "should": [
                    [{
                        "match_phrase": {
                            "entityNameText": "john lewis"
                        }
                    }]
                ],
                "must": [
                    [{
                        "match": {
                            "entityNameText": {
                                "query": "john lewis",
                                "operator": "and"
                            }
                        }
                    }]
                ]
            }
        }]
    ]
}
}

谢谢

更新:


entityName和entityNameText都映射为文本类型,并使用用于搜索和查询的自定义分析器。状态映射为关键字类型。

答案将取决于您如何指定映射,但我假设您没有客户映射

让我们先把不同的部分分解,然后再把它们重新组合起来

状态(“A”或“I”状态) 这是一个“terms”查询,可以将其视为SQL“IN”子句

实体名称:(约翰和刘易斯或“约翰刘易斯”) ElasticSearch将字符串字段分解为不同的部分。我们可以通过使用另一个“术语”查询来利用这一点。我们不需要将其指定为3个不同的部分,ES将在引擎盖下处理

"terms": {
              "entityName": [
                "john",
                "lewis"
              ]
            }
entityNameText:(约翰和刘易斯或“约翰刘易斯”)) 与上面的逻辑完全相同,只是在不同的字段上搜索

“条款”:{ “entityNameText”:[ “约翰”, “刘易斯” ] }

和vs或 在ES查询中。And=“must”或=“should”

把它们放在一起 下面是我用来测试查询的完整设置的链接


为将来对此感兴趣的人发布答案。 不完全确定原因,但我使用ES查询DSL编写了两个备选查询,发现它们与原始Lucene查询等效,返回完全相同的结果。不确定这是ES查询DSL的优点还是缺点

原始Lucene查询:

{
"query": {
    "query_string" : {
        "query" : "entityName:(john AND Lewis OR \"john Lewis\") OR entityNameText:(john AND Lewis OR \"john Lewis\")"
    }
}
}

查询备选案文1:

{
"bool": {
    "should": [
        [{
            "bool": {
                "should": [
                    [{
                        "match": {
                            "entityName": {
                                "query": "john Lewis",
                                "operator": "and"
                            }
                        }
                    }, {
                        "match_phrase": {
                            "entityName": "john Lewis"
                        }
                    }]
                ]
            }
        }, {
            "bool": {
                "should": [
                    [{
                        "match": {
                            "entityNameText": {
                                "query": "john Lewis",
                                "operator": "and"
                            }
                        }
                    }, {
                        "match_phrase": {
                            "entityNameText": "john Lewis"
                        }
                    }]
                ]
            }
        }]
    ]
}
}
查询备选方案2

{
"bool": {
    "should": [
        [{
            "multi_match": {
                "query": "john Lewis",
                "type": "most_fields",
                "fields": ["entityName", "entityNameText"],
                "operator": "and"
            }
        }, {
            "multi_match": {
                "query": "john Lewis",
                "type": "phrase",
                "fields": ["entityName", "entityNameText"]
            }
        }]
    ]
}
}
使用此映射:

{
"entity": {
    "dynamic_templates": [{
        "catch_all": {
            "match_mapping_type": "*",
            "mapping": {
                "type": "text",
                "store": true,
                "analyzer": "phonetic_index",
                "search_analyzer": "phonetic_query"
            }
        }
    }],
    "_all": {
        "enabled": false
    },
    "properties": {
        "entityName": {
            "type": "text",
            "store": true,
            "analyzer": "indexed_index",
            "search_analyzer": "indexed_query",
            "fields": {
                "entityNameLower": {
                    "type": "text",
                    "analyzer": "lowercase"
                },
                "entityNameText": {
                    "type": "text",
                    "store": true,
                    "analyzer": "text_index",
                    "search_analyzer": "text_query"
                },
                "entityNameNgram": {
                    "type": "text",
                    "analyzer": "ngram_index",
                    "search_analyzer": "ngram_query"
                },
                "entityNamePhonetic": {
                    "type": "text",
                    "analyzer": "ngram_index",
                    "search_analyzer": "ngram_query"
                }
            }
        },
        "status": {
            "type": "keyword",
            "norms": false,
            "store": true
        }
    }
}
}

嗨,吉尔登,谢谢你。我已更新了原始查询,以显示“entityName”和“entityNameText”映射为文本类型,“Status”映射为关键字类型。如果需要,我可以在回去工作时提供完整的映射。我相信这些额外的信息意味着我们不应该被用于文本字段的术语查询?唯一的区别是状态中的“A”和“I”将是大写的,其他一切都是一样的。
{
"bool": {
    "should": [
        [{
            "multi_match": {
                "query": "john Lewis",
                "type": "most_fields",
                "fields": ["entityName", "entityNameText"],
                "operator": "and"
            }
        }, {
            "multi_match": {
                "query": "john Lewis",
                "type": "phrase",
                "fields": ["entityName", "entityNameText"]
            }
        }]
    ]
}
}
{
"entity": {
    "dynamic_templates": [{
        "catch_all": {
            "match_mapping_type": "*",
            "mapping": {
                "type": "text",
                "store": true,
                "analyzer": "phonetic_index",
                "search_analyzer": "phonetic_query"
            }
        }
    }],
    "_all": {
        "enabled": false
    },
    "properties": {
        "entityName": {
            "type": "text",
            "store": true,
            "analyzer": "indexed_index",
            "search_analyzer": "indexed_query",
            "fields": {
                "entityNameLower": {
                    "type": "text",
                    "analyzer": "lowercase"
                },
                "entityNameText": {
                    "type": "text",
                    "store": true,
                    "analyzer": "text_index",
                    "search_analyzer": "text_query"
                },
                "entityNameNgram": {
                    "type": "text",
                    "analyzer": "ngram_index",
                    "search_analyzer": "ngram_query"
                },
                "entityNamePhonetic": {
                    "type": "text",
                    "analyzer": "ngram_index",
                    "search_analyzer": "ngram_query"
                }
            }
        },
        "status": {
            "type": "keyword",
            "norms": false,
            "store": true
        }
    }
}
}