Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic search multiple keywords in multiple fields

I'm trying to build a query in Elastic search to search inside the text of multiple fields for example: Title and Description are the fields and I want to search for "obama", "world", "news" inside these 3 fields. I've tryed searching and found information in Elastic website here:
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#_literal_fields_literal_and_per_field_boosting But the problem is when I tried this structures it produce errors:
First:

{
  "multi_match" : {
    "query":      "obama world news",
    "type":       "cross_fields",
    "fields":     [ "Title", "Description" ],
    "operator":   "and"
  }
}

It produces error like this:

{
    "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][0]: SearchParseException[[republishan2][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [    {\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n    }]]]; nested: SearchParseException[[republishan2][0]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][3]: SearchParseException[[republishan2][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [    {\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n    }]]]; nested: SearchParseException[[republishan2][3]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][4]: SearchParseException[[republishan2][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [    {\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n    }]]]; nested: SearchParseException[[republishan2][4]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][1]: SearchParseException[[republishan2][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [    {\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n    }]]]; nested: SearchParseException[[republishan2][1]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][2]: SearchParseException[[republishan2][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [    {\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n    }]]]; nested: SearchParseException[[republishan2][2]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }]",
    "status": 400
}

I also have tried this query:

{
    "sort": {
        "re_max": {
            "order": "desc"
            }
            }, 
    "query": {
      "multi_match" : {
        "query":      "obama world news",
        "type":       "cross_fields",
        "fields":     [ "Title", "Description" ],
        "operator":   "and"
      }
    },
    "from": 0, 
    "size": 30}
}

And this is the result:

{
    "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][0]: SearchParseException[[republishan2][0]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@1ef6a497>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B@52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][3]: SearchParseException[[republishan2][3]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@364d93ed>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B@52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][4]: SearchParseException[[republishan2][4]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@6e98bb14>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B@52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][1]: SearchParseException[[republishan2][1]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@6c0543f7>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B@52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][2]: SearchParseException[[republishan2][2]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@215084be>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n      \"multi_match\" : {\n        \"query\":      \"obama world news\",\n        \"type\":       \"cross_fields\",\n        \"fields\":     [ \"Title\", \"Description\" ],\n        \"operator\":   \"and\"\n      }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B@52afc562; line: 7, column: 3]]; }]",
    "status": 400
}

I'm using Postman Rest client to run the query. What's the problem with my code?

like image 342
ehsan shirzadi Avatar asked Jun 22 '14 11:06

ehsan shirzadi


1 Answers

Try like this

{
  "query": {
    "multi_match": {
      "query": "obama world news",
      "type": "cross_fields",
      "fields": ["Title", "Description"],
      "operator": "and"
    }
  },
  "from": 0,
  "size": 20,
  "sort": [
    {
      "re_max": {
        "order": "desc"
      }
    }
  ]
}

HOpe it helps..!

like image 128
BlackPOP Avatar answered Oct 31 '22 22:10

BlackPOP