2017-10-04 56 views
0

我想在ElasticSearch兩個字段進行搜索,字段有:如何在ElasticSearch中使用AND編碼?

1)SEARCHTERM(文本字段)

2)市(文本字段)

如果我使用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() 
     } 
    } 
    } 
], 
} 

請幫忙

KS

Randheer

回答

1

和條件,可以用must子句中你bool query

編輯建模: 我明白你的問題,你的城市的查詢是不正確的應該是:

{ 
    "bool": { 
    "should": [{ 
     "multi_match": { 
      "query": '"' + term.toLowerCase() + '"', 
      "fields": 
      [ 
      "title", "desc" 
      ], 
      "operator": "and" 
     } 
     }, { 
     "wildcard": { 
      "city": city.toLowerCase() 
     } 
     } 
    ] 
    } 
} 
+0

我試過了,但如果我只用城市那麼它不會工作, 「必須」工作,如果這兩個值輸入它dosn't單場不起作用! –

+0

什麼不在你的查詢中工作? – MartinSchulze

+0

如果我只想跟城市一起搜索就不會工作 –

1

帶有must子句的Bool查詢(使用捲曲): -

1.)select * from table where term = 'ABC' 與城市= '墨西哥'

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.)SELECT * FROM表,其中術語= 'ABC'

curl -XGET 'localhost:9200/indexName/_search?pretty&size=50' -H 'Content-Type: application/json' -d' { 
"query":{ 
    "bool":{ 
    "must":[{ 
     "term":{ "term":"abc" } 
    }] 
    } 
} 

}';

3)SELECT * FROM表,其中城市= '墨西哥'

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