I'm not very strong in Elasticsearch. I'm trying to set up search in my app and got some strange problems. I have two documents:
{
"title": "Second insight"
"content": "Bla bla bla"
"library": "workspace"
}
{
"title": "Test source"
"content": "Bla bla bla"
"library": "workspace"
}
Then, I want to be able to make a search by text fields like title
and content
and apply some filters on fields like library
. I have a query:
{
"query": {
"bool": {
"should": [
{ "match": { "title": "insight" }}
],
"filter": [
{
"term": {
"library": "workspace"
}
}
]
}
}
}
Despite the fact that I clearly defined title
to be matched to insight
, the query above returns both documents, not only the first one.
If I remove filter
block:
{
"query": {
"bool": {
"should": [
{ "match": { "title": "insight" }}
]
}
}
}
the query returns correct results.
Then, I also tried to make a partial search. For some reasons, the query uses ins
instead of insight
below doesn't work, so, it returns empty list:
{
"query": {
"bool": {
"should": [
{ "match": { "title": "ins" }}
]
}
}
}
How should I make partial search? And how can I set up filters correctly? In other words, how to make a search partial query by some fields, but at the same time filtered by other fields?
Thanks.
CodePudding user response:
You need to supply minimum_should_match
in your first query.
I did the following and only got a single document (your desired outcome)
POST test_things/_search
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"match": {
"title": "insight"
}
}
],
"filter": [
{
"term": {
"library": "workspace"
}
}
]
}
}
}
As for why ins
doesn't work, it depends on your mapping analyzer being used. You are matching against analyzed terms in the index, if you want to match against ins
you need to change your analyzer (possibly using the ngram tokenizer) or use a wildcard query.