我想在ElasticSearch中搜索两个字段,字段为:
1)SearchTerm(Textfield)
2)City(Textfield)
如果我使用mysql查询将是:
如果两者都有值
select * from table where term = 'abc' AND city = 'mexico'
如果只有字词有值
select * from table where term = 'abc'
如果只有城市有价值
select * from table where city = 'mexico'
我如何在ElasticSearch中使用它我正在尝试以下内容:
"bool" : {
"should": [
{
"multi_match": {
"query": '"'+term.toLowerCase()+'"',
"fields":
[
"title", "desc"
],
"operator" : "and"
}
},
{
"wildcard": {
"city": {
value: city.toLowerCase()
}
}
}
],
}
请帮忙
由于
Randheer
答案 0 :(得分:1)
条件可以使用bool query
中的must
条款建模
编辑: 我理解你的问题,你的城市查询不正确应该是:
{
"bool": {
"should": [{
"multi_match": {
"query": '"' + term.toLowerCase() + '"',
"fields":
[
"title", "desc"
],
"operator": "and"
}
}, {
"wildcard": {
"city": city.toLowerCase()
}
}
]
}
}
答案 1 :(得分:1)
使用must子句进行Bool查询(使用curl): -
1。)从表中选择*,其中term ='abc'和city ='mexico'
curl -XGET 'localhost:9200/indexName/_search?pretty&size=50' -H 'Content-Type: application/json' -d' {
"query":{
"bool":{
"must":[
{
"term":{ "city":"mexico" }
},
{
"term":{ "term":"abc" }
}
]
}
}
}';
2。)从表中选择*,其中term ='abc'
curl -XGET 'localhost:9200/indexName/_search?pretty&size=50' -H 'Content-Type: application/json' -d' {
"query":{
"bool":{
"must":[{
"term":{ "term":"abc" }
}]
}
}
}';
3。)从表中选择*,其中city ='mexico'
curl -XGET 'localhost:9200/indexName/_search?pretty&size=50' -H 'Content-Type: application/json' -d' {
"query":{
"bool":{
"must":[{
"term":{ "city":"mexico" }
}]
}
}
}';
有关详情,请点击此处: - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html